skipHtmlSubtree function
Returns the index of the token closing the subtree rooted at the open tag
openIndex within tokens (depth-counted on the same tag name). Returns
openIndex itself for self-closing tags, or the last token index when the
close tag is missing (malformed input).
Implementation
int skipHtmlSubtree(List<Object> tokens, int openIndex) {
final open = tokens[openIndex] as HtmlTag;
if (open.selfClosing) return openIndex;
var depth = 1;
var i = openIndex + 1;
while (i < tokens.length && depth > 0) {
final token = tokens[i];
if (token is HtmlTag && token.name == open.name) {
if (token.closing) {
depth--;
} else if (!token.selfClosing) {
depth++;
}
}
i++;
}
return i - 1;
}