问题
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.
回答1:
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 :)
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/3343067/how-many-nested-tags-in-html-can-you-have-before-it-crashes-a-browser