-
-
Save thingsinjars/1460798 to your computer and use it in GitHub Desktop.
function( | |
a,b // Placeholders | |
){ | |
with(document.doctype) // To save referencing it each time | |
return '<!DOCTYPE ' // Making an assumption that the doctype starts with this. | |
+ name // Usually 'html' | |
+ ((b=publicId,a=systemId) // Shorthand references, return value is systemId so we can test if there is a system definition | |
? // Then | |
(b // If this is a public standard | |
? // Then | |
' PUBLIC "'+b+'" ' // Write the type as "PUBLIC" and the ID | |
: // Else | |
' SYSTEM ' // Write the type as "SYSTEM" | |
) | |
+ '"'+a+'"' // Add on the system ID | |
: // Else | |
'' // Write nothing | |
) | |
+'>' // ...and done. | |
} |
function(a,b){with(document.doctype)return'<!DOCTYPE '+name+((b=publicId,a=systemId)?(b?' PUBLIC "'+b+'" ':' SYSTEM ')+'"'+a+'"':'')+'>'} |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Simon Madine <http://thingsinjars.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
{ | |
"name": "Detect Doctype", | |
"description": "Returns the doctype of the current page", | |
"keywords": [ | |
"html", | |
"doctype" | |
] | |
} |
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b><!DOCTYPE html></b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var myFunction = function(a,b){with(document.doctype)return'<!DOCTYPE '+name+((b=publicId,a=systemId)?(b?' PUBLIC "'+b+'" ':' SYSTEM ')+'"'+a+'"':'')+'>'} | |
document.getElementById( "ret" ).innerHTML = new Option(myFunction()).innerHTML //Borrowed from @jed's escapeHTML (https://gist.github.com/964847) | |
</script> |
I've just reshuffled it so that it includes the 'SYSTEM' type if it's an obscure custom doctype. If we want to skip that (admittedy, extremely rare) case, I'd go with @p01's. Although it does include an extra space if there isn't a systemid.
Does this code really need to be wrapped into function(){...}
?
At the time any script is executed DOCTYPE is already known, is there any difference between defining new string variable and defining new function that returns that string virtually always?
(Doctype may be changed dynamically, but I had to do so only once and don't want to do it again :) )
Your opinions, please.
We can shorten long property names:
function(a,b){with(document.doctype)return'<!DOCTYPE '+name+((b=publicId,a=systemId)?(b?' PUBLIC "'+b+'" ':' SYSTEM ')+'"'+a+'"':'')+'>'}
Of course. Before the reshuffle, there was nothing to be gained from shortening the variables but it makes sense now. I've updated and expanded in the annotated version.
As for the more general question of 'does this need to be wrapped in a function', I'm interested to know what others think. Keeping with the rules, yes; Keeping with the spirit, not necessarily.
The version below is 2 bytes shorter and makes sure there is a space before the systemId