floki icon indicating copy to clipboard operation
floki copied to clipboard

Floki.find doesn't support escaped colons in class names

Open fakenickels opened this issue 3 years ago • 2 comments

Description

Floki doesn't support selectors with escaped colons in them, probably it's mixing them up with pseudo selectors

To Reproduce

Steps to reproduce the behavior:

  • Using Floki v0.33.1
  • Using Elixir v1.13.3
  • Using Erlang OTP v24
  • With this code:
 > Floki.find(document, "a.xs\\:red-500")
[] 

Expected behavior

 > Floki.find(document, "a.xs\\:red-500")
[{"a", [{"class", "xs\\:red-500"}], ["I'm a link with a funny selector"]}]

The current workaround is to use a[class="xs:red-500"]

fakenickels avatar Jul 14 '22 12:07 fakenickels

I tried adding a escaped colon to this in a way that it doesn't conflict with pseudo-selectors but with no success so far https://github.com/philss/floki/blob/master/src/floki_selector_lexer.xrl#L3

fakenickels avatar Jul 18 '22 13:07 fakenickels

Hi @fakenickels :wave:

I don't know yet how to solve on the lexer/parser level, but there is a way that works today: you can create a Floki.Selector by hand.

html = """
<!doctype html>
<html><head><title>foo</title></head>
<body>
<h1>Hello world</h1>
<div class="container">
<a class="xs:red-500" href="https://example.com">My link</a>
</div>
</body>
</html>
"""

doc = Floki.parse_document!(html)

selector = %Floki.Selector{type: "a", classes: ["xs:red-500"]}

Floki.find(doc, selector)
#=> [{"a", [{"class", "xs:red-500"}, {"href", "https://example.com"}], ["My link"]}]

This is not ideal, but at least solve the problem for now. I will try to check this next week.

philss avatar Jul 22 '22 17:07 philss

hey @fakenickels, would you mind to test the change introduced in #458?

philss avatar May 29 '23 16:05 philss