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

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

Closed
senotrusov opened this issue Oct 9, 2012 · 1 comment
Closed

Comments

@senotrusov
Copy link

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
ruby:
  version: 1.9.3
  platform: x86_64-linux
  description: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
  engine: ruby
libxml:
  binding: extension
  compiled: 2.7.8
  loaded: 2.7.8
@flavorjones
Copy link
Member

Thanks for reporting. I'll take a look.

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