From bd3232dedbbfbc0c1f15e9d11b403d5078915f05 Mon Sep 17 00:00:00 2001 From: Alexandre de Souza Date: Fri, 23 Feb 2024 15:55:17 -0300 Subject: [PATCH] Fix generating html id with special characters (#2495) --- lib/livebook_web/helpers/html_helpers.ex | 7 ++++++- test/livebook_web/helpers/html_helpers_test.exs | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/livebook_web/helpers/html_helpers.ex b/lib/livebook_web/helpers/html_helpers.ex index dd1eda052..f5ef2ede1 100644 --- a/lib/livebook_web/helpers/html_helpers.ex +++ b/lib/livebook_web/helpers/html_helpers.ex @@ -34,9 +34,14 @@ defmodule LivebookWeb.HTMLHelpers do defp name_to_html_id(name) do name - |> String.trim() |> String.downcase() + |> String.replace(~r/[^\s\w]/u, "") + |> String.trim() |> String.replace(~r/\s+/u, "-") + |> case do + "" -> Base.url_encode64(name, padding: false) + id -> id + end end @doc """ diff --git a/test/livebook_web/helpers/html_helpers_test.exs b/test/livebook_web/helpers/html_helpers_test.exs index 61c1569c8..2698d8daa 100644 --- a/test/livebook_web/helpers/html_helpers_test.exs +++ b/test/livebook_web/helpers/html_helpers_test.exs @@ -18,11 +18,11 @@ defmodule LivebookWeb.HTMLHelpersTest do end test "emoji at end" do - assert HTMLHelpers.names_to_html_ids(["Test 🦦 "]) == ["test-🦦"] + assert HTMLHelpers.names_to_html_ids(["Test 🦦 "]) == ["test"] end test "emoji in middle" do - assert HTMLHelpers.names_to_html_ids(["One 🥮 Two"]) == ["one-🥮-two"] + assert HTMLHelpers.names_to_html_ids(["One 🥮 Two"]) == ["one-two"] end test "returns empty list for an empty list" do