Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/ch/digitalfondue/jfiveparse/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,6 @@ public enum Option {
* When encountering unknown self-closing tag, will interpret as self-closing tag instead of ignoring.
*/
INTERPRET_SELF_CLOSING_ANYTHING_ELSE,

DISABLE_IN_TABLE_TEXT_FOSTER_PARENTING
}
12 changes: 10 additions & 2 deletions src/main/java/ch/digitalfondue/jfiveparse/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Parser {
private final boolean transformEntities;
private final boolean disableIgnoreTokenInBodyStartTag;
private final boolean interpretSelfClosingAnythingElse;
private final boolean disableInTableTextForsterParenting;

/**
* Instantiate a parser with the default configuration.
Expand All @@ -42,6 +43,7 @@ public Parser() {
transformEntities = true;
disableIgnoreTokenInBodyStartTag = false;
interpretSelfClosingAnythingElse = false;
disableInTableTextForsterParenting = false;
}

/**
Expand All @@ -52,6 +54,7 @@ public Parser() {
* <li>{@link Option#DONT_TRANSFORM_ENTITIES}</li>
* <li>{@link Option#DISABLE_IGNORE_TOKEN_IN_BODY_START_TAG}</li>
* <li>{@link Option#INTERPRET_SELF_CLOSING_ANYTHING_ELSE}</li>
* <li>{@link Option#DISABLE_IN_TABLE_TEXT_FOSTER_PARENTING}</li>
* </ul>
*
* @param options
Expand All @@ -61,6 +64,7 @@ public Parser(Set<Option> options) {
this.transformEntities = !options.contains(Option.DONT_TRANSFORM_ENTITIES);
this.disableIgnoreTokenInBodyStartTag = options.contains(Option.DISABLE_IGNORE_TOKEN_IN_BODY_START_TAG);
this.interpretSelfClosingAnythingElse = options.contains(Option.INTERPRET_SELF_CLOSING_ANYTHING_ELSE);
this.disableInTableTextForsterParenting = options.contains(Option.DISABLE_IN_TABLE_TEXT_FOSTER_PARENTING);
}

/**
Expand Down Expand Up @@ -115,7 +119,7 @@ private List<Node> parseFragment(ProcessedInputStream is, Element node) {

// 1 when creating a tree constructor, a document is automatically
// created (good idea? y/n?)
TreeConstructor tokenHandler = new TreeConstructor(disableIgnoreTokenInBodyStartTag, interpretSelfClosingAnythingElse);
TreeConstructor tokenHandler = new TreeConstructor(disableIgnoreTokenInBodyStartTag, interpretSelfClosingAnythingElse , disableInTableTextForsterParenting);
//
tokenHandler.isHtmlFragmentParsing = true;
tokenHandler.scriptingFlag = scriptingFlag;
Expand Down Expand Up @@ -187,7 +191,11 @@ private static Element getFirstFormElementFrom(Node node) {
}

private Document parse(ProcessedInputStream is) {
TreeConstructor tokenHandler = new TreeConstructor(disableIgnoreTokenInBodyStartTag, interpretSelfClosingAnythingElse);
TreeConstructor tokenHandler = new TreeConstructor(
disableIgnoreTokenInBodyStartTag,
interpretSelfClosingAnythingElse,
disableInTableTextForsterParenting
);
tokenHandler.scriptingFlag = scriptingFlag;
Tokenizer tokenizer = new Tokenizer(tokenHandler, transformEntities);
tokenHandler.setTokenizer(tokenizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class TreeConstructor {

final boolean disableIgnoreTokenInBodyStartTag;
final boolean interpretSelfClosingAnythingElse;
final boolean disableInTableTextForsterParenting;

// ----
private int tokenType;
Expand Down Expand Up @@ -107,9 +108,10 @@ void setTokenizer(Tokenizer tokenizer) {
this.tokenizer = tokenizer;
}

TreeConstructor(boolean disableIgnoreTokenInBodyStartTag, boolean interpretSelfClosingAnythingElse) {
TreeConstructor(boolean disableIgnoreTokenInBodyStartTag, boolean interpretSelfClosingAnythingElse, boolean disableInTableTextForsterParenting) {
this.disableIgnoreTokenInBodyStartTag = disableIgnoreTokenInBodyStartTag;
this.interpretSelfClosingAnythingElse = interpretSelfClosingAnythingElse;
this.disableInTableTextForsterParenting = disableInTableTextForsterParenting;
}

private void setTagNameAndSaveOriginal(ResizableCharBuilder rawTagName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ static void inTableText(int tokenType, String tagName, int tagNameID, TreeConstr
treeConstructor.appendToPendingTableCharactersToken(chr);
} else {
ResizableCharBuilder chars = treeConstructor.getPendingTableCharactersToken();
if (!isAllSpaceCharacters(chars)) {
if (!isAllSpaceCharacters(chars) && !treeConstructor.disableInTableTextForsterParenting) {
// TODO CHECK

treeConstructor.emitParseError();
Expand Down
25 changes: 22 additions & 3 deletions src/test/java/ch/digitalfondue/jfiveparse/OptionParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -32,7 +32,7 @@ void rawTableHandling() {
assertEquals("<html><head></head><body>a</body></html>", JFiveParse.serialize(dw));

// with option
Document l = JFiveParse.parse("<html><body><tr><td>a</td></tr></body>", Collections.singleton(Option.DISABLE_IGNORE_TOKEN_IN_BODY_START_TAG));
Document l = JFiveParse.parse("<html><body><tr><td>a</td></tr></body>", Set.of(Option.DISABLE_IGNORE_TOKEN_IN_BODY_START_TAG));
assertEquals("<html><head></head><body><tr><td>a</td></tr></body></html>", JFiveParse.serialize(l));
}

Expand All @@ -44,8 +44,27 @@ void optionInterpretSelfClosing() {
assertEquals("<hr><sj-test><sj-a><sj-b></sj-b></sj-a></sj-test>", html);

// with option
var selfClosing = JFiveParse.parseFragment("<hr /><sj-test /><sj-a><sj-b /></mj-a>", Collections.singleton(Option.INTERPRET_SELF_CLOSING_ANYTHING_ELSE));
var selfClosing = JFiveParse.parseFragment("<hr /><sj-test /><sj-a><sj-b /></mj-a>", Set.of(Option.INTERPRET_SELF_CLOSING_ANYTHING_ELSE));
var selfClosingHtml = selfClosing.stream().map(s -> ((Element) s).getOuterHTML()).collect(Collectors.joining());
assertEquals("<hr><sj-test></sj-test><sj-a><sj-b></sj-b></sj-a>", selfClosingHtml);
}

@Test
void optionDisableInTableTextFosterParenting() {
var res = JFiveParse.parse("""
<table>1
<thead>12<tr>3<th>4</th>5</tr>6</thead>21
<tbody>22<tr>3<td>4</td>5</tr>6</tbody>22
<tfoot>32<tr>3<th>4</th>5</tr>6</tfoot>23
</table>
""", Set.of(Option.DISABLE_IN_TABLE_TEXT_FOSTER_PARENTING));

assertEquals("""
<table>1
<thead>12<tr>3<th>4</th>5</tr>6</thead>21
<tbody>22<tr>3<td>4</td>5</tr>6</tbody>22
<tfoot>32<tr>3<th>4</th>5</tr>6</tfoot>23
</table>
""", res.getBody().getInnerHTML());
}
}
2 changes: 1 addition & 1 deletion src/test/java/ch/digitalfondue/jfiveparse/TokenSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TokenSaver extends TreeConstructor {
final List<Token> tokens = new ArrayList<>();

public TokenSaver() {
super(false, false);
super(false, false, false);
}

@Override
Expand Down