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

Handling boolean attributes properly #120

Merged
merged 1 commit into from Mar 28, 2023
Merged

Conversation

stephannv
Copy link
Contributor

@stephannv stephannv commented Mar 28, 2023

Closes #119.

Nokogiri #value is always returning "" to <input required> and <input required="">, so I have to call #to_html and check if the = character is present. I'm not sure if this is the best way, but I tried a lot of Nokogiri methods and none of them returns nil.

Copy link
Owner

@marcoroth marcoroth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fair enough! Thanks for fixing this @stephannv!

@@ -54,7 +54,7 @@ def handle_attributes(node)

attributes = []

node.attributes.each_value do |attribute|
node.attribute_nodes.each do |attribute|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL attribute_nodes! Thanks for this!

@stephannv
Copy link
Contributor Author

@marcoroth I don't understand why the tests are broken, but I have to change the standalone head tag test to pass on local tests, should I push the changes?
Screen Shot 2023-03-28 at 15 06 13

@marcoroth
Copy link
Owner

marcoroth commented Mar 28, 2023

I suspect it's because we are calling to_html on the attribute and that this is forcing that <meta> tag to generate within the <head> element.

But I think I'm fine with updating the test 👍🏼

@marcoroth
Copy link
Owner

Yeah, seems like this is what is happening:

irb(main):049:0> head = Nokogiri::HTML::Document.parse("<head id='123'>").css("head").first
=> #(Element:0x1e97cc { name = "head", attributes = [ #(Attr:0x1e9920 { name = "id", value = "123" })] })

irb(main):050:0> head.attribute_nodes
=> [#(Attr:0x1e9920 { name = "id", value = "123" })]

irb(main):051:0> head.attribute_nodes.first.to_html
=> " id=\"123\""

irb(main):053:0> head
=>
#(Element:0x1e97cc {
  name = "head",
  attributes = [ #(Attr:0x1e9920 { name = "id", value = "123" })],
  children = [
    #(Element:0x1f4514 {
      name = "meta",
      attributes = [ #(Attr:0x1f4668 { name = "http-equiv", value = "Content-Type" }), #(Attr:0x1f4848 { name = "content", value = "text/html; charset=UTF-8" })]
      })]
  })

@stephannv
Copy link
Contributor Author

Now I'm making the to_html check only if the attribute value is blank instead of doing this to all attributes everytime.

@marcoroth
Copy link
Owner

Now I'm making the to_html check only if the attribute value is blank instead of doing this to all attributes everytime.

Awesome, even better! Thank you!

@marcoroth marcoroth merged commit 57d7af2 into marcoroth:main Mar 28, 2023
5 checks passed
@marcoroth
Copy link
Owner

This is now also running on https://phlexing.fun! 🎉

@stephannv
Copy link
Contributor Author

Nice @marcoroth 👏🏼
Screen Shot 2023-03-28 at 20 29 43

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

Successfully merging this pull request may close these issues.

Wrong boolean attributes conversion
2 participants