Why does HTML think “chucknorris” is a color?
stackoverflow.com
stackoverflow.com
"fire" "campfire" "firebrick" "firefly"
"cornflower" "cornflowerblue" "cornflowerred"
"seablue" "skyblue" "seagreen" "seafoam"
The permissiveness had a drawback, though, which was that many people didn't learn correctly what could and couldn't be omitted, or what was and wasn't required. And so badly-formed HTML dominated the Web. Browsers had to bend over backwards to make sense of this and display something, since at the time there was heated competition in the browser space: if Netscape just bailed out with a parsing error and IE didn't, people would use IE because they'd blame the failure on Netscape instead of on the HTML (same thing for IE -- it couldn't bail out with an error because Netscape might look better in comparison by rendering the page).
XHTML -- HTML reformulated on XML instead of SGML -- tried to bring XML's strict well-formedness handling to HTML, but turned out to be an utter failure. See Evan Goer's classic "XHTML 100" for an example of how even experts routinely could not do XHTML properly and had to be saved by browsers which never really applied the strictness correctly:
http://goer.org/Journal/2003/04/the_xhtml_100.html
There was an attempt at XHTML2, but it collapsed under its own ambition.
Enter HTML5, which began as a response to the perceived stagnation of W3C in iterating the HTML standard (since much of their effort was being expended off in markup-astronaut space on XHTML2). HTML5 introduced a bunch of useful things people had been asking for for a while, but also took the approach of codifying how to parse HTML. And not just valid HTML, but anything claiming to be HTML. If you run the HTML5 algorithms, you can get a parse tree out of damn near any kind of junk, and that process is now standardized. In pragmatic fashion, it mostly codified the hacks and workarounds browsers had already come up with.
And that's how we get the HTML5 legacy color parsing algorithm, which can turn almost any junk you stick in a declaration into an RGB color value. Most of what it's doing is trying to filter out obvious cases (matches for named colors and certain hex specifications) early, then figure out a way to turn random junk consistently into a hexadecimal number.
If you're interested, I maintain a Python library for working with HTML/CSS color values, and an implementation of the legacy color parsing algorithm with directives from the spec interspersed as comments so you can step through it and see what's going on:
https://github.com/ubernostrum/webcolors/blob/master/webcolo...
Its documentation also covers the history of how colors are specified in HTML/CSS:
http://webcolors.readthedocs.io/en/1.7/colors.html
But a simple description of the way it works is:
1. Bail out if the value is not something the algorithm can work with. Non-Unicode inputs can't be parsed into a color, and an empty string can't be parsed into a color.
2. Strip leading/trailing whitespace, and look to see if the input matches a CSS named color, or the three-hexadecimal-digit shortcut format of CSS2, shortcutting to parsing via those rules if possible.
3. Run the junk normalizer. This is where it looks complicated, but mostly it's just finding anything that can't possibly be part of a hexadecimal number and replacing it with one or more zeroes, then doing some padding and truncation to get the end result to come out as a hexadecimal string specifying a 24-bit integer. Then that 24-bit integer is the color value.
At that point 'chucknorris' becomes 'c00c0000000'.
In step 11, the value is padded with zeroes until its length is a non-zero multiple of three. At that point it becomes 'c00c00000000'.
In step 12, the value is split into three equal-length sub-values which will become the red, green and blue components of the color value. They are: (c00c, 0000, 0000).
Steps 13 and 14 then attempt to truncate those values, first by reducing them to eight characters in length and then removing leading zeroes (but only if all three sub-values have a leading zero). These make no changes for the sub-values we've arrived at.
Step 15 performs the final truncation: if the sub-values are still of length greater than two, all but the first two characters are removed. At this point the sub-values are: (c0, 00, 00).
Finally, steps 16-20 convert these from hexdecimal to decimal, and return the value: (192, 0, 0).
The color-parsing algorithm which turns "chucknorris" into a color also will return rgb(128, 128, 128) for either spelling, since it specifies that any value which is a case-insensitive match for a CSS3 named color should just short-circuit resolve to that color.
It's fine to love his movies, politics don't effect art in that regard, but if you think he's cool and worthy of a meme, I guess fair enough, as long as you're aware of his politics and consider it to not effect memes should you disagree.