Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Operating on node attributes #666

Closed
zzak opened this Issue · 6 comments

3 participants

@zzak

From the docs: A Nokogiri::XML::Node may be treated similarly to a hash with regard to attributes

I want to prepend a regular hash attribute

my_hash = { 'my_key' => 'bar' }
my_hash['my_key'].insert 0, 'foo'
my_hash #=> {'my_key'=>'foobar'}

This simply cannot be done with nokogiri

doc = Nokogiri::HTML('<img src="bar" />')
doc.xpath("//img").each { |img| img['src'].insert 0, 'foo' }
doc.to_html #=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><img src=\"bar\"></body></html>\n"

You can do this if you get(attribute) first, however

doc.xpath("//img").each { |img| _src = img['src']; img['src'] = _src.insert 0, 'foo' }
doc.to_html #=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><img src=\"foobar\"></body></html>\n"
@flavorjones
Owner

Hello!

Thanks for asking this question! Your request for assistance using
Nokogiri will not go unanswered!

However, Nokogiri's Github Issues is reserved for reporting bugs or
submitting patches. If you ask your question on the mailing list, Team
Nokogiri promises someone will provide you with an answer in a timely
manner.

If you'd like to read up on Team Nokogiri's rationale for this policy,
please go to http://bit.ly/nokohelp.

Thank you so much for understanding! And thank you for using Nokogiri.

@zzak

Done, and done. Sorry for the noise.

@flavorjones
Owner

I've noted this issue in the 2.0 Roadmap, as we're planning on improving the API, and this is a candidate suggestion.

@benlangfeld

@flavorjones Could you link to the roadmap for 2.0?

@benlangfeld

Thanks Mike. feels retarded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.