Is it bad to have HTML source code in one long line

邮差的信 提交于 2019-12-19 10:33:24

问题


I was just wondering.

In my PHP CMS application I catch most of generated HTML into buffers and then, at the right spot in the template page, I flush their contents. This is done by my own buffer class, where I recently added a method to (sorta) minify the resulting HTML using regex that replaces 2 or more whitespaces into just one space.

The result of this is the fact, that the final HTML has no new lines at all. That works fine in the browser and it's displayed fine, no problem whatsoever.

I'm not interested in any other method of HTML minifying or other PHP classes that do the same thing. I have a simple two liner that does everything I need and the resulting HTML is much smaller than before.

What I am interested in is this: Are there any downsides of having an .html file with content that has no new lines at all and has all the source code in one very long line?

Apart from the human readibility, of course :) I'm interested in somewhat technical (or maybe even other) reasons, that would render this a bad practice.


回答1:


Some proxies or browsers might have problems with a very large HTML file on a single line.

For JavaScript for example, tools that minify the code usually add line breaks at specific intervals.

The Closure Compiler intentionally adds line breaks every 500 characters or so. Firewalls and proxies sometimes corrupt or ignore large JavaScript files with very long lines. Adding line breaks every 500 characters prevents this problem. Removing the line breaks has no effect on a script's semantics. The impact on code size is small, and the Compiler optimizes line break placement so that the code size penalty is even smaller when files are gzipped.

UglifyJS does the same:

--max-line-len (default 32K characters) — add a newline after around 32K characters. I’ve seen both FF and Chrome croak when all the code was on a single line of around 670K. Pass –max-line-len 0 to disable this safety feature.



来源:https://stackoverflow.com/questions/18509853/is-it-bad-to-have-html-source-code-in-one-long-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!