编程与软件工程stackoverflow.com
为什么 HTML 会把“chucknorris”当作颜色?(2011)
Why does HTML think “chucknorris” is a color? (2011)
2013 — 2020,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇 Stack Overflow 经典问答解释了为什么在 HTML 中,字符串 “chucknorris” 会被当作一种颜色(深红色 #c00000)。原因在于 HTML 早期为宽容错误输入而采用的解析规则:在颜色值中,非十六进制的字符会被替换为 0,然后取前几位作为颜色码。
为什么经久不衰:它用一个令人啼笑皆非的例子揭示了 HTML 早期设计中的宽容哲学和浏览器解析颜色的怪异行为,既是程序员之间的经典梗,也是理解 Web 历史的有趣窗口。
HN 评论精选
作者分享童年拼错 gray 导致颜色变绿的困惑,最后发现是无效字符被替换为 0 的机制所致。
这确实纠正了我多年的一个误解。刚开始写 HTML 时,我会用英文颜色名,比如 color="red" 或 "white",这很好用。但每次设置灰色时都会得到奇怪结果。原来是我拼写不好(当时还小,现在拼写也烂)。写成 <font color="gray">,颜色却是绿色。什么?我很快意识到拼写错误,但想知道为什么是绿色。我猜解释器读取颜色名直到第一个无效字符(这里得到 "gr"),然后选择最接近的匹配。绿色在字母顺序上在灰色之前,所以……瞧!现在我知道答案很简单:gray 被解释为 00 a0 00。又一个生活谜团解开了!
tobyjsullivan查看英文原文
This definitely corrects a misconception I've had for years. When I first started writing HTML, I would set colours using the english colour names, for example color="red" or color="white" and this worked well for my needs. However, I would always get an odd result when I tried to set the colour grey. Turns out, I didn't spell very well (I was pretty young at the time, though still spell atrociously). <font color="gray"> And the colour would come out green. What? I quickly realised my spelling error but wondered why things ended up green. I decided what was happening was that the interpreter read the colour name up until the first invalid character (in this case resulting in "gr"), then chose the closest match. Green would be before grey in alphabetical order and... voila! I see now the solution was simply that gray was interpreted as 00 a0 00. Another life mystery solved!
解释 HTML 宽松设计的历史原因:早期网页多为手写,为降低非技术用户的门槛而容忍错误输入。
详细说明一下:在早期,HTML 通常是手写的。万维网早期愿景的一部分是任何人都可以编写 HTML 并发布内容。如果允许格式错误的 HTML 导致完全空白页面或错误消息,对技术不太熟练的用户来说太不友好。在早期没有 CSS 和 JavaScript 的日子里,HTML 更像今天的 bbcode、Markdown 或 RST。有趣的是,据我所知,那些语言没有‘格式错误输入’的概念——没有像 XHTML 试图收紧 HTML 那样收紧 Markdown 的运动。
picomancer查看英文原文
> HTML is built around intentionally ignoring malformed input To elaborate: In the very early days, HTML was often written by hand. Part of the early vision of the web was that anybody could write HTML and publish content. It was thought that allowing malformed HTML to result in a completely blank page or an error message would be too unfriendly for less technical users. In those early pre-CSS pre-JavaScript days, HTML was more like bbcode, Markdown, or RST today. It's interesting that AFAIK those languages don't have a notion of malformed input -- there is no movement to tighten the bolts on Markdown in the way that XHTML attempted to tighten the bolts on HTML.
直言答案简单:非十六进制字符被替换为 0,没有深层彩蛋。
答案 unfortunately 有点无聊 :'( 我原本希望有个很酷的故事或深层彩蛋……但并没有。剧透:只是不存在的十六进制字符被替换成了 0 :P
GekkePrutser查看英文原文
The answer is unfortunately a bit boring :'( I was hoping for some cool story with some deep easter egg.. But no. <spoiler>It's just that non-existing hex chars are replaced by 0's :P</spoiler>
提出性能问题:不同颜色表示法的解析开销差异,以及是否存在最耗时的字符串。
所以现在我想知道——使用关键字 red 比使用十六进制值慢多少,使用 chucknorris 又比 red 慢多少?是否存在某种意外的字符串(或字符串们)能产生一种颜色,且浏览器需要最多步骤来生成,从而(我假设)消耗更多计算能力?
bryanrasmussen查看英文原文
So now it makes me wonder - how much slower is it to use the keyword red than to use a hex value, and how much slower is it to use chucknorris than red. Is there an unexpected text string (or strings) that will produce a color that takes the largest amount of steps for the browser to produce out of the string, thus, I assume, using more computing power.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选