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

Add support for parsing attributes as maps #467

Merged
merged 4 commits into from
Jun 14, 2023

Conversation

philss
Copy link
Owner

@philss philss commented Jun 9, 2023

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.
lib/floki.ex Outdated Show resolved Hide resolved
Co-authored-by: José Valim <jose.valim@dashbit.co>
Attrs = [{norm(K, Opts), iolist_to_binary(V)} || {K, V} <- Attrs],
case lists:keyfind(attributes_as_maps, 1, Opts) of
{attributes_as_maps, true} ->
{norm(Tag, Opts), maps:from_list(Attrs), []};
Copy link

@bennelsonweiss bennelsonweiss Jun 10, 2023

Choose a reason for hiding this comment

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

Documentation on maps:from_list says

If the same key appears more than once, the latter (right-most) value is used and the previous values are ignored.

This means for duplicate attributes the last instance is used in the map.

The HTML spec suggests that it's a parse error when you encounter multiple identical attributes (duplicate-attribute parse error), and that the parser ignores the duplicates (keeping the first).

Copy link
Owner Author

Choose a reason for hiding this comment

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

Great catch! Fixed in 708ad44
Thank you! 💜

philss added 2 commits June 10, 2023 19:16
This makes searching against a tree with attributes as maps work.
@philss philss merged commit 0030b78 into main Jun 14, 2023
@philss philss deleted the ps-add-option-to-have-attrs-as-maps branch June 14, 2023 18:02
philss added a commit to rusterlium/html5ever_elixir that referenced this pull request Jun 14, 2023
This is in line with Floki's new feature, that was
introduced in philss/floki#467

The initial idea is from philss/floki#463
philss added a commit to rusterlium/html5ever_elixir that referenced this pull request Jun 16, 2023
* 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
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.

3 participants