Skip to content

Commit 66892bf

Browse files
Only encode urls when it is needed.
1 parent 39f1c73 commit 66892bf

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

pelican/plugins/image_process/image_process.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,14 @@ def process_img_tag(img, settings, derivative):
417417
process_image((path.source, destination, process), settings)
418418

419419

420+
def format_srcset_element(path, condition):
421+
# space and comma have special meaning in srcset
422+
if " " in path or "," in path:
423+
path = urllib.parse.quote(path)
424+
425+
return f"{path} {condition}"
426+
427+
420428
def build_srcset(img, settings, derivative):
421429
path = compute_paths(img, settings, derivative)
422430
process = settings["IMAGE_PROCESS"][derivative]
@@ -446,8 +454,7 @@ def build_srcset(img, settings, derivative):
446454
srcset = []
447455
for src in process["srcset"]:
448456
file_path = posixpath.join(path.base_url, src[0], path.filename)
449-
file_path = urllib.parse.quote(file_path)
450-
srcset.append(f"{file_path} {src[0]}")
457+
srcset.append(format_srcset_element(file_path, src[0]))
451458
destination = os.path.join(str(path.base_path), src[0], path.filename)
452459
process_image((path.source, destination, src[1]), settings)
453460

@@ -535,8 +542,7 @@ def convert_div_to_picture_tag(soup, img, group, settings, derivative):
535542
srcset = []
536543
for src in s["srcset"]:
537544
url = os.path.join(s["base_url"], s["name"], src[0], s["filename"])
538-
url = urllib.parse.quote(str(url))
539-
srcset.append(f"{url} {src[0]}")
545+
srcset.append(format_srcset_element(str(url), src[0]))
540546

541547
source = os.path.join(settings["PATH"], s["url"][1:])
542548
destination = os.path.join(s["base_path"], s["name"], src[0], s["filename"])
@@ -644,8 +650,7 @@ def process_picture(soup, img, group, settings, derivative):
644650
srcset = []
645651
for src in s["srcset"]:
646652
url = posixpath.join(s["base_url"], s["name"], src[0], s["filename"])
647-
url = urllib.parse.quote(str(url))
648-
srcset.append(f"{url} {src[0]}")
653+
srcset.append(format_srcset_element(str(url), src[0]))
649654

650655
source = os.path.join(settings["PATH"], s["url"][1:])
651656
destination = os.path.join(s["base_path"], s["name"], src[0], s["filename"])

pelican/plugins/image_process/test_image_process.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,12 @@ def test_html_and_pictures_generation(mocker, orig_tag, new_tag, call_args):
525525
@pytest.mark.parametrize(
526526
"orig_tag, new_tag",
527527
[
528+
# <img/> src attribute with no quotes, spaces or commas.
529+
(
530+
'<img class="image-process-thumb" src="/tmp/my&amp;_dir/my!_test.jpg" />',
531+
'<img class="image-process-thumb" '
532+
'src="/tmp/my&amp;_dir/derivs/thumb/my!_test.jpg"/>',
533+
),
528534
# <img/> src attribute with double quotes, spaces and commas.
529535
(
530536
'<img class="image-process-thumb" '
@@ -539,8 +545,16 @@ def test_html_and_pictures_generation(mocker, orig_tag, new_tag, call_args):
539545
'<img class="image-process-thumb" '
540546
'src="/tmp/m\'y,&quot; dir/derivs/thumb/my &quot;test,.jpg"/>',
541547
),
548+
# <img/> srcset attribute with no quotes, spaces or commas.
549+
(
550+
'<img class="image-process-crisp" src="/tmp/my&amp;_dir/my!_test.jpg" />',
551+
'<img class="image-process-crisp" '
552+
'src="/tmp/my&amp;_dir/derivs/crisp/1x/my!_test.jpg" '
553+
'srcset="/tmp/my&amp;_dir/derivs/crisp/1x/my!_test.jpg 1x, '
554+
"/tmp/my&amp;_dir/derivs/crisp/2x/my!_test.jpg 2x, "
555+
'/tmp/my&amp;_dir/derivs/crisp/4x/my!_test.jpg 4x"/>',
556+
),
542557
# <img/> srcset attribute with double quotes, spaces and commas.
543-
# In srcset, space and comma have special meaning.
544558
(
545559
'<img class="image-process-crisp" '
546560
'src="/tmp/my,&quot; dir/my &#34;test,.jpg" />',
@@ -551,7 +565,6 @@ def test_html_and_pictures_generation(mocker, orig_tag, new_tag, call_args):
551565
'/tmp/my%2C%22%20dir/derivs/crisp/4x/my%20%22test%2C.jpg 4x"/>',
552566
),
553567
# <img/> srcset attribute with single and double quotes, spaces and commas.
554-
# In srcset, space and comma have special meaning.
555568
(
556569
'<img class="image-process-crisp" '
557570
'src="/tmp/m\'y,&quot; dir/my &#34;test,.jpg" />',
@@ -561,6 +574,25 @@ def test_html_and_pictures_generation(mocker, orig_tag, new_tag, call_args):
561574
"/tmp/m%27y%2C%22%20dir/derivs/crisp/2x/my%20%22test%2C.jpg 2x, "
562575
'/tmp/m%27y%2C%22%20dir/derivs/crisp/4x/my%20%22test%2C.jpg 4x"/>',
563576
),
577+
# <picture/> src and srcset attributes with no quotes, spaces or commas.
578+
(
579+
'<picture><source class="source-1" '
580+
'src="/my&amp;_dir/my!_pelican-closeup.jpg"/><img '
581+
'class="image-process-pict" src="/my&amp;_dir/my!_pelican.jpg"/>'
582+
"</picture>",
583+
'<picture><source media="(min-width: 640px)" sizes="100vw" '
584+
'srcset="/my&amp;_dir/derivs/pict/default/640w/'
585+
"my!_pelican.jpg 640w, "
586+
"/my&amp;_dir/derivs/pict/default/1024w/my!_pelican.jpg 1024w, "
587+
'/my&amp;_dir/derivs/pict/default/1600w/my!_pelican.jpg 1600w"/>'
588+
'<source srcset="/my&amp;_dir/derivs/pict/source-1/1x/'
589+
"my!_pelican-closeup.jpg 1x, "
590+
"/my&amp;_dir/derivs/pict/source-1/2x/"
591+
'my!_pelican-closeup.jpg 2x"/><img '
592+
'class="image-process-pict" '
593+
'src="/my&amp;_dir/derivs/pict/default/640w/my!_pelican.jpg"/>'
594+
"</picture>",
595+
),
564596
# <picture/> src and srcset attributes with double quotes, spaces and commas.
565597
(
566598
'<picture><source class="source-1" '
@@ -611,7 +643,7 @@ def test_special_chars_in_image_path_are_handled_properly(mocker, orig_tag, new_
611643
according to the quotation mark used to enclose the attribute value.
612644
613645
For the srcset attribute, in addition to quotes, spaces and commas
614-
need to be escaped.
646+
need to be url-encoded.
615647
616648
Related to issue #78 https://github.com/pelican-plugins/image-process/issues/78
617649
"""

0 commit comments

Comments
 (0)