From 2ca0c5cd5defe8da8b3d9f5096eff48fa69e8f59 Mon Sep 17 00:00:00 2001 From: David Oliver Date: Fri, 4 Apr 2025 22:37:47 +0200 Subject: [PATCH 1/3] Replace EasyHTML with LazyHTML --- mix.exs | 2 +- mix.lock | 5 +++- test/ex_doc/formatter/html/templates_test.exs | 23 +++++++++++++------ test/ex_doc/formatter/html_test.exs | 10 ++++---- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/mix.exs b/mix.exs index 825bc3143..16835c176 100644 --- a/mix.exs +++ b/mix.exs @@ -47,7 +47,7 @@ defmodule ExDoc.Mixfile do {:makeup_html, ">= 0.1.0", optional: true}, {:jason, "~> 1.2", only: :test}, {:floki, "~> 0.0", only: :test}, - {:easyhtml, "~> 0.0", only: :test} + {:lazy_html, "~> 0.1.0", only: :test} ] end diff --git a/mix.lock b/mix.lock index 419c2703e..244619977 100644 --- a/mix.lock +++ b/mix.lock @@ -1,8 +1,11 @@ %{ + "cc_precompiler": {:hex, :cc_precompiler, "0.1.10", "47c9c08d8869cf09b41da36538f62bc1abd3e19e41701c2cea2675b53c704258", [:mix], [{:elixir_make, "~> 0.7", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "f6e046254e53cd6b41c6bacd70ae728011aa82b2742a80d6e2214855c6e06b22"}, "earmark_parser": {:hex, :earmark_parser, "1.4.44", "f20830dd6b5c77afe2b063777ddbbff09f9759396500cdbe7523efd58d7a339c", [:mix], [], "hexpm", "4778ac752b4701a5599215f7030989c989ffdc4f6df457c5f36938cc2d2a2750"}, - "easyhtml": {:hex, :easyhtml, "0.3.2", "050adfc8074f53b261f7dfe83303d864f1fbf5988245b369f8fdff1bf4c4b3e6", [:mix], [{:floki, "~> 0.35", [hex: :floki, repo: "hexpm", optional: false]}], "hexpm", "b6a936f91612a4870aa3e828cd8da5a08d9e3b6221b4d3012b6ec70b87845d06"}, + "elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"}, + "fine": {:hex, :fine, "0.1.0", "9bb99a5ff9b968f12c3b458fa1277c39e9a620b23a9439103703a25917293871", [:mix], [], "hexpm", "1d6485bf811b95dc6ae3d197c0e6f994880b86167a827983bb29cbfc03a02684"}, "floki": {:hex, :floki, "0.36.2", "a7da0193538c93f937714a6704369711998a51a6164a222d710ebd54020aa7a3", [:mix], [], "hexpm", "a8766c0bc92f074e5cb36c4f9961982eda84c5d2b8e979ca67f5c268ec8ed580"}, "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"}, + "lazy_html": {:hex, :lazy_html, "0.1.0", "619c4c124a7375ecbf66263de90270d221ffc7479afde436717a4e5cceaac954", [:make, :mix], [{:cc_precompiler, "~> 0.1", [hex: :cc_precompiler, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.9.0", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:fine, "~> 0.1.0", [hex: :fine, repo: "hexpm", optional: false]}], "hexpm", "a212f417b0e546055b7d5d72d302fce747b63ac9dfe0cf491c1f9af6d198e256"}, "makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"}, "makeup_c": {:hex, :makeup_c, "0.1.1", "14250b1a69770b1892f4113129417a2df098e2a72b9e1477aa9096e9e6c473a6", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "89e9cf45372822d354c19a7e18d77f84cfd70e2d206ac987eb15a1b8357f2869"}, "makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"}, diff --git a/test/ex_doc/formatter/html/templates_test.exs b/test/ex_doc/formatter/html/templates_test.exs index ae4dbbba1..8c560c4a6 100644 --- a/test/ex_doc/formatter/html/templates_test.exs +++ b/test/ex_doc/formatter/html/templates_test.exs @@ -34,6 +34,15 @@ defmodule ExDoc.Formatter.HTML.TemplatesTest do Templates.module_page(mod, config) end + defp num_nodes_matched(doc, selector) do + import LazyHTML + + doc + |> query(selector) + |> to_tree() + |> length() + end + setup %{tmp_dir: tmp_dir} do File.cp_r!("formatters/html", tmp_dir <> "/html_templates") File.touch!(tmp_dir <> "/html_templates/dist/sidebar_items-123456.js") @@ -497,13 +506,13 @@ defmodule ExDoc.Formatter.HTML.TemplatesTest do end ) - doc = EasyHTML.parse!(content) - assert doc["#example-functions a[href='#example-functions']"] - assert doc["#legacy a[href='#legacy']"] - assert doc["#example-functions [id='example/2']"] - refute doc["#legacy [id='example/2']"] - assert doc["#functions [id='example_1/0']"] - refute doc["#functions [id='example/2']"] + doc = LazyHTML.from_document(content) + assert num_nodes_matched(doc, "#example-functions a[href='#example-functions']") == 1 + assert num_nodes_matched(doc, "#legacy a[href='#legacy']") == 1 + assert num_nodes_matched(doc, "#example-functions [id='example/2']") == 1 + assert num_nodes_matched(doc, "#legacy [id='example/2']") == 0 + assert num_nodes_matched(doc, "#functions [id='example_1/0']") == 1 + assert num_nodes_matched(doc, "#functions [id='example/2']") == 0 end test "outputs deprecation information", context do diff --git a/test/ex_doc/formatter/html_test.exs b/test/ex_doc/formatter/html_test.exs index 8677bbf07..18af6da0a 100644 --- a/test/ex_doc/formatter/html_test.exs +++ b/test/ex_doc/formatter/html_test.exs @@ -85,6 +85,8 @@ defmodule ExDoc.Formatter.HTMLTest do end test "multiple extras with the same name", c do + import LazyHTML + File.mkdir_p!("#{c.tmp_dir}/foo") File.write!("#{c.tmp_dir}/foo/README.md", """ @@ -105,11 +107,11 @@ defmodule ExDoc.Formatter.HTMLTest do generate_docs(config) - foo_content = EasyHTML.parse!(File.read!("#{c.tmp_dir}/html/readme-1.html"))["#content"] - bar_content = EasyHTML.parse!(File.read!("#{c.tmp_dir}/html/readme-2.html"))["#content"] + foo_content = "#{c.tmp_dir}/html/readme-1.html" |> File.read!() |> from_document() + bar_content = "#{c.tmp_dir}/html/readme-2.html" |> File.read!() |> from_document() - assert to_string(foo_content["h1"]) == "README foo" - assert to_string(bar_content["h1"]) == "README bar" + assert foo_content |> query("h1") |> text() == "README foo" + assert bar_content |> query("h1") |> text() == "README bar" end test "warns when generating an index.html file with an invalid redirect", From 46f0fa00caa0c0ddaf529ad584031c716a68a699 Mon Sep 17 00:00:00 2001 From: David Oliver Date: Sat, 5 Apr 2025 15:09:39 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Use=20LazyHTML=E2=80=99s=20Access=20and=20E?= =?UTF-8?q?numerable=20implementations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/ex_doc/formatter/html/templates_test.exs | 21 ++++++------------- test/ex_doc/formatter/html_test.exs | 10 ++++----- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/test/ex_doc/formatter/html/templates_test.exs b/test/ex_doc/formatter/html/templates_test.exs index 8c560c4a6..e622d2dad 100644 --- a/test/ex_doc/formatter/html/templates_test.exs +++ b/test/ex_doc/formatter/html/templates_test.exs @@ -34,15 +34,6 @@ defmodule ExDoc.Formatter.HTML.TemplatesTest do Templates.module_page(mod, config) end - defp num_nodes_matched(doc, selector) do - import LazyHTML - - doc - |> query(selector) - |> to_tree() - |> length() - end - setup %{tmp_dir: tmp_dir} do File.cp_r!("formatters/html", tmp_dir <> "/html_templates") File.touch!(tmp_dir <> "/html_templates/dist/sidebar_items-123456.js") @@ -507,12 +498,12 @@ defmodule ExDoc.Formatter.HTML.TemplatesTest do ) doc = LazyHTML.from_document(content) - assert num_nodes_matched(doc, "#example-functions a[href='#example-functions']") == 1 - assert num_nodes_matched(doc, "#legacy a[href='#legacy']") == 1 - assert num_nodes_matched(doc, "#example-functions [id='example/2']") == 1 - assert num_nodes_matched(doc, "#legacy [id='example/2']") == 0 - assert num_nodes_matched(doc, "#functions [id='example_1/0']") == 1 - assert num_nodes_matched(doc, "#functions [id='example/2']") == 0 + assert Enum.count(doc["#example-functions a[href='#example-functions']"]) == 1 + assert Enum.count(doc["#legacy a[href='#legacy']"]) == 1 + assert Enum.count(doc["#example-functions [id='example/2']"]) == 1 + assert Enum.count(doc["#legacy [id='example/2']"]) == 0 + assert Enum.count(doc["#functions [id='example_1/0']"]) == 1 + assert Enum.count(doc["#functions [id='example/2']"]) == 0 end test "outputs deprecation information", context do diff --git a/test/ex_doc/formatter/html_test.exs b/test/ex_doc/formatter/html_test.exs index 18af6da0a..fc31e4d70 100644 --- a/test/ex_doc/formatter/html_test.exs +++ b/test/ex_doc/formatter/html_test.exs @@ -85,8 +85,6 @@ defmodule ExDoc.Formatter.HTMLTest do end test "multiple extras with the same name", c do - import LazyHTML - File.mkdir_p!("#{c.tmp_dir}/foo") File.write!("#{c.tmp_dir}/foo/README.md", """ @@ -107,11 +105,11 @@ defmodule ExDoc.Formatter.HTMLTest do generate_docs(config) - foo_content = "#{c.tmp_dir}/html/readme-1.html" |> File.read!() |> from_document() - bar_content = "#{c.tmp_dir}/html/readme-2.html" |> File.read!() |> from_document() + foo_content = "#{c.tmp_dir}/html/readme-1.html" |> File.read!() |> LazyHTML.from_document() + bar_content = "#{c.tmp_dir}/html/readme-2.html" |> File.read!() |> LazyHTML.from_document() - assert foo_content |> query("h1") |> text() == "README foo" - assert bar_content |> query("h1") |> text() == "README bar" + assert LazyHTML.text(foo_content["h1"]) == "README foo" + assert LazyHTML.text(bar_content["h1"]) == "README bar" end test "warns when generating an index.html file with an invalid redirect", From 4cf477479311535097060f8a2e144af075e9dde8 Mon Sep 17 00:00:00 2001 From: David Oliver Date: Sat, 5 Apr 2025 15:26:36 +0200 Subject: [PATCH 3/3] Use LazyHTML instead of Floki --- mix.exs | 1 - mix.lock | 1 - test/ex_doc/formatter/html/erlang_test.exs | 7 +++++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/mix.exs b/mix.exs index 16835c176..665135b56 100644 --- a/mix.exs +++ b/mix.exs @@ -46,7 +46,6 @@ defmodule ExDoc.Mixfile do {:makeup_c, ">= 0.1.0", optional: true}, {:makeup_html, ">= 0.1.0", optional: true}, {:jason, "~> 1.2", only: :test}, - {:floki, "~> 0.0", only: :test}, {:lazy_html, "~> 0.1.0", only: :test} ] end diff --git a/mix.lock b/mix.lock index 244619977..ce8e36e50 100644 --- a/mix.lock +++ b/mix.lock @@ -3,7 +3,6 @@ "earmark_parser": {:hex, :earmark_parser, "1.4.44", "f20830dd6b5c77afe2b063777ddbbff09f9759396500cdbe7523efd58d7a339c", [:mix], [], "hexpm", "4778ac752b4701a5599215f7030989c989ffdc4f6df457c5f36938cc2d2a2750"}, "elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"}, "fine": {:hex, :fine, "0.1.0", "9bb99a5ff9b968f12c3b458fa1277c39e9a620b23a9439103703a25917293871", [:mix], [], "hexpm", "1d6485bf811b95dc6ae3d197c0e6f994880b86167a827983bb29cbfc03a02684"}, - "floki": {:hex, :floki, "0.36.2", "a7da0193538c93f937714a6704369711998a51a6164a222d710ebd54020aa7a3", [:mix], [], "hexpm", "a8766c0bc92f074e5cb36c4f9961982eda84c5d2b8e979ca67f5c268ec8ed580"}, "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"}, "lazy_html": {:hex, :lazy_html, "0.1.0", "619c4c124a7375ecbf66263de90270d221ffc7479afde436717a4e5cceaac954", [:make, :mix], [{:cc_precompiler, "~> 0.1", [hex: :cc_precompiler, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.9.0", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:fine, "~> 0.1.0", [hex: :fine, repo: "hexpm", optional: false]}], "hexpm", "a212f417b0e546055b7d5d72d302fce747b63ac9dfe0cf491c1f9af6d198e256"}, "makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"}, diff --git a/test/ex_doc/formatter/html/erlang_test.exs b/test/ex_doc/formatter/html/erlang_test.exs index 4c7edc9cf..6f1944523 100644 --- a/test/ex_doc/formatter/html/erlang_test.exs +++ b/test/ex_doc/formatter/html/erlang_test.exs @@ -30,7 +30,7 @@ defmodule ExDoc.Formatter.HTML.ErlangTest do """) doc = generate_docs(c) - html = Floki.raw_html(doc) + html = LazyHTML.to_html(doc) assert html =~ ~s|-spec foo(t()) -> t().| @@ -54,6 +54,9 @@ defmodule ExDoc.Formatter.HTML.ErlangTest do ExDoc.generate_docs(config[:project], config[:version], config) - [c.tmp_dir, "doc", "foo.html"] |> Path.join() |> File.read!() |> Floki.parse_document!() + [c.tmp_dir, "doc", "foo.html"] + |> Path.join() + |> File.read!() + |> LazyHTML.from_document() end end