diff --git a/CHANGES.md b/CHANGES.md index e82b18f11d..4cb8e102c5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/src/main/java/org/jsoup/parser/Parser.java b/src/main/java/org/jsoup/parser/Parser.java index 538413d43b..e183e36a57 100644 --- a/src/main/java/org/jsoup/parser/Parser.java +++ b/src/main/java/org/jsoup/parser/Parser.java @@ -51,7 +51,7 @@ 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) { @@ -59,8 +59,13 @@ public Document parseInput(Reader inputHtml, String baseUri) { } public List parseFragmentInput(String fragment, @Nullable Element context, String baseUri) { + return parseFragmentInput(new StringReader(fragment), context, baseUri); + } + + public List parseFragmentInput(Reader fragment, @Nullable Element context, String baseUri) { return treeBuilder.parseFragment(fragment, context, baseUri, this); } + // gets & sets /** * Get the TreeBuilder currently in use. @@ -184,7 +189,7 @@ public static Document parse(String html, String baseUri) { */ public static List 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)); } /** @@ -202,7 +207,7 @@ public static List 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); } /** @@ -214,7 +219,7 @@ public static List parseFragment(String fragmentHtml, Element context, Str */ public static List 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)); } /** diff --git a/src/main/java/org/jsoup/parser/TreeBuilder.java b/src/main/java/org/jsoup/parser/TreeBuilder.java index 89ba82701b..c9887dfcf6 100644 --- a/src/main/java/org/jsoup/parser/TreeBuilder.java +++ b/src/main/java/org/jsoup/parser/TreeBuilder.java @@ -77,8 +77,8 @@ Document parse(Reader input, String baseUri, Parser parser) { return doc; } - List parseFragment(String inputFragment, @Nullable Element context, String baseUri, Parser parser) { - initialiseParse(new StringReader(inputFragment), baseUri, parser); + List parseFragment(Reader inputFragment, @Nullable Element context, String baseUri, Parser parser) { + initialiseParse(inputFragment, baseUri, parser); initialiseParseFragment(context); runParser(); return completeParseFragment();