Fix a bug in StrictHtmlValidationPass.
Before trying to find common prefix, we still need to pop optional tags in conditional branches if necessary.
@@ -315,6 +315,35 @@ final class HtmlTagEntry {
}
}
+ /**
+ * Pops all optional tags in the openStack that can be popped by the current close tag. Returns
+ * the top of the stack after popping.
+ */
+ private static HtmlTagEntry popOptionalTagInStack(
+ ArrayDeque<HtmlTagEntry> openStack, TagName closeTag) {
+ HtmlTagEntry openTag = openStack.peekFirst();
+ while (openTag != null) {
+ if (openTag.hasTagName()
+ && !openTag.getTagName().equals(closeTag)
+ && TagName.checkOptionalTagShouldBePopped(openTag.getTagName(), closeTag)) {
+ openStack.pollFirst();
+ openTag = openStack.peekFirst();
+ continue;
+ } else if (!openTag.hasTagName()) {
+ // For Conditional branches, we also need to pop optional tags.
+ openTag.getBranches().popOptionalTags(closeTag);
+ if (openTag.getBranches().isEmpty()) {
+ openStack.pollFirst();
+ openTag = openStack.peekFirst();
+ continue;
+ }
+ }
+ // At this point we should break.
+ break;
+ }
+ return openTag;
+ }
+
/**
* Try to match a close tag with a stack of open tags, and report errors accordingly.
*
@@ -327,14 +356,7 @@ final class HtmlTagEntry {
static boolean tryMatchCloseTag(
ArrayDeque<HtmlTagEntry> openStack, TagName closeTag, ErrorReporter errorReporter) {
// Pop out every optional tags that does not match the current close tag.
- HtmlTagEntry openTag = openStack.peekFirst();
- while (openTag != null
- && openTag.hasTagName()
- && !openTag.getTagName().equals(closeTag)
- && TagName.checkOptionalTagShouldBePopped(openTag.getTagName(), closeTag)) {
- openStack.pollFirst();
- openTag = openStack.peekFirst();
- }
+ HtmlTagEntry openTag = popOptionalTagInStack(openStack, closeTag);
if (openTag == null) {
return false;
} else if (openTag.hasTagName()) {
