Skip to content

Commit

Permalink
Support Reader in parseFragment
Browse files Browse the repository at this point in the history
Fixes #1177
  • Loading branch information
jhy committed Dec 16, 2024
1 parent 959994d commit dd45bb2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
matching elements. Elements are evaluated and returned as they are found, and the stream can be
terminated early. [2092](https://github.com/jhy/jsoup/pull/2092)
* `Element` objects now implement `Iterable`, enabling them to be used in enhanced for loops.
* Added support for fragment parsing from a `Reader` via
`Parser#parseFragmentInput(Reader, Element, String)`. [1177](https://github.com/jhy/jsoup/issues/1177)

### Bug Fixes

Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/jsoup/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,21 @@ private Parser(Parser copy) {
}

public Document parseInput(String html, String baseUri) {
return treeBuilder.parse(new StringReader(html), baseUri, this);
return parseInput(new StringReader(html), baseUri);
}

public Document parseInput(Reader inputHtml, String baseUri) {
return treeBuilder.parse(inputHtml, baseUri, this);
}

public List<Node> parseFragmentInput(String fragment, @Nullable Element context, String baseUri) {
return parseFragmentInput(new StringReader(fragment), context, baseUri);
}

public List<Node> parseFragmentInput(Reader fragment, @Nullable Element context, String baseUri) {
return treeBuilder.parseFragment(fragment, context, baseUri, this);
}

// gets & sets
/**
* Get the TreeBuilder currently in use.
Expand Down Expand Up @@ -184,7 +189,7 @@ public static Document parse(String html, String baseUri) {
*/
public static List<Node> parseFragment(String fragmentHtml, Element context, String baseUri) {
HtmlTreeBuilder treeBuilder = new HtmlTreeBuilder();
return treeBuilder.parseFragment(fragmentHtml, context, baseUri, new Parser(treeBuilder));
return treeBuilder.parseFragment(new StringReader(fragmentHtml), context, baseUri, new Parser(treeBuilder));
}

/**
Expand All @@ -202,7 +207,7 @@ public static List<Node> parseFragment(String fragmentHtml, Element context, Str
HtmlTreeBuilder treeBuilder = new HtmlTreeBuilder();
Parser parser = new Parser(treeBuilder);
parser.errors = errorList;
return treeBuilder.parseFragment(fragmentHtml, context, baseUri, parser);
return treeBuilder.parseFragment(new StringReader(fragmentHtml), context, baseUri, parser);
}

/**
Expand All @@ -214,7 +219,7 @@ public static List<Node> parseFragment(String fragmentHtml, Element context, Str
*/
public static List<Node> parseXmlFragment(String fragmentXml, String baseUri) {
XmlTreeBuilder treeBuilder = new XmlTreeBuilder();
return treeBuilder.parseFragment(fragmentXml, null, baseUri, new Parser(treeBuilder));
return treeBuilder.parseFragment(new StringReader(fragmentXml), null, baseUri, new Parser(treeBuilder));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jsoup/parser/TreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ Document parse(Reader input, String baseUri, Parser parser) {
return doc;
}

List<Node> parseFragment(String inputFragment, @Nullable Element context, String baseUri, Parser parser) {
initialiseParse(new StringReader(inputFragment), baseUri, parser);
List<Node> parseFragment(Reader inputFragment, @Nullable Element context, String baseUri, Parser parser) {
initialiseParse(inputFragment, baseUri, parser);
initialiseParseFragment(context);
runParser();
return completeParseFragment();
Expand Down

0 comments on commit dd45bb2

Please # to comment.