Skip to content

Commit d4f1d8f

Browse files
authored
Comma-separated tokens (#904)
WIP implementation of #882
1 parent 3847e8f commit d4f1d8f

File tree

3 files changed

+99
-6
lines changed

3 files changed

+99
-6
lines changed

lib/phlex/sgml.rb

Lines changed: 6 additions & 6 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)
611+
private def __nested_tokens__(tokens, sep = " ")
612612
buffer = +""
613613

614614
i, length = 0, tokens.length
@@ -619,28 +619,28 @@ def json_escape(string)
619619
case token
620620
when String
621621
if i > 0
622-
buffer << " " << token
622+
buffer << sep << token
623623
else
624624
buffer << token
625625
end
626626
when Symbol
627627
if i > 0
628-
buffer << " " << token.name.tr("_", "-")
628+
buffer << sep << token.name.tr("_", "-")
629629
else
630630
buffer << token.name.tr("_", "-")
631631
end
632632
when Integer, Float, Phlex::SGML::SafeObject
633633
if i > 0
634-
buffer << " " << token.to_s
634+
buffer << sep << token.to_s
635635
else
636636
buffer << token.to_s
637637
end
638638
when Array
639639
if token.length > 0
640640
if i > 0
641-
buffer << " " << __nested_tokens__(token)
641+
buffer << sep << __nested_tokens__(token, sep)
642642
else
643-
buffer << __nested_tokens__(token)
643+
buffer << __nested_tokens__(token, sep)
644644
end
645645
end
646646
when nil

lib/phlex/sgml/elements.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
# frozen_string_literal: true
22

33
module Phlex::SGML::Elements
4+
COMMA_SEPARATED_TOKENS = {
5+
img: <<~RUBY,
6+
if Array === (srcset_attribute = attributes[:srcset])
7+
attributes[:srcset] = __nested_tokens__(srcset_attribute, ", ")
8+
end
9+
RUBY
10+
link: <<~RUBY,
11+
if Array === (media_attribute = attributes[:media])
12+
attributes[:media] = __nested_tokens__(media_attribute, ", ")
13+
end
14+
15+
if Array === (sizes_attribute = attributes[:sizes])
16+
attributes[:sizes] = __nested_tokens__(sizes_attribute, ", ")
17+
end
18+
19+
if Array === (imagesrcset_attribute = attributes[:imagesrcset])
20+
rel_attribute = attributes[:rel] || attributes["rel"]
21+
as_attribute = attributes[:as] || attributes["as"]
22+
23+
if ("preload" == rel_attribute || :preload == rel_attribute) && ("image" == as_attribute || :image == as_attribute)
24+
attributes[:imagesrcset] = __nested_tokens__(imagesrcset_attribute, ", ")
25+
end
26+
end
27+
RUBY
28+
input: <<~RUBY,
29+
if Array === (accept_attribute = attributes[:accept])
30+
type_attribute = attributes[:type] || attributes["type"]
31+
32+
if "file" == type_attribute || :file == type_attribute
33+
attributes[:accept] = __nested_tokens__(accept_attribute, ", ")
34+
end
35+
end
36+
RUBY
37+
}.freeze
38+
439
def __registered_elements__
540
@__registered_elements__ ||= {}
641
end
@@ -23,6 +58,7 @@ def #{method_name}(**attributes)
2358
if block_given # with content block
2459
buffer << "<#{tag}"
2560
begin
61+
#{COMMA_SEPARATED_TOKENS[method_name]}
2662
buffer << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes))
2763
ensure
2864
buffer << ">"
@@ -53,6 +89,7 @@ def #{method_name}(**attributes)
5389
else # without content
5490
buffer << "<#{tag}"
5591
begin
92+
#{COMMA_SEPARATED_TOKENS[method_name]}
5693
buffer << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes))
5794
ensure
5895
buffer << "></#{tag}>"
@@ -114,6 +151,7 @@ def #{method_name}(**attributes)
114151
if attributes.length > 0 # with attributes
115152
buffer << "<#{tag}"
116153
begin
154+
#{COMMA_SEPARATED_TOKENS[method_name]}
117155
buffer << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes))
118156
ensure
119157
buffer << ">"

quickdraw/sgml/attributes.test.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,61 @@
538538
end
539539
end
540540

541+
test ":srcset on img with an Array" do
542+
output = phlex { img(srcset: []) }
543+
assert_equal_html output, %(<img srcset="">)
544+
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">)
547+
end
548+
549+
test ":media on link with an Array" do
550+
output = phlex { link(media: []) }
551+
assert_equal_html output, %(<link media="">)
552+
553+
output = phlex { link(media: ["screen", "print"]) }
554+
assert_equal_html output, %(<link media="screen, print">)
555+
556+
output = phlex { link(media: ["(max-width: 600px)", "(min-width: 1200px)"]) }
557+
assert_equal_html output, %(<link media="(max-width: 600px), (min-width: 1200px)">)
558+
end
559+
560+
test ":sizes on link with an Array" do
561+
output = phlex { link(sizes: []) }
562+
assert_equal_html output, %(<link sizes="">)
563+
564+
output = phlex { link(sizes: ["16x16", "32x32", "64x64"]) }
565+
assert_equal_html output, %(<link sizes="16x16, 32x32, 64x64">)
566+
end
567+
568+
test ":imagesrcset on link with an Array when rel is preload and as is image" do
569+
output = phlex { link(imagesrcset: [], rel: "preload", as: "image") }
570+
assert_equal_html output, %(<link imagesrcset="" rel="preload" as="image">)
571+
572+
output = phlex { link(imagesrcset: ["image.jpg 1x", "image@2x.jpg 2x"], rel: "preload", as: "image") }
573+
assert_equal_html output, %(<link imagesrcset="image.jpg 1x, image@2x.jpg 2x" rel="preload" as="image">)
574+
575+
output = phlex { link(imagesrcset: ["image.jpg 1x", "image@2x.jpg 2x"], rel: :preload, as: :image) }
576+
assert_equal_html output, %(<link imagesrcset="image.jpg 1x, image@2x.jpg 2x" rel="preload" as="image">)
577+
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">)
580+
end
581+
582+
test ":accept on input with array when type is file" do
583+
output = phlex { input(accept: [], type: "file") }
584+
assert_equal_html output, %(<input accept="" type="file">)
585+
586+
output = phlex { input(accept: ["image/jpeg", "image/png"], type: "file") }
587+
assert_equal_html output, %(<input accept="image/jpeg, image/png" type="file">)
588+
589+
output = phlex { input(accept: ["image/jpeg", "image/png"], type: :file) }
590+
assert_equal_html output, %(<input accept="image/jpeg, image/png" type="file">)
591+
592+
output = phlex { input(:accept => ["image/jpeg", "image/png"], "type" => "file") }
593+
assert_equal_html output, %(<input accept="image/jpeg, image/png" type="file">)
594+
end
595+
541596
# This is just for coverage.
542597
Phlex::HTML.call do |c|
543598
c.__send__(:__styles__, nil)

0 commit comments

Comments
 (0)