Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added case insentive variation of fl-contains #409

Merged
merged 3 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@ Here you find all the [CSS selectors](https://www.w3.org/TR/selectors/#selectors

There are also some selectors based on non-standard specifications. They are:

| Pattern | Description |
|----------------------|-----------------------------------------------------|
| E:fl-contains('foo') | an E element that contains "foo" inside a text node |
| Pattern | Description |
|-----------------------|------------------------------------------------------------------------|
| E:fl-contains('foo') | an E element that contains "foo" inside a text node |
| E:fl-icontains('foo') | an E element that contains "foo" inside a text node (case insensitive) |

## Special thanks

Expand Down
5 changes: 5 additions & 0 deletions lib/floki/selector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ defmodule Floki.Selector do
PseudoClass.match_contains?(tree, html_node, pseudo_class)
end

# Case insensitive contains
defp pseudo_class_match?(html_node, pseudo_class = %{name: "fl-icontains"}, tree) do
PseudoClass.match_icontains?(tree, html_node, pseudo_class)
end

defp pseudo_class_match?(html_node, %{name: "root"}, tree) do
PseudoClass.match_root?(html_node, tree)
end
Expand Down
15 changes: 15 additions & 0 deletions lib/floki/selector/pseudo_class.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ defmodule Floki.Selector.PseudoClass do
res != nil
end

# Case insensitive contains
def match_icontains?(tree, html_node, %__MODULE__{value: value}) do
downcase_value = String.downcase(value)

res =
Enum.find(html_node.children_nodes_ids, fn id ->
case Map.get(tree.nodes, id) do
%Text{content: content} -> String.downcase(content) =~ downcase_value
_ -> false
end
end)

res != nil
end

defp match_position?(relative_position, value, name) do
case value do
position when is_integer(position) ->
Expand Down
8 changes: 8 additions & 0 deletions test/floki_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,14 @@ defmodule FlokiTest do
]
end

test "icontains pseudo-class" do
doc = document!(html_body(~s(<p>One</p><p>Two</p><div>nothing<b>42</b></div>)))

assert Floki.find(doc, "p:fl-icontains('two')") == [
{"p", [], ["Two"]}
]
end

test "contains psuedo-class with substring" do
html =
document!(
Expand Down