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

Extend `fl-contains` to mach on children text as well #382

Open revati opened 2 years ago

revati commented 2 years ago

Feature goal

Extend fl-contains to mach on children text as well.

"<div>Some</div>"
|> Floki.parse_document!()
|> Floki.find("div:fl-contains('Some')")
|> IO.inspect(label: :first)
# Output: first: [{"div", [], ["Some"]}]

"<div><span>Some</span></div>"
|> Floki.parse_document!()
|> Floki.find("div:fl-contains('Some')")
|> IO.inspect(label: :second)
# Output: second: []
# Desired output: [{"div", [], [{"span", [], ["Some"]}]}]
philss commented 2 years ago

Hey @revati :wave:

You actually can archive similar results using a descendant combinator with the "all" selector ("*"). Please try:

"<div><span>Some</span></div>"
|> Floki.parse_document!()
|> Floki.find("div *:fl-contains('Some')")
|> IO.inspect(label: :second)
# Output: second: [{"span", [], ["Some"]}]
# Desired output: [{"div", [], [{"span", [], ["Some"]}]}]