在 HTML 中可以有多少个嵌套标签才会导致浏览器崩溃?
今天早些时候我有一个关于嵌套 HTML 标签以及浏览器如何渲染它们的想法:
<html xmlns="http://www.w3.org/1999/xhtml" {or whichever html version} xml:lang="en" lang="en">
<head>
</head>
<body>
let n
= 1
<div>
递归 div n
次,直到最大(浏览器失败)
</div>
</body>
</html>
n
当浏览器无法处理更多递归时?
我认为这对于每个浏览器来说都是不同的,对于移动应用程序来说也是不同的。是否有网络标准,例如域名的最大长度为 127 个字符?
我从来没有遇到过这个问题,但我很好奇什么时候会遇到。
I had a thought earlier today regarding nested HTML tags and how browsers render them:
<html xmlns="http://www.w3.org/1999/xhtml" {or whichever html version} xml:lang="en" lang="en">
<head>
</head>
<body>
let n
= 1
<div>
recurse div n
times until maximum (browser fails)
</div>
</body>
</html>
what will n
be when the browser cannot handle any more recursion?
I would think this would be different for each browser, and different also for mobile apps. Is there a web standard, such as the maximum 127 character length for domain names?
I have never run into this problem, but I am curious when it would.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有要求最大嵌套的标准,因此这完全是特定于实现的。
有可能在崩溃之前,浏览器会变得无法使用(速度变慢等)。
如果您很好奇,您可以对此进行基准测试 - 编写一个生成嵌套标签的应用程序,并查看每个浏览器何时崩溃:)
There is no standard requiring a maximum nesting, so this will be entirely implementation specific.
Chances are that before crashing, the browser would become unusable (slow downs etc).
If you are very curious, you can benchmark this - code an application that generates nested tags and see when each browser crashes on you :)
你担心太多了。或者你正在计划一个太复杂的布局。即便如此,如果不是特意创建的 HTML,您也不太可能达到这样的限制。
如果浏览器的 HTML 解析器是递归的,那么在输入深度嵌套的标签时,它可能会因为堆栈溢出而崩溃。但在现代系统/操作系统上,堆栈默认情况下足够大,可以支持一百级或更多级别的递归,具体取决于堆栈分配变量的大小。
如果解析器不是递归的,那么当给定一个极其复杂(难以置信的大和令人难以置信的深度嵌套)文档时,我的下一个赌注将是OutOfMemoryError。
You worry too much. Or you're planning a waaay too complicated layout. And even then, it's very unlikely you will every reach such a limit with HTML not deliberately created to do so.
If the browser's HTML parser is recursive, it might crash when fed deeply-nested tags simply because the stack overflows. But on modern systems/OSs, the stack is by default large enough to support a hundred or more of levels of recursion, depending on the size of stack-allocated variables.
If the parser isn't recursive, my next bet would be an OutOfMemoryError when given an extremely complex (incredibly large and incredibly deeply nested) document.
Firefox 似乎(似乎?)在这方面存在一些问题。
请参阅 https://groups.google.com/forum/# !topic/mozilla.dev.platform/SUknMzK1ZAc
特别是,Firefox 将深度限制为 200 个元素。
There seems (seemed?) to be some issues with Firefox regarding this.
See https://groups.google.com/forum/#!topic/mozilla.dev.platform/SUknMzK1ZAc
Particulary, Firefox limit(ed?) the deep to 200 elements.