Skip to content

Commit

Permalink
update doc+test
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jan 29, 2024
1 parent b0f9d94 commit e99ee54
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,19 @@ static method in the helper class: `W3CDom.toW3CDocument`.
- html by whatwg: https://html.spec.whatwg.org/multipage/syntax.html (of interest: tokenization, tree-construction)
- entities: https://html.spec.whatwg.org/entities.json

### template element handling
### Template element handling

The template element is a "normal" element, so the child nodes are _not_
placed inside a documentFragment. This will be fixed.

### Special parsing options

The parser can be customized to allow some non-standard behaviour, you can see the following tests: https://github.com/digitalfondue/jfiveparse/blob/master/src/test/java/ch/digitalfondue/jfiveparse/OptionParseTest.java

- DISABLE_IGNORE_TOKEN_IN_BODY_START_TAG : allow to have for example "tr" tag without the containing table/tbody.
- INTERPRET_SELF_CLOSING_ANYTHING_ELSE : When encountering unknown self-closing tag, they will be interpreted
as it is and not as open tag only, thus creating a non-intuitive DOM.

### Entities
The &ntities; are by default (and by specification) parsed and interpreted.
This behavior can be disabled by:
Expand Down
15 changes: 10 additions & 5 deletions src/test/java/ch/digitalfondue/jfiveparse/OptionParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,30 @@
import org.junit.Test;

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

public class OptionParseTest {


@Test
public void testRawTableHanding() {
List<Node> l = JFiveParse.parseFragment("<tr><td>a</td></tr>", Collections.singleton(Option.DISABLE_IGNORE_TOKEN_IN_BODY_START_TAG));
Assert.assertEquals("<tr><td>a</td></tr>", JFiveParse.serialize(l.get(0)));
public void testRawTableHandling() {
// without option
Document dw = JFiveParse.parse("<html><body><tr><td>a</td></tr></body>");
Assert.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));
Assert.assertEquals("<html><head></head><body><tr><td>a</td></tr></body></html>", JFiveParse.serialize(l));
}

@Test
public void testOptionInterpretSelfClosing() {
// without option
var res = JFiveParse.parseFragment("<hr /><sj-test /><sj-a><sj-b /></mj-a>");
var html = res.stream().map(s -> ((Element) s).getOuterHTML()).collect(Collectors.joining());
Assert.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 selfClosingHtml = selfClosing.stream().map(s -> ((Element) s).getOuterHTML()).collect(Collectors.joining());
Assert.assertEquals("<hr><sj-test></sj-test><sj-a><sj-b></sj-b></sj-a>", selfClosingHtml);
Expand Down

0 comments on commit e99ee54

Please sign in to comment.