HTML 缩小 : Whitespace between element attributes

标签 html cross-browser whitespace minify specifications

我想从我的输出中删除更多不必要的字节,似乎可以接受(在实践中)去除可能加起来相当多的字节通过省略/折叠 DOM 元素属性之间的间隙,从 HTML 标记中去除空白。

尽管我已经进行了测试和研究(在这两种情况下都有一点),但我想知道它的安全如何?

我在Chrome (43.0.2357.65 m)IE (11.0.9600.17801)、< strong>FF (38.0.1)Safari (5.1.7 (blah-di-blah)) 他们没有似乎不介意,在 The Specs 中找不到任何具体内容关于属性之间的空格。

w3.org's Validator提示,这是一个强烈的迹象表明这不是安全并且不应期望有效,但是(总是有一个“但是”)它可能是要求仅当不存在引号时(出于显而易见的原因),空格才是严格的。
另外(尖锐但尖锐):他们的 SSL “过时” 这不会激发人们对他们的意见的信心。

我还注意到 someone's HTML compressor可以(启用时)去除属性值周围没有空格的引号(例如 id),这意味着即使不是全部,至少也是大多数 HTML 解析是集中的在等号两侧的文本(当然除了 bool 值),以及使用引号的地方,它们将被视为优先定界符。

那么,会:

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

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

还有什么其他原因可以在生产输出中保留此空白(代码“可读性”在这种情况下不是问题)?

更新(因为找到一个答案):

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

考虑到生产 HTML 中空格可能被意外省略的频率,并且我测试的浏览器似乎并不介意它们何时被忽略,我认为如果浏览器无法处理包含这些的文档,那将是非常罕见的省略空格。

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

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

最佳答案

有规范(毕竟)

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

根据 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 缩小 : Whitespace between element attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30420684/

相关文章:

HTML 5 输入类型电子邮件检查验证丢失的焦点

html - Bootstrap3 表单不会在移动设备上保持布局

html - CSS float 和 block 元素

php include html 添加空格

javascript - 设置 doctype 破坏了我的 css 和 javascript

html - 如何触发点击组件中的 HTML 文件元素? ( Angular 2)

angular - Typescript + Angular 2 : How can I ensure emitted decorator metadata will refer to variables in scope, 以避免 "not defined"错误?

jquery - 是否可以使用 jQuery.Support 检查 PNG 支持?

python - 为什么 True 在 numpy 数组中打印前导空格

html - CSS X 溢出留下空白