Skip to content

Commit 75ec6e2

Browse files
authored
Refactor remaining Floki usage to LazyHTML (#6125)
1 parent f72b151 commit 75ec6e2

4 files changed

Lines changed: 19 additions & 14 deletions

File tree

lib/plausible_web/email.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,12 @@ defmodule PlausibleWeb.Email do
621621
end
622622

623623
defp textify(html) do
624-
Floki.parse_fragment!(html)
624+
html
625+
|> LazyHTML.from_fragment()
626+
|> LazyHTML.to_tree()
625627
|> traverse_and_textify()
626-
|> Floki.text()
628+
|> LazyHTML.from_tree()
629+
|> LazyHTML.text()
627630
|> collapse_whitespace()
628631
end
629632

@@ -640,7 +643,7 @@ defmodule PlausibleWeb.Email do
640643
children = traverse_and_textify(children)
641644

642645
if href do
643-
text = Floki.text(children)
646+
text = children |> LazyHTML.from_tree() |> LazyHTML.text()
644647

645648
if text == href do
646649
# avoids rendering "http://localhost:8000 (http://localhost:8000)" in base_email footer
@@ -653,6 +656,10 @@ defmodule PlausibleWeb.Email do
653656
end
654657
end
655658

659+
defp traverse_and_textify({"br", _attrs, _children}) do
660+
"\n"
661+
end
662+
656663
defp traverse_and_textify({tag, attrs, children}) do
657664
{tag, attrs, traverse_and_textify(children)}
658665
end

mix.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ defmodule Plausible.MixProject do
9494
{:ex_machina, "~> 2.3", only: [:dev, :test, :ce_dev, :ce_test, :e2e_test]},
9595
{:excoveralls, "~> 0.10", only: :test},
9696
{:finch, "~> 0.20.0"},
97-
{:floki, "~> 0.36"},
9897
{:lazy_html, "~> 0.1.8"},
9998
{:fun_with_flags, "~> 1.13.0"},
10099
{:fun_with_flags_ui, "~> 1.0"},

mix.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"file_system": {:hex, :file_system, "1.1.1", "31864f4685b0148f25bd3fbef2b1228457c0c89024ad67f7a81a3ffbc0bbad3a", [:mix], [], "hexpm", "7a15ff97dfe526aeefb090a7a9d3d03aa907e100e262a0f8f7746b78f8f87a5d"},
5858
"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"},
5959
"fine": {:hex, :fine, "0.1.4", "b19a89c1476c7c57afb5f9314aed5960b5bc95d5277de4cb5ee8e1d1616ce379", [:mix], [], "hexpm", "be3324cc454a42d80951cf6023b9954e9ff27c6daa255483b3e8d608670303f5"},
60-
"floki": {:hex, :floki, "0.38.0", "62b642386fa3f2f90713f6e231da0fa3256e41ef1089f83b6ceac7a3fd3abf33", [:mix], [], "hexpm", "a5943ee91e93fb2d635b612caf5508e36d37548e84928463ef9dd986f0d1abd9"},
6160
"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"},
6261
"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"},
6362
"gen_cycle": {:hex, :gen_cycle, "1.0.4", "2315199f1256116328fad6a9d4371c2fb0bc39c7f5111b88d2de582300c931cc", [:rebar3], [], "hexpm", "67817e31b352bb00715f80a5571b3c069e26e994b2ebafa376acf76a2d0f66d8"},

test/plausible_web/controllers/settings_controller_test.exs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ defmodule PlausibleWeb.SettingsControllerTest do
13641364
:team_danger_zone,
13651365
:danger_zone
13661366
])
1367-
|> Floki.parse_document!()
1367+
|> LazyHTML.from_document()
13681368
|> assert_sidebar_menu(expected_account_menu)
13691369
|> assert_mobile_menu(expected_account_menu)
13701370
end
@@ -1420,7 +1420,7 @@ defmodule PlausibleWeb.SettingsControllerTest do
14201420
else: [:subscription, :team_general, :sso]
14211421
)
14221422
)
1423-
|> Floki.parse_document!()
1423+
|> LazyHTML.from_document()
14241424
|> assert_sidebar_menu(expected_account_menu)
14251425
|> assert_mobile_menu(expected_account_menu)
14261426
end
@@ -1448,7 +1448,7 @@ defmodule PlausibleWeb.SettingsControllerTest do
14481448
else: [:subscription, :team_general, :sso]
14491449
)
14501450
)
1451-
|> Floki.parse_document!()
1451+
|> LazyHTML.from_document()
14521452
|> assert_sidebar_menu(expected_account_menu)
14531453
|> assert_mobile_menu(expected_account_menu)
14541454
end
@@ -1479,7 +1479,7 @@ defmodule PlausibleWeb.SettingsControllerTest do
14791479
)
14801480

14811481
html
1482-
|> Floki.parse_document!()
1482+
|> LazyHTML.from_document()
14831483
|> assert_sidebar_menu(expected_account_menu, expected_team_menu)
14841484
|> assert_mobile_menu(expected_account_menu, expected_team_menu)
14851485
end
@@ -1518,7 +1518,7 @@ defmodule PlausibleWeb.SettingsControllerTest do
15181518
)
15191519

15201520
html
1521-
|> Floki.parse_document!()
1521+
|> LazyHTML.from_document()
15221522
|> assert_sidebar_menu(expected_account_menu, expected_team_menu)
15231523
|> assert_mobile_menu(expected_account_menu, expected_team_menu)
15241524
end
@@ -2075,18 +2075,18 @@ defmodule PlausibleWeb.SettingsControllerTest do
20752075
end
20762076

20772077
defp get_mobile_menu_options(document) do
2078-
Floki.find(document, "[data-testid='mobile-nav-dropdown'] option")
2078+
LazyHTML.query(document, "[data-testid='mobile-nav-dropdown'] option")
20792079
|> Enum.map(&parse_option/1)
20802080
end
20812081

20822082
defp parse_option(option),
2083-
do: {Floki.text(option), Floki.attribute(option, "value") |> List.first()}
2083+
do: {LazyHTML.text(option), LazyHTML.attribute(option, "value") |> List.first()}
20842084

20852085
defp get_sidebar_menu_items(document) do
2086-
Floki.find(document, "[data-testid='settings-sidebar'] a")
2086+
LazyHTML.query(document, "[data-testid='settings-sidebar'] a")
20872087
|> Enum.map(&parse_link/1)
20882088
end
20892089

20902090
defp parse_link(link),
2091-
do: {Floki.text(link) |> String.trim(), Floki.attribute(link, "href") |> List.first()}
2091+
do: {LazyHTML.text(link) |> String.trim(), LazyHTML.attribute(link, "href") |> List.first()}
20922092
end

0 commit comments

Comments
 (0)