philss / floki

Floki is a simple HTML parser that enables search for nodes using CSS selectors.
https://hex.pm/packages/floki
MIT License
2.05k stars 155 forks source link

Support HTML ids which contain periods #352

Closed nathanl closed 2 years ago

nathanl commented 3 years ago

Feature goal

HTML ids which contain periods are legal, but Floki does not appear to support this. Periods would need to be escaped somehow in a selector so that they're not interpreted as classes.

For instance, to find <form id="my.example.com-port-filters" class="foo">, we could use Floki.find(~s{form#my\\.example\\.com-port-filters.foo}).

This selector works in the browser console using JavaScript's document.querySelector/1.

nathanl commented 3 years ago

Turns out there's a workaround.

This came up in the context of using LiveViewTest.element/3. @chrismccord pointed out that the attribute syntax could work, which it does. And it also works here: Floki.find(html, ~s{form[id="my.example.com-port-filters"]}).

philss commented 3 years ago

@nathanl I will investigate how we can escape the period. If you want to give it a try, I think we need to add a new rule for scaping in the lexer: https://github.com/philss/floki/blob/master/src/floki_selector_lexer.xrl and then we need to parse it properly here: https://github.com/philss/floki/blob/master/lib/floki/selector/parser.ex

nathanl commented 2 years ago

👏 👍 😄