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

Already on GitHub? Sign in to your account

Error trying to replace top-level text node in DocumentFragment #775

senotrusov opened this Issue Oct 9, 2012 · 1 comment


None yet
3 participants

The code:

html = "foo"
doc = Nokogiri::HTML::DocumentFragment.parse(html)
doc.children[0].replace "bar"

Gives us error:

RuntimeError: error parsing fragment (1)
  from nokogiri-1.5.5/lib/nokogiri/xml/node.rb:532:in `in_context'
  from nokogiri-1.5.5/lib/nokogiri/xml/node.rb:532:in `parse'
  from nokogiri-1.5.5/lib/nokogiri/html/document_fragment.rb:22:in `initialize'
  from nokogiri-1.5.5/lib/nokogiri/xml/node.rb:508:in `new'
  from nokogiri-1.5.5/lib/nokogiri/xml/node.rb:508:in `fragment'
  from nokogiri-1.5.5/lib/nokogiri/xml/node.rb:925:in `coerce'
  from nokogiri-1.5.5/lib/nokogiri/xml/node.rb:406:in `replace'

As I can tell, that error raises from in_context function in xml_node.c.

Meanwhile, replacing the text nodes which are not in the top level, works as expected:

html = "<div>foo</div>"
doc = Nokogiri::HTML::DocumentFragment.parse(html)

doc.children[0].children[0].replace "bar"
doc.to_html # => "<div>bar</div>"

Just reporting, don't feel competent to provide patch to that issue, sorry.

nokogiri -v:

warnings: []
nokogiri: 1.5.5
  version: 1.9.3
  platform: x86_64-linux
  description: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
  engine: ruby
  binding: extension
  compiled: 2.7.8
  loaded: 2.7.8

flavorjones commented Oct 9, 2012

Thanks for reporting. I'll take a look.

@jvshahid jvshahid closed this in 2d5dcca Oct 21, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment