有一个长排列的HTML源代码是不好的 [英] Is it bad to have HTML source code in one long line

查看:92
本文介绍了有一个长排列的HTML源代码是不好的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的PHP CMS应用程序中,我将大部分生成的HTML捕获到缓冲区中,然后在模板页面的正确位置,他们的内容。这是通过我自己的缓冲区类来完成的,我最近添加了一个方法来(使用 regex 来缩小生成的HTML,将 2个或更多个空格替换为)一个空间

这样做的结果是,最终的HTML根本没有新行。这在浏览器中工作正常,并且显示正常,没有任何问题。



我对任何其他的HTML缩小或其他PHP类都做同样的事情不感兴趣事情。我有一个简单的双线程,可以完成我所需要的任何工作,并且生成的HTML比以前小得多。



我感兴趣的是:有没有有一个.html文件,其内容根本没有新行,并且所有源代码都放在一行很长的行中?



除了人类的可读性,当然:)我对某种技术(或者甚至是其他)的原因感兴趣,这会使得这是一个糟糕的做法。

解决方案

某些代理或浏览器可能在单行上有非常大的HTML文件时出现问题。

对于JavaScript来说,缩小代码的工具通常会按特定的时间间隔添加换行符。


Closure Compiler每500个字符左右有意添加换行符。防火墙和代理有时会破坏或忽略长行很长的JavaScript文件。每500个字符添加换行符可防止出现此问题。删除换行符对脚本的语义没有影响。对代码大小的影响很小,编译器优化了换行符的放置位置,以便在对文件进行gzip压缩时,代码大小的损失更小。




< UglifyJS也是这样做的:

lockquote

- max-line-len(默认32K字符) - 大约32K字符后添加一个换行符。当所有代码都在大约670K的单行上时,我已经看到FF和Chrome都在呱呱叫。通过-max-line-len 0来禁用这个安全功能。



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.

解决方案

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.

这篇关于有一个长排列的HTML源代码是不好的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆