-
-
Notifications
You must be signed in to change notification settings - Fork 155
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
Allow option to parse attributes as maps #463
Comments
@chrismccord So the idea is to add an option to PS: I think we will not have this problem once we implement #457, but that would be a bigger change. cc @wojtekmach |
Correct me if I'm misunderstanding your proposal, but parsing attributes into a map would lose data (the attribute ordering). The loss of this data might manifest in ways such as the output of rendering parsed HTML being different than the originally parsed HTML. Is the benefit of equality matching AST really worth losing potentially useful data in the process? In regards to changes to the order of rendered attributes in Phoenix or LiveView, shouldn't attributes be rendered in a consistent fashion to avoid continuously altering the produced HTML (helping cacheability, etc)? |
Yes exactly. And yes, it will be problematic for a number of areas, such as |
Confirm, this would lose attribute ordering, but if it's an option would be opt-in. HTML wise, I can say that the order of attributes rarely (if ever?) matters. They can technically be duped as well, but are ignored by browser engines afaik. As far as caching, I'm not convinced caching HTML fragments is really desirable, and we have dynamic attribute generation in LiveView, so we'd need to explicitly sort attributes every time, which wouldn't be ideal. |
@bennelsonweiss if you want to do some measurements it could be helpful. The LiveView heex engine could maintain a user defined order + sorted order of dynamic attributes (say alphabetically), but I would need to know the cost. We use a lot of dynamic attributes, like in the |
Opt-in seems good- I could see it causing some confusion if it was the default behavior but if somebody needs to turn it on then hopefully they'll understand the implications. That said, if there is some alternate solution that provides the testing behavior you want without changing the format of Floki - provided by #457 or through some other helper / dsl - I could see that being even better. |
This is a feature request from #463 and is the base for implementing the same feature in more backends. Right now only the built-in `mochiweb` parser implements the feature.
This is a feature request from #463 and is the base for implementing the same feature in more backends. Right now only the built-in `mochiweb` parser implements the feature.
This is a feature request from #463 and is the base for implementing the same feature in more backends. Right now only the built-in `mochiweb` parser implements the feature.
@chrismccord I implemented the bases for this feature in #467. Please let me know what you think. After merging this, I should implement the feature in https://github.com/rusterlium/html5ever_elixir cc @josevalim |
To clarify, the "issue" is that Phoenix generates HTML attributes in random order, which means assertions on Phoenix apps want to be order independent too. :) |
* Add support for parsing attributes as maps This is a feature request from #463 and is the base for implementing the same feature in more backends. Right now only the built-in `mochiweb` parser implements the feature. * Fix handle of duplicated attributes * Adapt "selector" and tests to accept attrs as maps This makes searching against a tree with attributes as maps work.
This is in line with Floki's new feature, that was introduced in philss/floki#467 The initial idea is from philss/floki#463
* Add parse functions that return attributes as maps This is in line with Floki's new feature, that was introduced in philss/floki#467 The initial idea is from philss/floki#463 * Improve error handling * Add a Rust CI workflow to check fmt and clippy * Add missing guard clause * Remove unused module and rename function * Omit "self::" to refer to local module
This feature has landed on main - you can pass the Thanks everyone! :) |
With the OTP 26 map optimizations, it has become clear that I was matching on incidentally generated html attributes in specific order. This is a problem to test against, because Floki parses the attributes in the order they appear into a list, so if Phoenix or LiveView starts spitting out attributes in different order, tests fail even tho we are relying on HTML parsing to avoid matching directly on strings. Parsing attributes into a map will allow equality matching on the markup AST in tests and prevents ordering issues. What do you think? Thanks!
The text was updated successfully, but these errors were encountered: