From 86951ff8d653c582528195810e26ab83266ab135 Mon Sep 17 00:00:00 2001 From: santhreal <64453045+santhreal@users.noreply.github.com> Date: Sat, 18 Jul 2026 16:57:52 -0700 Subject: [PATCH] fix: do not treat empty href quotes as URLs LINK_REGEX required at least one href character, so href='' captured the closing quote and extract_links emitted paths like /%27. Allow empty matches so empty hrefs resolve to the page URL like other relatives. --- courlan/core.py | 3 ++- tests/unit_tests.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/courlan/core.py b/courlan/core.py index 92c9847..e12a6b4 100644 --- a/courlan/core.py +++ b/courlan/core.py @@ -33,7 +33,8 @@ FIND_LINKS_REGEX = re.compile(r"]+?>", re.I) HREFLANG_REGEX = re.compile(r'hreflang=["\']?([a-z-]+)', re.I) -LINK_REGEX = re.compile(r'href=["\']?([^ ]+?)(["\' >])', re.I) +# *? so empty href="" / href='' do not capture the closing quote as the URL +LINK_REGEX = re.compile(r'href=["\']?([^ ]*?)(["\' >])', re.I) def check_url( diff --git a/tests/unit_tests.py b/tests/unit_tests.py index 2983d5b..12142b7 100644 --- a/tests/unit_tests.py +++ b/tests/unit_tests.py @@ -1248,6 +1248,21 @@ def test_extraction(): # hreflang matches the target language but the tag has no href pagecontent = 'no href' assert not extract_links(pagecontent, "https://test.com/", False, language="de") + # empty quoted href must not capture the closing quote as a path (/%27) + pagecontent = "x" + assert extract_links(pagecontent, "https://example.org", False) == { + "https://example.org" + } + assert extract_links(pagecontent, "https://example.org", no_filter=True) == { + "https://example.org" + } + pagecontent = 'x' + assert extract_links(pagecontent, "https://example.org", False) == { + "https://example.org" + } + assert extract_links(pagecontent, "https://example.org", no_filter=True) == { + "https://example.org" + } # link known under another form pagecontent = '' assert len(extract_links(pagecontent, "https://test.org", False)) == 1