views:

59

answers:

2

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.

+3  A: 

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 :)

Oded
I may try just that. But I wanted to see if anyone knew this already, or had run across it first.
Talvi Watia
A: 

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.

delnan
not worried. the idea was hypothetical.
Talvi Watia
@delnan - in my experience it used to be large, complex deeply nested tables that caused problems. Mostly to do with the layout engine, not the parser.
Oded
@Oded: Didn't think of it, but now you mention it, is sounds more likely (at least, more likely than blowing the parser up)
delnan
@Oded large tables causing issues make sense, but does it only affect layout, or does the parse fail also?
Talvi Watia
@Talvi Watia - Most HTML parsers are built as tokenizers, so they tend to not get affected as much by size. It's the layout engine that suffers...
Oded