From 6767da7c449d2463fdbf322fe8c67194a11d9cb6 Mon Sep 17 00:00:00 2001 From: Jack McCracken Date: Sat, 18 Nov 2017 03:09:02 -0500 Subject: [PATCH 1/4] Add an options parameter to all the methods that allow creation of an HTML document fragment --- lib/nokogiri/html.rb | 4 ++-- lib/nokogiri/html/document_fragment.rb | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/nokogiri/html.rb b/lib/nokogiri/html.rb index 624e9fcd2f..617cf9c7fe 100644 --- a/lib/nokogiri/html.rb +++ b/lib/nokogiri/html.rb @@ -26,8 +26,8 @@ def parse thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT #### # Parse a fragment from +string+ in to a NodeSet. - def fragment string, encoding = nil - HTML::DocumentFragment.parse string, encoding + def fragment string, encoding = nil, options = nil, &block + HTML::DocumentFragment.parse string, encoding, options, &block end end diff --git a/lib/nokogiri/html/document_fragment.rb b/lib/nokogiri/html/document_fragment.rb index 466d0ce223..3546d99a4a 100644 --- a/lib/nokogiri/html/document_fragment.rb +++ b/lib/nokogiri/html/document_fragment.rb @@ -3,7 +3,7 @@ module HTML class DocumentFragment < Nokogiri::XML::DocumentFragment #### # Create a Nokogiri::XML::DocumentFragment from +tags+, using +encoding+ - def self.parse tags, encoding = nil + def self.parse tags, encoding = nil, options = nil doc = HTML::Document.new encoding ||= if tags.respond_to?(:encoding) @@ -19,10 +19,10 @@ def self.parse tags, encoding = nil doc.encoding = encoding - new(doc, tags) + new(doc, tags, nil, options) end - def initialize document, tags = nil, ctx = nil + def initialize document, tags = nil, ctx = nil, options = nil, &block return self unless tags if ctx @@ -38,7 +38,7 @@ def initialize document, tags = nil, ctx = nil path = "/html/body/node()" end - temp_doc = HTML::Document.parse "#{tags}", nil, document.encoding + temp_doc = HTML::Document.parse "#{tags}", nil, document.encoding, options, &block temp_doc.xpath(path).each { |child| child.parent = self } self.errors = temp_doc.errors end From cc392decc040751430a0cf43bcae0c4683807818 Mon Sep 17 00:00:00 2001 From: Jack McCracken Date: Mon, 20 Nov 2017 14:36:48 -0500 Subject: [PATCH 2/4] Add test and fix an issue with options parameter passing --- lib/nokogiri/html.rb | 2 +- lib/nokogiri/html/document_fragment.rb | 6 +++--- test/html/test_document_fragment.rb | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/nokogiri/html.rb b/lib/nokogiri/html.rb index 617cf9c7fe..276c2deeb2 100644 --- a/lib/nokogiri/html.rb +++ b/lib/nokogiri/html.rb @@ -26,7 +26,7 @@ def parse thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT #### # Parse a fragment from +string+ in to a NodeSet. - def fragment string, encoding = nil, options = nil, &block + def fragment string, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block HTML::DocumentFragment.parse string, encoding, options, &block end end diff --git a/lib/nokogiri/html/document_fragment.rb b/lib/nokogiri/html/document_fragment.rb index 3546d99a4a..eab510820c 100644 --- a/lib/nokogiri/html/document_fragment.rb +++ b/lib/nokogiri/html/document_fragment.rb @@ -3,7 +3,7 @@ module HTML class DocumentFragment < Nokogiri::XML::DocumentFragment #### # Create a Nokogiri::XML::DocumentFragment from +tags+, using +encoding+ - def self.parse tags, encoding = nil, options = nil + def self.parse tags, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block doc = HTML::Document.new encoding ||= if tags.respond_to?(:encoding) @@ -19,10 +19,10 @@ def self.parse tags, encoding = nil, options = nil doc.encoding = encoding - new(doc, tags, nil, options) + new(doc, tags, nil, options, &block) end - def initialize document, tags = nil, ctx = nil, options = nil, &block + def initialize document, tags = nil, ctx = nil, options = XML::ParseOptions::DEFAULT_HTML, &block return self unless tags if ctx diff --git a/test/html/test_document_fragment.rb b/test/html/test_document_fragment.rb index 3a85740b42..6cd3da60d7 100644 --- a/test/html/test_document_fragment.rb +++ b/test/html/test_document_fragment.rb @@ -38,6 +38,14 @@ def test_colons_are_not_removed assert_match(/3:30/, doc.to_s) end + def test_passed_options_have_an_effect + html = "testtest", doc.to_html + end + def test_parse_encoding fragment = "
hello world
" f = Nokogiri::HTML::DocumentFragment.parse fragment, 'ISO-8859-1' From 8cc364ddd67d244cb1896d188fa06cde34138e55 Mon Sep 17 00:00:00 2001 From: Jack McCracken Date: Sun, 10 Dec 2017 19:28:15 -0500 Subject: [PATCH 3/4] Remove old code that yields self in xml document fragment C extension --- ext/nokogiri/xml_document_fragment.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/ext/nokogiri/xml_document_fragment.c b/ext/nokogiri/xml_document_fragment.c index 2d7fb17150..45de13ca5a 100644 --- a/ext/nokogiri/xml_document_fragment.c +++ b/ext/nokogiri/xml_document_fragment.c @@ -25,8 +25,6 @@ static VALUE new(int argc, VALUE *argv, VALUE klass) rb_node = Nokogiri_wrap_xml_node(klass, node); rb_obj_call_init(rb_node, argc, argv); - if(rb_block_given_p()) rb_yield(rb_node); - return rb_node; } From d2a79473de1a891cdda27d2dfbefccb10297fbb7 Mon Sep 17 00:00:00 2001 From: Jack McCracken Date: Mon, 11 Dec 2017 01:11:29 -0500 Subject: [PATCH 4/4] Fix JRuby extension --- ext/java/nokogiri/XmlDocumentFragment.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/ext/java/nokogiri/XmlDocumentFragment.java b/ext/java/nokogiri/XmlDocumentFragment.java index 7912303561..afb1bb1101 100644 --- a/ext/java/nokogiri/XmlDocumentFragment.java +++ b/ext/java/nokogiri/XmlDocumentFragment.java @@ -17,10 +17,10 @@ * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: - * + * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. @@ -52,6 +52,7 @@ import org.jruby.anno.JRubyMethod; import org.jruby.javasupport.util.RuntimeHelpers; import org.jruby.runtime.ThreadContext; +import org.jruby.runtime.Block; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.ByteList; import org.w3c.dom.Attr; @@ -59,7 +60,7 @@ /** * Class for Nokogiri::XML::DocumentFragment - * + * * @author sergio * @author Yoko Harada */ @@ -75,9 +76,9 @@ public XmlDocumentFragment(Ruby ruby, RubyClass klazz) { super(ruby, klazz); } - @JRubyMethod(name="new", meta = true, required=1, optional=2) - public static IRubyObject rbNew(ThreadContext context, IRubyObject cls, IRubyObject[] args) { - + @JRubyMethod(name="new", meta = true, required=1, optional=3) + public static IRubyObject rbNew(ThreadContext context, IRubyObject cls, IRubyObject[] args, Block block) { + if(args.length < 1) { throw context.getRuntime().newArgumentError(args.length, 1); } @@ -87,7 +88,7 @@ public static IRubyObject rbNew(ThreadContext context, IRubyObject cls, IRubyObj } XmlDocument doc = (XmlDocument) args[0]; - + // make wellformed fragment, ignore invalid namespace, or add appropriate namespace to parse if (args.length > 1 && args[1] instanceof RubyString) { if (XmlDocumentFragment.isTag((RubyString)args[1])) { @@ -159,10 +160,10 @@ private static String addNamespaceDeclIfNeeded(XmlDocument doc, String tags) { tags = tags.replace(e.getKey(), e.getValue()); } } - + return tags; } - + private static CharSequence getNamespaceDecl(final String prefix, NamedNodeMap nodeMap) { for (int i=0; i < nodeMap.getLength(); i++) { Attr attr = (Attr) nodeMap.item(i);