Skip to content

Commit

Permalink
In adoption agency, make sure there's sufficient elements on stack
Browse files Browse the repository at this point in the history
Fixes #1602
  • Loading branch information
jhy committed Aug 4, 2021
1 parent dd2536b commit d6a4d20
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -23,6 +23,9 @@ jsoup changelog
* Bugfix: fixed an NPE when parsing fragment heading HTML into a standalone p element.
<https://github.com/jhy/jsoup/issues/1601>

* Bugfix: fixed an IOOB when parsing a formatting fragment into a standalone p element.
<https://github.com/jhy/jsoup/issues/1602>

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

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jsoup/parser/HtmlTreeBuilderState.java
Expand Up @@ -816,7 +816,8 @@ else if (!tb.onStack(formatEl)) {
// run-aways
final int stackSize = stack.size();
int bookmark = -1;
for (int si = 0; si < stackSize && si < 64; si++) {
for (int si = 1; si < stackSize && si < 64; si++) {
// TODO: this no longer matches the current spec at https://html.spec.whatwg.org/#adoption-agency-algorithm and should be updated
el = stack.get(si);
if (el == formatEl) {
commonAncestor = stack.get(si - 1);
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/jsoup/parser/HtmlParserTest.java
Expand Up @@ -1433,11 +1433,21 @@ private boolean didAddElements(String input) {
}

@Test public void parseFragmentOnCreatedDocument() {
// https://github.com/jhy/jsoup/issues/1601
String bareFragment = "<h2>text</h2>";
List<Node> nodes = new Document("").parser().parseFragmentInput(bareFragment, new Element("p"), "");
assertEquals(1, nodes.size());
Node node = nodes.get(0);
assertEquals("h2", node.nodeName());
assertEquals("<p><h2>text</h2></p>", node.parent().outerHtml());
}

@Test public void nestedPFragments() {
// https://github.com/jhy/jsoup/issues/1602
String bareFragment = "<p></p><a></a>";
List<Node> nodes = new Document("").parser().parseFragmentInput(bareFragment, new Element("p"), "");
assertEquals(2, nodes.size());
Node node = nodes.get(0);
assertEquals("<p><p></p><a></a></p>", node.parent().outerHtml()); // mis-nested because fragment forced into the element, OK
}
}

0 comments on commit d6a4d20

Please sign in to comment.