Attribute selectors match elements without the attribute; [x^=''] matches everything
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
Start with get_value/2 in Floki.Selector.AttributeSelector, the cause identified in the issue, and trace how attribute values are compared, including the i flag. Add or update tests for missing attributes and empty values across the listed operators, then verify the results match the expected lists in the issue and browser behavior.
Written by the indexing model from the issue text.
Description
Attribute selectors that compare a value also match elements that do not have the attribute, and ^=, $=, *= with an empty value match every element.
html = ~s(<p id="a" x=""></p><p id="b" x="v"></p><p id="c"></p><p id="d" x="-z"></p>)
{:ok, doc} = Floki.parse_document(html)
ids = fn sel -> doc |> Floki.find(sel) |> Floki.attribute("id") end
ids.("p[x='']") # ["a", "c"] expected ["a"]
ids.("p[x|='']") # ["a", "c", "d"] expected ["a", "d"]
ids.("p[x^='']") # ["a", "b", "c", "d"] expected []
ids.("p[x$='']") # ["a", "b", "c", "d"] expected []
ids.("p[x*='']") # ["a", "b", "c", "d"] expected []
The same happens with the i flag. Browsers (document.querySelectorAll) give the expected lists. Selectors Level 4 says that if the value of ^=, $= or *= is the empty string, the selector does not represent anything.
The cause is get_value/2 in Floki.Selector.AttributeSelector, which returns "" for a missing attribute, so "" is compared as if the attribute were there.
- Dominant language
- Elixir
- Stars
- 2.1k
- Forks
- 172
- Avg merge
- 9d 2h
- Merged PRs (30d)
- 4
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from philss/floki
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Bug
Difficulty 4/5 3-5 days Newbie friendliness 25/100
-
Floki.find doesn't support non-alpha charactersPossibly taken @peguimasid claimed this 71 days ago. OpenBug
Difficulty 3/5 1-2 days Newbie friendliness 48/100
-
Bug
Difficulty 3/5 1-2 days Newbie friendliness 48/100
-
Floki is less lenient with nested comments than browsersPossibly taken @agustin2606 claimed this 2 days ago. OpenBug
Difficulty 4/5 3-5 days Newbie friendliness 38/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
intellij-elixir/intellij-elixir#4938 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
expert-lsp/expert#1040 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 85/100
ash-project/ash_mysql#278 · 2 comments ·
-
L: github:actions L: javascript
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
dependabot/dependabot-core#17100 ·
Maintainers usually reply within 2 days
-
transfer into an existing resource hands the source resource's rights and custody to the receiverOpen
Difficulty 2/5 1-3 hours Newbie friendliness 78/100