-
-
Notifications
You must be signed in to change notification settings - Fork 901
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
attribute.value = attribute.value changes output #1800
Comments
Thanks for reporting this! Certainly weird behavior. I dug in a bit and discovered this doesn't happen if we assign to the attribute via doc = Nokogiri::HTML('<!doctype html><html><body><div alt="x" foo="x"></div></body></html>')
puts doc.to_html
# => <html><body><div alt="x" foo="x"></div></body></html>
doc.at_css("div")["alt"] = ""
doc.at_css("div")["foo"] = ""
puts doc.to_html
# => <html><body><div alt="" foo=""></div></body></html> but does happen if we assign an empty string to the attribute value: doc = Nokogiri::HTML('<!doctype html><html><body><div alt="x" foo="x"></div></body></html>')
puts doc.to_html
# => <html><body><div alt="x" foo="x"></div></body></html>
doc.xpath('//*/@*').each do |a|
a.value = ""
end
puts doc.to_html
# => <html><body><div alt foo></div></body></html> This appears to narrow down the behavior to the |
previously was rendered as a boolean attribute. also allow for boolean attributes by setting value to `nil`. Fixes #1800.
PR is in #1827, going through CI now. |
I'll note that that PR works like this: #! /usr/bin/env ruby
require 'nokogiri'
doc = Nokogiri::HTML('<!doctype html><html><body><div bar="x" foo="x"></div></body></html>')
puts doc.to_html
# => <html><body><div bar="x" foo="x"></div></body></html>
doc.at_css("div").tap do |node|
node.attributes['bar'].value = nil
node.attributes['foo'].value = ""
end
puts doc.to_html
# => <html><body><div bar foo=""></div></body></html> |
Will be fixed in the next release, hopefully in the next few days. |
Wow, thanks! |
Sorry to slightly resurrect this. Sometimes it's desirable to add a new attribute that has no value. The only way I can see to do this is:
Is there a better way? |
@brendon The second way you mention is what Nokogiri currently supports:
If you're thinking that's not a great syntax to have to type, I agree with you, but it's what we're stuck with now for fear of introducing a backwards-incompatible change to the API. FWIW, if this is something you do often, and you're up for it, I'd be open to a PR proposing a new method to do this. Something like |
Hi @flavorjones, that sounds like a great plan. Thank you for being open to that possibility. EDIT: I've not had the time to pursue this. I've only had to do it once and no one else seems to have had the need so I'll leave it for now. |
What problems are you experiencing?
doing attribute.value = attribute.value makes it "boolean"/"minimized"
What's the output from
nokogiri -v
?Nokogiri (1.8.4)
Can you provide a self-contained script that reproduces what you're seeing?
The text was updated successfully, but these errors were encountered: