Skip to content

Commit 4921cbe

Browse files
authored
Add comma escaping in values of comma-separated lists (yippee-fun#913)
1 parent da754d1 commit 4921cbe

3 files changed

Lines changed: 16 additions & 12 deletions

File tree

lib/phlex/sgml.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ def json_escape(string)
608608
end
609609
end
610610

611-
private def __nested_tokens__(tokens, sep = " ")
611+
private def __nested_tokens__(tokens, sep = " ", gsub_from = nil, gsub_to = "")
612612
buffer = +""
613613

614614
i, length = 0, tokens.length
@@ -618,6 +618,7 @@ def json_escape(string)
618618

619619
case token
620620
when String
621+
token = token.gsub(gsub_from, gsub_to) if gsub_from
621622
if i > 0
622623
buffer << sep << token
623624
else
@@ -638,9 +639,9 @@ def json_escape(string)
638639
when Array
639640
if token.length > 0
640641
if i > 0
641-
buffer << sep << __nested_tokens__(token, sep)
642+
buffer << sep << __nested_tokens__(token, sep, gsub_from, gsub_to)
642643
else
643-
buffer << __nested_tokens__(token, sep)
644+
buffer << __nested_tokens__(token, sep, gsub_from, gsub_to)
644645
end
645646
end
646647
when nil

lib/phlex/sgml/elements.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ module Phlex::SGML::Elements
44
COMMA_SEPARATED_TOKENS = {
55
img: <<~RUBY,
66
if Array === (srcset_attribute = attributes[:srcset])
7-
attributes[:srcset] = __nested_tokens__(srcset_attribute, ", ")
7+
attributes[:srcset] = __nested_tokens__(srcset_attribute, ", ", ",", "%2C")
88
end
99
RUBY
1010
link: <<~RUBY,
1111
if Array === (media_attribute = attributes[:media])
12-
attributes[:media] = __nested_tokens__(media_attribute, ", ")
12+
attributes[:media] = __nested_tokens__(media_attribute, ", ", ",", "%2C")
1313
end
1414
1515
if Array === (sizes_attribute = attributes[:sizes])
16-
attributes[:sizes] = __nested_tokens__(sizes_attribute, ", ")
16+
attributes[:sizes] = __nested_tokens__(sizes_attribute, ", ", ",", "%2C")
1717
end
1818
1919
if Array === (imagesrcset_attribute = attributes[:imagesrcset])
2020
rel_attribute = attributes[:rel] || attributes["rel"]
2121
as_attribute = attributes[:as] || attributes["as"]
2222
2323
if ("preload" == rel_attribute || :preload == rel_attribute) && ("image" == as_attribute || :image == as_attribute)
24-
attributes[:imagesrcset] = __nested_tokens__(imagesrcset_attribute, ", ")
24+
attributes[:imagesrcset] = __nested_tokens__(imagesrcset_attribute, ", ", ",", "%2C")
2525
end
2626
end
2727
RUBY
@@ -30,7 +30,7 @@ module Phlex::SGML::Elements
3030
type_attribute = attributes[:type] || attributes["type"]
3131
3232
if "file" == type_attribute || :file == type_attribute
33-
attributes[:accept] = __nested_tokens__(accept_attribute, ", ")
33+
attributes[:accept] = __nested_tokens__(accept_attribute, ", ", ",", "%2C")
3434
end
3535
end
3636
RUBY

quickdraw/sgml/attributes.test.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,11 @@
542542
output = phlex { img(srcset: []) }
543543
assert_equal_html output, %(<img srcset="">)
544544

545-
output = phlex { img(srcset: ["image.jpg 1x", "image@2x.jpg 2x"]) }
546-
assert_equal_html output, %(<img srcset="image.jpg 1x, image@2x.jpg 2x">)
545+
output = phlex { img(srcset: ["/width=400/image.jpg 1x", "/width=400,dpr=2/image.jpg 2x"]) }
546+
assert_equal_html output, %(<img srcset="/width=400/image.jpg 1x, /width=400%2Cdpr=2/image.jpg 2x">)
547+
548+
output = phlex { img(srcset: ["/width=400/image.jpg 1x", ["/width=400,dpr=2/image.jpg 2x"]]) }
549+
assert_equal_html output, %(<img srcset="/width=400/image.jpg 1x, /width=400%2Cdpr=2/image.jpg 2x">)
547550
end
548551

549552
test ":media on link with an Array" do
@@ -575,8 +578,8 @@
575578
output = phlex { link(imagesrcset: ["image.jpg 1x", "image@2x.jpg 2x"], rel: :preload, as: :image) }
576579
assert_equal_html output, %(<link imagesrcset="image.jpg 1x, image@2x.jpg 2x" rel="preload" as="image">)
577580

578-
output = phlex { link(:imagesrcset => ["image.jpg 1x", "image@2x.jpg 2x"], "rel" => "preload", "as" => "image") }
579-
assert_equal_html output, %(<link imagesrcset="image.jpg 1x, image@2x.jpg 2x" rel="preload" as="image">)
581+
output = phlex { link(:imagesrcset => ["/width=400/image.jpg 1x", "/width=400,dpr=2/image@2x.jpg 2x"], "rel" => "preload", "as" => "image") }
582+
assert_equal_html output, %(<link imagesrcset="/width=400/image.jpg 1x, /width=400%2Cdpr=2/image@2x.jpg 2x" rel="preload" as="image">)
580583
end
581584

582585
test ":accept on input with array when type is file" do

0 commit comments

Comments
 (0)