-
-
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
Add support for parsing attributes as maps #467
Conversation
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.
Co-authored-by: José Valim <jose.valim@dashbit.co>
src/floki_mochi_html.erl
Outdated
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), []}; |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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! 💜
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 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.