Skip to content

Instantly share code, notes, and snippets.

@thingsinjars
Forked from 140bytes/LICENSE.txt
Created December 11, 2011 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thingsinjars/1460798 to your computer and use it in GitHub Desktop.
Save thingsinjars/1460798 to your computer and use it in GitHub Desktop.
Detect Doctype
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>&lt;!DOCTYPE html&gt;</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>
@subzey
Copy link

subzey commented Dec 12, 2011

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.

@subzey
Copy link

subzey commented Dec 12, 2011

We can shorten long property names:

function(a,b){with(document.doctype)return'<!DOCTYPE '+name+((b=publicId,a=systemId)?(b?' PUBLIC "'+b+'" ':' SYSTEM ')+'"'+a+'"':'')+'>'}

@thingsinjars
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment