// tok must come from htmlcoarsetok
// returns all container tags found (including content) as CNC
// honors nested tags correctly
static LL findContainerOrEmptyTag(L tok, S tag) {
new LL l;
for (int i = 1; i < l(tok); i += 2)
if (isEmptyTag(tok.get(i), tag))
l.add(tok.subList(i-1, i+1));
else if (isOpeningTag(tok.get(i), tag)) {
int j, level = 1;
for (j = i+2; j < tok.size(); j += 2)
if (isOpeningTag(tok.get(j), tag))
++level;
else if (isTag(tok.get(j), "/" + tag)) {
--level;
if (level == 0) {
l.add(subList(tok, i-1, j+2)); // actual CNC
break;
}
}
i = j;
}
ret l;
}
static LL findContainerOrEmptyTag(S html, S tag) {
ret findContainerOrEmptyTag(htmlTok(html), tag);
}