Documentation
¶
Overview ¶
Command depth enforces a maximum element nesting depth and reports the deepest path it found.
A depth budget is a tree question, and this library does not build a tree. It reports tokens: start tags, end tags, text. So the program keeps the stack of open elements itself, and the whole difficulty is that a stack driven by tokens is not the same as a stack driven by elements.
HTML lets a document leave end tags out. In <ul><li>a<li>b</ul> the first item is closed by the second item's start tag, and there is no token that says so. A counter that only decrements on end tags reports that list as three deep where a browser has two, and the error accumulates: a page of forty implicit list items reports a depth of forty-one.
So the stack pops on a start tag as well, following the specification's implied end tags. That is the part of a parser this program has to be, and it is written out in impliedlyClosedBy below rather than hidden, because the list is the program.
End tags are the other half, and they arrive here through a detour. There is no top-level end-tag handler; the only way to see one is Element.OnEndTag, which fires against the tag that closed the element - so for <ul><li>a<li>b</ul> three handlers fire at the single </ul> token. The program registers one on every element and then deduplicates by source location, because what it wants is the token, and the token is what the source location identifies.