HTML 缩小:元素属性之间的空白

HTML Minification: Whitespace between element attributes

我想从我的输出中删除更多不必要的字节,似乎可以接受(实际上)去除什么可以通过 omitting/collapsing DOM 元素属性之间的间隙从 HTML 标记中加起来相当多的白色 space。

虽然我已经测试和研究过(在这两种情况下都有一点),但我想知道安全如何?

我在 Chrome (43.0.2357.65 m), IE (11.0.9600.17801), FF (38.0.1)Safari (5.1.7 (blah-di-blah)) 他们似乎并不介意,并且在 [=17= 中找不到任何具体内容] 约白space属性之间

w3.org's Validator 抱怨,这强烈表明这不 安全 并且不应该期望工作,但是(总是有一个 "but") requirement for a space 可能仅在不存在引号时才严格(出于显而易见的原因)。
另外(尖锐但尖锐):他们的 SSL 是 "out of date",这并不能激发人们对他们的 观点 的信心。

我还注意到 someone's HTML compressor 可以(启用时)去除属性值周围的引号,其中这些值中没有白色 space(例如 id),这意味着至少大部分(如果不是全部)HTML 解析都集中在等号两边的文本(当然布尔值除外),并且在使用引号的地方,他们会被视为优先定界符。

所以,会:

<!DOCTYPE html><html><body>
<a href="http://example.org"target="_blank"title="This is a test">Yabba Dabba Doo!</a>
</body></html>

▲ 曾经出错,如果出错,是在什么条件下?

还有什么其他原因可以在生产输出中保持这种白色space(代码"readability"在这种情况下不是问题)?

更新(自找到一个答案后):

虽然我基本上回答了我自己的问题,因为有一个规范来管理属性之间是否应该一个space,但我仍然想知道在使用时是否省略它们引用的值实际上可以被认为是 安全的 ,并且希望得到关于这一点的反馈。

考虑到 spaces 在生产中可能被意外遗漏的频率 HTML,而且我测试的浏览器似乎并不介意它们何时遗漏,我认为这种情况非常罕见如果浏览器无法处理省略这些 space 的文档。

虽然在几乎所有情况下都遵循规范是明智的,但这一次有点作弊可能可以接受吗?

毕竟 - 如果我们可以 神奇地 保存几百个字节而不影响输出质量,为什么不呢?

有规范(毕竟)

事实证明我应该更仔细地看一下。我的错。

根据these specs

If an attribute using the empty attribute syntax is to be followed by another attribute, then there must be a space character separating the two.

If an attribute using the unquoted attribute syntax is to be followed by another attribute or by the optional U+002F SOLIDUS character (/) allowed in step 6 of the start tag syntax above, then there must be a space character separating the two.

If an attribute using the single-quoted attribute syntax is to be followed by another attribute, then there must be a space character separating the two.

If an attribute using the double-quoted attribute syntax is to be followed by another attribute, then there must be a space character separating the two.

除非我(再次)弄错了,否则意味着属性之间必须总是有空格。

您可以在线尝试 HTML 缩小器,例如 http://www.whak.ca/minify/HTML.htm or http://www.scriptcompress.com/minify-HTML.htm(搜索 google 了解更多信息)并发现它们会更改一些小东西以提示可以删除哪些内容,但仍会呈现 HTML代码。

第一个 link 您的代码:

<!DOCTYPE html><html><body> <a href="http://example.org"target="_blank"title="This is a test">Yabba Dabba Doo!</a> </body></html>

变成:

<!DOCTYPE html><html><body><a href=http://example.org target=_blank title="This is a test">Yabba Dabba Doo!</a>

已经为您节省了 18 个字节...