Skip to content

Commit

Permalink
Updated resetInsertionMode
Browse files Browse the repository at this point in the history
Fixes #1491

Need to add support for template tags
  • Loading branch information
jhy committed Jul 11, 2021
1 parent 049204c commit 2676f41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -5,6 +5,9 @@ jsoup changelog

* Bugfix: when making a HTTP POST, if the request write fails, make sure the connection is immediately cleaned up.

* Bugfix: updated the HtmlTreeParser resetInsertionMode to the current spec for supported elements
<https://github.com/jhy/jsoup/issues/1491>

* Bugfix [Fuzz]: fixed a slow parse when a tag has thousands of null characters in it.
<https://github.com/jhy/jsoup/issues/1580>

Expand Down
24 changes: 14 additions & 10 deletions src/main/java/org/jsoup/parser/HtmlTreeBuilder.java
Expand Up @@ -438,17 +438,20 @@ private void replaceInQueue(ArrayList<Element> queue, Element out, Element in) {
}

void resetInsertionMode() {
// https://html.spec.whatwg.org/multipage/parsing.html#the-insertion-mode
boolean last = false;
for (int pos = stack.size() -1; pos >= 0; pos--) {
Element node = stack.get(pos);
if (pos == 0) {
last = true;
node = contextElement;
if (fragmentParsing)
node = contextElement;
}
String name = node != null ? node.normalName() : "";
if ("select".equals(name)) {
transition(HtmlTreeBuilderState.InSelect);
break; // frag
// todo - should loop up (with some limit) and check for table or template hits
break;
} else if (("td".equals(name) || "th".equals(name) && !last)) {
transition(HtmlTreeBuilderState.InCell);
break;
Expand All @@ -463,25 +466,26 @@ void resetInsertionMode() {
break;
} else if ("colgroup".equals(name)) {
transition(HtmlTreeBuilderState.InColumnGroup);
break; // frag
break;
} else if ("table".equals(name)) {
transition(HtmlTreeBuilderState.InTable);
break;
} else if ("head".equals(name)) {
transition(HtmlTreeBuilderState.InBody);
break; // frag
// todo - template
} else if ("head".equals(name) && !last) {
transition(HtmlTreeBuilderState.InHead);
break;
} else if ("body".equals(name)) {
transition(HtmlTreeBuilderState.InBody);
break;
} else if ("frameset".equals(name)) {
transition(HtmlTreeBuilderState.InFrameset);
break; // frag
break;
} else if ("html".equals(name)) {
transition(HtmlTreeBuilderState.BeforeHead);
break; // frag
transition(headElement == null ? HtmlTreeBuilderState.BeforeHead : HtmlTreeBuilderState.AfterHead);
break;
} else if (last) {
transition(HtmlTreeBuilderState.InBody);
break; // frag
break;
}
}
}
Expand Down

0 comments on commit 2676f41

Please sign in to comment.