Skip to content

Commit

Permalink
Update InRow state to current spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jhy committed Oct 18, 2023
1 parent 90f24b4 commit d61ec5a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/jsoup/parser/HtmlTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ void error(HtmlTreeBuilderState state) {
currentToken.tokenType(), currentToken, state));
}

/** Inserts an HTML element for the given tag) */
Element insert(final Token.StartTag startTag) {
dedupeAttributes(startTag);

Expand Down Expand Up @@ -714,7 +715,10 @@ private boolean inSpecificScope(String[] targetNames, String[] baseTypes, @Nulla
// don't walk too far up the tree

for (int pos = bottom; pos >= top; pos--) {
final String elName = stack.get(pos).normalName();
Element el = stack.get(pos);
if (!el.tag().namespace().equals(NamespaceHtml)) continue;

final String elName = el.normalName();
if (inSorted(elName, targetNames))
return true;
if (inSorted(elName, baseTypes))
Expand Down
39 changes: 25 additions & 14 deletions src/main/java/org/jsoup/parser/HtmlTreeBuilderState.java
Original file line number Diff line number Diff line change
Expand Up @@ -1299,13 +1299,20 @@ boolean process(Token t, HtmlTreeBuilder tb) {
Token.StartTag startTag = t.asStartTag();
String name = startTag.normalName();

if (inSorted(name, InCellNames)) {
if (inSorted(name, InCellNames)) { // th, th
tb.clearStackToTableRowContext();
tb.insert(startTag);
tb.transition(InCell);
tb.insertMarkerToFormattingElements();
} else if (inSorted(name, InRowMissing)) {
return handleMissingTr(t, tb);
} else if (inSorted(name, InRowMissing)) { // "caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr"
if (!tb.inTableScope("tr")) {
tb.error(this);
return false;
}
tb.clearStackToTableRowContext();
tb.pop(); // tr
tb.transition(InTableBody);
return tb.process(t);
} else {
return anythingElse(t, tb);
}
Expand All @@ -1322,15 +1329,27 @@ boolean process(Token t, HtmlTreeBuilder tb) {
tb.pop(); // tr
tb.transition(InTableBody);
} else if (name.equals("table")) {
return handleMissingTr(t, tb);
} else if (inSorted(name, InTableToBody)) {
if (!tb.inTableScope(name) || !tb.inTableScope("tr")) {
if (!tb.inTableScope("tr")) {
tb.error(this);
return false;
}
tb.clearStackToTableRowContext();
tb.pop(); // tr
tb.transition(InTableBody);
return tb.process(t);
} else if (inSorted(name, InTableToBody)) { // "tbody", "tfoot", "thead"
if (!tb.inTableScope(name)) {
tb.error(this);
return false;
}
if (!tb.inTableScope("tr")) {
// not an error per spec?
return false;
}
tb.clearStackToTableRowContext();
tb.pop(); // tr
tb.transition(InTableBody);
return tb.process(t);
} else if (inSorted(name, InRowIgnore)) {
tb.error(this);
return false;
Expand All @@ -1346,14 +1365,6 @@ boolean process(Token t, HtmlTreeBuilder tb) {
private boolean anythingElse(Token t, HtmlTreeBuilder tb) {
return tb.process(t, InTable);
}

private boolean handleMissingTr(Token t, TreeBuilder tb) {
boolean processed = tb.processEndTag("tr");
if (processed)
return tb.process(t);
else
return false;
}
},
InCell {
boolean process(Token t, HtmlTreeBuilder tb) {
Expand Down
Binary file added src/test/resources/fuzztests/63242.html.gz
Binary file not shown.

0 comments on commit d61ec5a

Please sign in to comment.