naqvis / crystal-html5

Crystal implementation of HTML5-Compliant Tokenizer and Parser with XPath & CSS Selector support
MIT License
36 stars 3 forks source link

Removing attributes from nodes #7

Closed matthewmcgarvey closed 2 years ago

matthewmcgarvey commented 2 years ago

Would it make sense to add a method like HTML5::Node#remove_attr? Right now the only way to do it is to do

node.attr.reject! { |attr| attr.key == key_to_delete }

I'm sure you can come up with some use for it, but the way that I'm using this currently is when an option of a select is picked, the other options need to have selected removed.

naqvis commented 2 years ago

This is parser and doesn't provide API to change the parsed contents. Technically speaking, it should be returning an immutable instance of parsed HTML, and allows navigation and/or filtering via XPath and/or CSS selectors. For methods which returns instances of mutable data, it returns a copy of them, so changing those values locally, won't be affecting the actual in-memory node representation. For example Node#attr returns a copy of attributes via invoking dup method on it.

matthewmcgarvey commented 2 years ago

Then you definitely dont want to look at what I'm using this library for 🙈

It's totally fine if you dont want to support my use-case. In my mind, I'm equating this library to nokogiri and I'm assuming it allows html manipulation. Would you care to explain the differences?

naqvis commented 2 years ago

Thanks @matthewmcgarvey , this library was born due to my frustration of experiencing segfaults while using libxml to parse HTMLs (might be, due to way i was using that) and the time use-case was just to parse the HTML, navigate through the document, find some specific nodes and/or details.

But if there is a chance, where this library can be extended for other use-cases or it can help others to use for their different use cases, then I'm definitely in and would happily accept PRs, get into discussions, or suggestions on how this can be further enhanced.

PS: I haven't used nokogiri and not aware of the functionalities it provide, so unable to give any comments on how this can be compared with nokogiri.

matthewmcgarvey commented 2 years ago

Been meaning to get back to this. This is what I used this library for https://github.com/luckyframework/lucky_flow/pull/137

Because of your shard, lucky flow specs will be well over twice as fast (probably closer to 4x)!

I know that I'm using the shard in a way you did not intend, but I wanted to let you know that I am very appreciative of the work you've put into these shards and have every intention of contributing back to them if issues arise.

Something that just occurred to me, your work helped me make one of my first shards when I began programming Crystal as well (crystar in this case) https://github.com/matthewmcgarvey/webdrivers.cr/blob/fa447b5a41eb82b2ecf3c26372803f2784201d3c/shard.yml#L14-L16

❤️

But! Back to this issue, I will close it for now. It's not a bug, just a feature request and I've already worked around it.