Skip to content

Commit 81b8b34

Browse files
kmikeEltonChouEltonChou
authored
Fix keep_fragments=True (#183)
* [FIX] avoid adding `#` after url avoid adding `#` after url if there is no fragment. * [FIX] avoid adding `#` after url when no fragment * fix formatting Co-authored-by: EltonChou <[email protected]> Co-authored-by: Elton Chou <[email protected]>
1 parent 6c23d82 commit 81b8b34

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

tests/test_url.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,12 @@ def test_url_query_cleaner_keep_fragments(self):
630630
keep_fragments=True,
631631
),
632632
)
633+
self.assertEqual(
634+
"product.html?id=200",
635+
url_query_cleaner(
636+
"product.html?id=200&foo=bar&name=wired", ["id"], keep_fragments=True
637+
),
638+
)
633639

634640
def test_path_to_file_uri(self):
635641
if os.name == "nt":

w3lib/url.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def url_query_cleaner(
275275
querylist.append(ksv)
276276
seen.add(k)
277277
url = "?".join([base, sep.join(querylist)]) if querylist else base
278-
if keep_fragments:
278+
if keep_fragments and fragment:
279279
url += "#" + fragment
280280
return cast(str, url)
281281

0 commit comments

Comments
 (0)