Skip to content

Commit

Permalink
Merge pull request #77 from starrify/remove-comments-for-base-url
Browse files Browse the repository at this point in the history
[MRG+1] Added: Removing comments before extracting base URLs. Not a solution to #70, but does help in some cases.
  • Loading branch information
wRAR authored Nov 7, 2022
2 parents dc29296 + a013663 commit fb70566
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,30 @@ def test_get_base_url(self):
get_base_url(text, baseurl.encode("ascii")), "http://example.org/something"
)

def test_base_url_in_comment(self):
self.assertEqual(
get_base_url("""<!-- <base href="http://example.com/"/> -->"""), ""
)
self.assertEqual(
get_base_url("""<!-- <base href="http://example.com/"/>"""), ""
)
self.assertEqual(
get_base_url("""<!-- <base href="http://example.com/"/> --"""), ""
)
self.assertEqual(
get_base_url(
"""<!-- <!-- <base href="http://example.com/"/> -- --> <base href="http://example_2.com/"/> """
),
"http://example_2.com/",
)

self.assertEqual(
get_base_url(
"""<!-- <base href="http://example.com/"/> --> <!-- <base href="http://example_2.com/"/> --> <base href="http://example_3.com/"/>"""
),
"http://example_3.com/",
)

def test_relative_url_with_absolute_path(self):
baseurl = "https://example.org"
text = """\
Expand Down
2 changes: 1 addition & 1 deletion w3lib/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def get_base_url(
"""

utext = to_unicode(text, encoding)
utext: str = remove_comments(text, encoding=encoding)
m = _baseurl_re.search(utext)
if m:
return urljoin(
Expand Down

0 comments on commit fb70566

Please sign in to comment.