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); 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; } diff --git a/lib/nokogiri/html.rb b/lib/nokogiri/html.rb index 624e9fcd2f..276c2deeb2 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 = 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 466d0ce223..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 + 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 doc.encoding = encoding - new(doc, tags) + new(doc, tags, nil, options, &block) end - def initialize document, tags = nil, ctx = nil + def initialize document, tags = nil, ctx = nil, options = XML::ParseOptions::DEFAULT_HTML, &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 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'