Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Refactor remaining Floki usage to LazyHTML (#6125)

Uku Taht75ec6e2fbc37
diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex
index ff87f03c07..b3863eeb95 100644
--- a/lib/plausible_web/email.ex
+++ b/lib/plausible_web/email.ex
@@ -621,9 +621,12 @@ defmodule PlausibleWeb.Email do
end
defp textify(html) do
- Floki.parse_fragment!(html)
+ html
+ |> LazyHTML.from_fragment()
+ |> LazyHTML.to_tree()
|> traverse_and_textify()
- |> Floki.text()
+ |> LazyHTML.from_tree()
+ |> LazyHTML.text()
|> collapse_whitespace()
end
@@ -640,7 +643,7 @@ defmodule PlausibleWeb.Email do
children = traverse_and_textify(children)
if href do
- text = Floki.text(children)
+ text = children |> LazyHTML.from_tree() |> LazyHTML.text()
if text == href do
# avoids rendering "http://localhost:8000 (http://localhost:8000)" in base_email footer
@@ -653,6 +656,10 @@ defmodule PlausibleWeb.Email do
end
end
+ defp traverse_and_textify({"br", _attrs, _children}) do
+ "\n"
+ end
+
defp traverse_and_textify({tag, attrs, children}) do
{tag, attrs, traverse_and_textify(children)}
end
diff --git a/mix.exs b/mix.exs
index b54f83dd71..41f77b95b7 100644
--- a/mix.exs
+++ b/mix.exs
@@ -94,7 +94,6 @@ defmodule Plausible.MixProject do
{:ex_machina, "~> 2.3", only: [:dev, :test, :ce_dev, :ce_test, :e2e_test]},
{:excoveralls, "~> 0.10", only: :test},
{:finch, "~> 0.20.0"},
- {:floki, "~> 0.36"},
{:lazy_html, "~> 0.1.8"},
{:fun_with_flags, "~> 1.13.0"},
{:fun_with_flags_ui, "~> 1.0"},
diff --git a/mix.lock b/mix.lock
index 932a4c0129..b260e56773 100644
--- a/mix.lock
+++ b/mix.lock
@@ -57,7 +57,6 @@
"file_system": {:hex, :file_system, "1.1.1", "31864f4685b0148f25bd3fbef2b1228457c0c89024ad67f7a81a3ffbc0bbad3a", [:mix], [], "hexpm", "7a15ff97dfe526aeefb090a7a9d3d03aa907e100e262a0f8f7746b78f8f87a5d"},
"finch": {:hex, :finch, "0.20.0", "5330aefb6b010f424dcbbc4615d914e9e3deae40095e73ab0c1bb0968933cadf", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2658131a74d051aabfcba936093c903b8e89da9a1b63e430bee62045fa9b2ee2"},
"fine": {:hex, :fine, "0.1.4", "b19a89c1476c7c57afb5f9314aed5960b5bc95d5277de4cb5ee8e1d1616ce379", [:mix], [], "hexpm", "be3324cc454a42d80951cf6023b9954e9ff27c6daa255483b3e8d608670303f5"},
- "floki": {:hex, :floki, "0.38.0", "62b642386fa3f2f90713f6e231da0fa3256e41ef1089f83b6ceac7a3fd3abf33", [:mix], [], "hexpm", "a5943ee91e93fb2d635b612caf5508e36d37548e84928463ef9dd986f0d1abd9"},
"fun_with_flags": {:hex, :fun_with_flags, "1.13.0", "8e8eddd6b723691211387923b160bc779fa593f59485209a058c90ea4bbb1cd5", [:mix], [{:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: true]}, {:redix, "~> 1.0", [hex: :redix, repo: "hexpm", optional: true]}, {:telemetry, "~> 1.3", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "dbbbff607e6f096d3857c88530d1a4e178d9eeec5dc00118e2cc1d076578710a"},
"fun_with_flags_ui": {:hex, :fun_with_flags_ui, "1.1.0", "61a394cd7d80c09943a4b02e07c9ab8899781f3392a157b45527d22731bf871a", [:mix], [{:cowboy, ">= 2.0.0", [hex: :cowboy, repo: "hexpm", optional: true]}, {:fun_with_flags, "~> 1.12", [hex: :fun_with_flags, repo: "hexpm", optional: false]}, {:plug, "~> 1.12", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, ">= 2.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm", "99b578a250141537ff7774abb832427f3406a4faff3cfc4f074788beeef38f5d"},
"gen_cycle": {:hex, :gen_cycle, "1.0.4", "2315199f1256116328fad6a9d4371c2fb0bc39c7f5111b88d2de582300c931cc", [:rebar3], [], "hexpm", "67817e31b352bb00715f80a5571b3c069e26e994b2ebafa376acf76a2d0f66d8"},
diff --git a/test/plausible_web/controllers/settings_controller_test.exs b/test/plausible_web/controllers/settings_controller_test.exs
index 102b54c60b..55c4735415 100644
--- a/test/plausible_web/controllers/settings_controller_test.exs
+++ b/test/plausible_web/controllers/settings_controller_test.exs
@@ -1364,7 +1364,7 @@ defmodule PlausibleWeb.SettingsControllerTest do
:team_danger_zone,
:danger_zone
])
- |> Floki.parse_document!()
+ |> LazyHTML.from_document()
|> assert_sidebar_menu(expected_account_menu)
|> assert_mobile_menu(expected_account_menu)
end
@@ -1420,7 +1420,7 @@ defmodule PlausibleWeb.SettingsControllerTest do
else: [:subscription, :team_general, :sso]
)
)
- |> Floki.parse_document!()
+ |> LazyHTML.from_document()
|> assert_sidebar_menu(expected_account_menu)
|> assert_mobile_menu(expected_account_menu)
end
@@ -1448,7 +1448,7 @@ defmodule PlausibleWeb.SettingsControllerTest do
else: [:subscription, :team_general, :sso]
)
)
- |> Floki.parse_document!()
+ |> LazyHTML.from_document()
|> assert_sidebar_menu(expected_account_menu)
|> assert_mobile_menu(expected_account_menu)
end
@@ -1479,7 +1479,7 @@ defmodule PlausibleWeb.SettingsControllerTest do
)
html
- |> Floki.parse_document!()
+ |> LazyHTML.from_document()
|> assert_sidebar_menu(expected_account_menu, expected_team_menu)
|> assert_mobile_menu(expected_account_menu, expected_team_menu)
end
@@ -1518,7 +1518,7 @@ defmodule PlausibleWeb.SettingsControllerTest do
)
html
- |> Floki.parse_document!()
+ |> LazyHTML.from_document()
|> assert_sidebar_menu(expected_account_menu, expected_team_menu)
|> assert_mobile_menu(expected_account_menu, expected_team_menu)
end
@@ -2075,18 +2075,18 @@ defmodule PlausibleWeb.SettingsControllerTest do
end
defp get_mobile_menu_options(document) do
- Floki.find(document, "[data-testid='mobile-nav-dropdown'] option")
+ LazyHTML.query(document, "[data-testid='mobile-nav-dropdown'] option")
|> Enum.map(&parse_option/1)
end
defp parse_option(option),
- do: {Floki.text(option), Floki.attribute(option, "value") |> List.first()}
+ do: {LazyHTML.text(option), LazyHTML.attribute(option, "value") |> List.first()}
defp get_sidebar_menu_items(document) do
- Floki.find(document, "[data-testid='settings-sidebar'] a")
+ LazyHTML.query(document, "[data-testid='settings-sidebar'] a")
|> Enum.map(&parse_link/1)
end
defp parse_link(link),
- do: {Floki.text(link) |> String.trim(), Floki.attribute(link, "href") |> List.first()}
+ do: {LazyHTML.text(link) |> String.trim(), LazyHTML.attribute(link, "href") |> List.first()}
end