From f2c240549b2de1ebde26eafc882efbd0bbbdd54f Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 24 May 2024 12:44:31 +0200 Subject: [PATCH] Support trailing slash in internal link tab selection --- .../alchemy/admin/link_dialog/internal_tab.rb | 4 +-- .../admin/link_dialog/internal_tab_spec.rb | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/app/components/alchemy/admin/link_dialog/internal_tab.rb b/app/components/alchemy/admin/link_dialog/internal_tab.rb index 64943f7656..8e782a271d 100644 --- a/app/components/alchemy/admin/link_dialog/internal_tab.rb +++ b/app/components/alchemy/admin/link_dialog/internal_tab.rb @@ -4,7 +4,7 @@ module Alchemy module Admin module LinkDialog class InternalTab < BaseTab - PAGE_URL_PATTERN = /\/(?[a-z]{2})?(?\/)?(?.*)/ + PAGE_URL_PATTERN = /\A\/(?[a-z]{2})?(?\/)?(?.*?)(?\/)?\z/ def title Alchemy.t("link_overlay_tab_label.internal") @@ -49,7 +49,7 @@ def page end def page_attributes - locale, _slash, urlname = uri.path.match(PAGE_URL_PATTERN)&.captures + locale, _, urlname, _ = uri.path.match(PAGE_URL_PATTERN)&.captures if locale && urlname.present? {language_code: locale, urlname: urlname} diff --git a/spec/components/alchemy/admin/link_dialog/internal_tab_spec.rb b/spec/components/alchemy/admin/link_dialog/internal_tab_spec.rb index 59933758d7..5d4dde97f0 100644 --- a/spec/components/alchemy/admin/link_dialog/internal_tab_spec.rb +++ b/spec/components/alchemy/admin/link_dialog/internal_tab_spec.rb @@ -25,6 +25,20 @@ expect(page.find(:css, "input[name=internal_link]").value).to eq(url) end + context "with trailing slash" do + let(:language) { create(:alchemy_language, default: true, site: site) } + let(:alchemy_page) { create(:alchemy_page, language: language) } + let(:url) { alchemy_page.url_path + "/" + "#" + fragment } + + it "has url value set" do + expect(page.find(:css, "input[name=internal_link]").value).to eq(url) + end + + it "has hash fragment set" do + expect(page.find(:css, "select[name=element_anchor]").value).to eq("#" + fragment) + end + end + it "has hash fragment set" do expect(page.find(:css, "select[name=element_anchor]").value).to eq("#" + fragment) end @@ -35,6 +49,16 @@ it "has url value set" do expect(page.find(:css, "input[name=internal_link]").value).to eq(url) end + + context "with trailing slash" do + let(:language) { create(:alchemy_language, default: true, site: site) } + let(:alchemy_page) { create(:alchemy_page, language: language) } + let(:url) { "/#{alchemy_page.language_code}/#{alchemy_page.urlname}/" } + + it "has url value set" do + expect(page.find(:css, "input[name=internal_link]").value).to eq(url) + end + end end context "with root url" do @@ -57,6 +81,17 @@ it "has url value set to root url" do expect(page.find(:css, "input[name=internal_link]").value).to eq(url) end + + context "with trailing slash" do + let(:language) { create(:alchemy_language, default: true, site: site) } + let(:alchemy_page) { create(:alchemy_page, language: language) } + + let(:url) { alchemy_page && "/en/" } + + it "has url value set to root url" do + expect(page.find(:css, "input[name=internal_link]").value).to eq(url) + end + end end end