Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an options parameter to all the methods that allow creation of an HTML document fragment #1692

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions ext/java/nokogiri/XmlDocumentFragment.java
Expand Up @@ -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.
Expand Down Expand Up @@ -52,14 +52,15 @@
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;
import org.w3c.dom.NamedNodeMap;

/**
* Class for Nokogiri::XML::DocumentFragment
*
*
* @author sergio
* @author Yoko Harada <yokolet@gmail.com>
*/
Expand All @@ -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);
}
Expand All @@ -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])) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions ext/nokogiri/xml_document_fragment.c
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/nokogiri/html.rb
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions lib/nokogiri/html/document_fragment.rb
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -38,7 +38,7 @@ def initialize document, tags = nil, ctx = nil
path = "/html/body/node()"
end

temp_doc = HTML::Document.parse "<html><body>#{tags}", nil, document.encoding
temp_doc = HTML::Document.parse "<html><body>#{tags}", nil, document.encoding, options, &block
temp_doc.xpath(path).each { |child| child.parent = self }
self.errors = temp_doc.errors
end
Expand Down
8 changes: 8 additions & 0 deletions test/html/test_document_fragment.rb
Expand Up @@ -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 = "<span>test</span"
doc = Nokogiri::HTML::DocumentFragment.parse(html) do |options|
options.norecover
end
assert_equal "<span>test</span>", doc.to_html
end

def test_parse_encoding
fragment = "<div>hello world</div>"
f = Nokogiri::HTML::DocumentFragment.parse fragment, 'ISO-8859-1'
Expand Down