Skip to content

Commit

Permalink
Call onNodeClosed when closing header tags
Browse files Browse the repository at this point in the history
Ensures that the end source range for headers is tracked correctly.

Fixes #1987
  • Loading branch information
jhy committed Sep 8, 2023
1 parent 04b3a82 commit 0eb8232
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Release 1.16.2 [PENDING]
thread after interrupting it.
<https://github.com/jhy/jsoup/issues/1991>

* Bugfix: when tracking HTML source positions, the closing tags for H1...H6 elements were not tracked correctly.
<https://github.com/jhy/jsoup/issues/1987>

* Change: removed previously deprecated methods Document#normalise, Element#forEach(org.jsoup.helper.Consumer<>),
Node#forEach(org.jsoup.helper.Consumer<>), and the org.jsoup.helper.Consumer interface; the latter being a
previously required compatibility shim prior to Android's de-sugaring support.
Expand Down
16 changes: 4 additions & 12 deletions src/main/java/org/jsoup/parser/HtmlTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,20 +424,12 @@ Element popStackToClose(String elName) {
// elnames is sorted, comes from Constants
void popStackToClose(String... elNames) {
for (int pos = stack.size() -1; pos >= 0; pos--) {
Element next = stack.get(pos);
Element el = stack.get(pos);
stack.remove(pos);
if (inSorted(next.normalName(), elNames))
break;
}
}

void popStackToBefore(String elName) {
for (int pos = stack.size() -1; pos >= 0; pos--) {
Element next = stack.get(pos);
if (next.normalName().equals(elName)) {
if (inSorted(el.normalName(), elNames)) {
if (currentToken instanceof Token.EndTag)
onNodeClosed(el, currentToken);
break;
} else {
stack.remove(pos);
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/jsoup/nodes/PositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,20 @@ class PositionTest {
}
}

@Test void tracksClosingHeadingTags() {
// https://github.com/jhy/jsoup/issues/1987
String html = "<h1>One</h1><h2>Two</h2><h10>Ten</h10>";
Document doc = Jsoup.parse(html, TrackingParser);

Elements els = doc.body().children();
for (Element el : els) {
assertTrue(el.sourceRange().isTracked());
assertTrue(el.endSourceRange().isTracked());
}

Element h2 = doc.expectFirst("h2");
assertEquals("1,13:12-1,17:16", h2.sourceRange().toString());
assertEquals("1,20:19-1,25:24", h2.endSourceRange().toString());
}

}

0 comments on commit 0eb8232

Please sign in to comment.