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

XML::Document#add_child unduly restricts its parameter's type #546

Closed
alexch opened this issue Oct 8, 2011 · 2 comments
Closed

XML::Document#add_child unduly restricts its parameter's type #546

alexch opened this issue Oct 8, 2011 · 2 comments

Comments

@alexch
Copy link

alexch commented Oct 8, 2011

Node#add_child is fine with lots of different types of child -- strings, nodesets, etc...

https://github.com/tenderlove/nokogiri/blob/master/lib/nokogiri/xml/node.rb#L265

...but XML::Document#add_child fails if its parameter doesn't have a "type" method

https://github.com/tenderlove/nokogiri/blob/master/lib/nokogiri/xml/document.rb#L207

This is the sound of the duck typing duck quacking sadly:

undefined method `type' for [#<Nokogiri::XML::Element:0x3fe904f77114 name="alpha">]:Nokogiri::XML::NodeSet

I haven't written a test and patch yet but I'd be happy to give it a shot (if I can get nokogiri to build locally).

@flavorjones
Copy link
Member

Alex,

Thanks for pointing it out. I'll take a look.

@rklemme
Copy link

rklemme commented Jan 3, 2012

Same issue here in 1.5.0, this breaks with 1.9.3 which has #type removed.

irb(main):013:0> dom.add_child ''
NoMethodError: undefined method type' for "<root/>":String from /usr/local/lib/ruby/gems/1.9.1/gems/nokogiri-1.5.0/lib/nokogiri/xml/document.rb:199:inadd_child'
from (irb):13
from /usr/local/bin/irb19:12:in `

'

According to the docs this should work. When I readd #type

class Object
alias type class
end

it works:

irb(main):022:0> dom.add_child 'X'
=> [#<Nokogiri::XML::Element:0x8271f2a name="root" children=[#<Nokogiri::XML::Text:0x82719f8 "X">]>]

The solution seems pretty obvious. :-)

flavorjones added a commit that referenced this issue Feb 20, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants