Skip to content

Commit b875b92

Browse files
committed
Updated in specific scope tag searches
Includes template and SVG / MathML tags. This is not currently specifically namespace aware. In practice I think that's OK, but we can update if required. Fixes #2271
1 parent 59ec905 commit b875b92

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
* A `template` tag containing an `li` within an open `li` would be parsed incorrectly, as it was not recognized as a
5757
"special" tag (which have additional processing rules). Also, added the SVG and MathML namespace tags to the list of
5858
special tags. [2258](https://github.com/jhy/jsoup/issues/2258)
59+
* A `template` tag containing a `button` within an open `button` would be parsed incorrectly, as the "in button scope"
60+
check was not aware of the `template` element. Corrected other instances including MathML and SVG elements,
61+
also. [2271](https://github.com/jhy/jsoup/issues/2271)
5962
* An `:nth-child` selector with a negative digit-less step, such as `:nth-child(-n+2)`, would be parsed incorrectly as a
6063
positive step, and so would not match as expected. [1147](https://github.com/jhy/jsoup/issues/1147)
6164
* Calling `doc.charset(charset)` on an empty XML document would throw an

src/main/java/org/jsoup/parser/HtmlTreeBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
import static org.jsoup.internal.StringUtil.inSorted;
2222
import static org.jsoup.parser.HtmlTreeBuilderState.Constants.InTableFoster;
2323
import static org.jsoup.parser.HtmlTreeBuilderState.ForeignContent;
24-
import static org.jsoup.parser.Parser.NamespaceHtml;
24+
import static org.jsoup.parser.Parser.*;
2525

2626
/**
2727
* HTML Tree Builder; creates a DOM from Tokens.
2828
*/
2929
public class HtmlTreeBuilder extends TreeBuilder {
3030
// tag searches. must be sorted, used in inSorted. HtmlTreeBuilderTest validates they're sorted.
31-
static final String[] TagsSearchInScope = new String[]{"applet", "caption", "html", "marquee", "object", "table", "td", "th"};
31+
// todo - tag search in scope might need to be properly namespace aware - https://html.spec.whatwg.org/#has-an-element-in-scope
32+
static final String[] TagsSearchInScope = new String[]{
33+
"annotation-xml", "applet", "caption", "desc", "foreignObject", "html", "marquee", "mi", "mn", "mo", "ms", "mtext", "object", "table", "td", "template", "th", "title" // <- svg title
34+
};
3235
static final String[] TagSearchList = new String[]{"ol", "ul"};
3336
static final String[] TagSearchButton = new String[]{"button"};
3437
static final String[] TagSearchTableScope = new String[]{"html", "table"};
@@ -669,11 +672,8 @@ private boolean inSpecificScope(String[] targetNames, String[] baseTypes, @Nulla
669672
final int bottom = stack.size() -1;
670673
final int top = bottom > MaxScopeSearchDepth ? bottom - MaxScopeSearchDepth : 0;
671674
// don't walk too far up the tree
672-
673675
for (int pos = bottom; pos >= top; pos--) {
674676
Element el = stack.get(pos);
675-
if (!el.tag().namespace().equals(NamespaceHtml)) continue;
676-
677677
final String elName = el.normalName();
678678
if (inSorted(elName, targetNames))
679679
return true;

src/test/java/org/jsoup/parser/HtmlParserTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,13 @@ private boolean didAddElements(String input) {
16611661
TextUtil.stripNewlines(doc.body().html()));
16621662
}
16631663

1664+
@Test void templateInButton() {
1665+
// https://github.com/jhy/jsoup/issues/2271
1666+
String html = "<button><template><button></button></template></button>";
1667+
Document doc = Jsoup.parse(html);
1668+
assertEquals(html, TextUtil.stripNewlines(doc.body().html()));
1669+
}
1670+
16641671
@Test void errorsBeforeHtml() {
16651672
Parser parser = Parser.htmlParser();
16661673
parser.setTrackErrors(10);

0 commit comments

Comments
 (0)