Attribute selectors match elements without the attribute; [x^=''] matches everything

Open Beginner friendly
#718 0 comments 0 reactions 0 assignees View on GitHub

@bardrop is already working on this.

Since Dec 3, 2026.

  • #719 by @bardrop — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
elixir
Domain
frontend, web-dev

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from philss/floki

All issues in philss/floki

Similar issues

More Elixir issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.