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

Operating on node attributes #666

Closed
zzak opened this issue Apr 25, 2012 · 6 comments
Closed

Operating on node attributes #666

zzak opened this issue Apr 25, 2012 · 6 comments

Comments

@zzak
Copy link

zzak commented Apr 25, 2012

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
Copy link
Member

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
Copy link
Author

zzak commented Apr 26, 2012

Done, and done. Sorry for the noise.

@flavorjones
Copy link
Member

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
Copy link
Contributor

@flavorjones Could you link to the roadmap for 2.0?

@flavorjones
Copy link
Member

@benlangfeld
Copy link
Contributor

Thanks Mike. feels retarded

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