Skip to content

Commit 502e2bd

Browse files
committed
Omit attributes when the value is an empty list or contains only nils
1 parent da754d1 commit 502e2bd

2 files changed

Lines changed: 98 additions & 14 deletions

File tree

lib/phlex/sgml.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,13 @@ def json_escape(string)
591591
when Hash
592592
__nested_attributes__(v, "#{base_name}#{name}-", buffer)
593593
when Array
594-
buffer << " " << base_name << name << '="' << __nested_tokens__(v) << '"'
594+
if (value = __nested_tokens__(v))
595+
buffer << " " << base_name << name << '="' << value << '"'
596+
end
595597
when Set
596-
buffer << " " << base_name << name << '="' << __nested_tokens__(v.to_a) << '"'
598+
if (value = __nested_tokens__(v.to_a))
599+
buffer << " " << base_name << name << '="' << value << '"'
600+
end
597601
when Phlex::SGML::SafeObject
598602
buffer << " " << base_name << name << '="' << v.to_s.gsub('"', "&quot;") << '"'
599603
else
@@ -636,11 +640,19 @@ def json_escape(string)
636640
buffer << token.to_s
637641
end
638642
when Array
639-
if token.length > 0
643+
if (value = __nested_tokens__(token, sep))
644+
if i > 0
645+
buffer << sep << value
646+
else
647+
buffer << value
648+
end
649+
end
650+
when Set
651+
if (value = __nested_tokens__(token.to_a, sep))
640652
if i > 0
641-
buffer << sep << __nested_tokens__(token, sep)
653+
buffer << sep << value
642654
else
643-
buffer << __nested_tokens__(token, sep)
655+
buffer << value
644656
end
645657
end
646658
when nil
@@ -652,6 +664,8 @@ def json_escape(string)
652664
i += 1
653665
end
654666

667+
return if buffer.empty?
668+
655669
buffer.gsub('"', "&quot;")
656670
end
657671

quickdraw/sgml/attributes.test.rb

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,27 @@
194194

195195
test "_, Array" do
196196
output = phlex { div(attribute: []) }
197-
assert_equal_html output, %(<div attribute=""></div>)
197+
assert_equal_html output, %(<div></div>)
198+
end
199+
200+
test "_, Array(Array)" do
201+
output = phlex { div(attribute: [[], [], []]) }
202+
assert_equal_html output, %(<div></div>)
198203
end
199204

200205
test "_, Array(nil)" do
201206
output = phlex { div(attribute: [nil, nil, nil]) }
202-
assert_equal_html output, %(<div attribute=""></div>)
207+
assert_equal_html output, %(<div></div>)
208+
end
209+
210+
test "_, Array(Array(nil))" do
211+
output = phlex { div(attribute: [[nil, nil], [nil, nil]]) }
212+
assert_equal_html output, %(<div></div>)
213+
end
214+
215+
test "_, Array(Array(nil), nil)" do
216+
output = phlex { div(attribute: [[nil, nil], nil]) }
217+
assert_equal_html output, %(<div></div>)
203218
end
204219

205220
test "_, Array(String)" do
@@ -386,15 +401,70 @@
386401
assert_equal_html output, %(<div></div>)
387402
end
388403

404+
test "_, Hash(_, Array)" do
405+
output = phlex { div(data: { action: [] }) }
406+
assert_equal_html output, %(<div></div>)
407+
end
408+
409+
test "_, Hash(_, Array(nil))" do
410+
output = phlex { div(data: { action: [nil] }) }
411+
assert_equal_html output, %(<div></div>)
412+
end
413+
414+
test "_, Hash(_, Set)" do
415+
output = phlex { div(data: { action: Set[] }) }
416+
assert_equal_html output, %(<div></div>)
417+
end
418+
419+
test "_, Hash(_, Set)" do
420+
output = phlex { div(data: { action: Set[Set[]] }) }
421+
assert_equal_html output, %(<div></div>)
422+
end
423+
424+
test "_, Hash(_, Set(nil))" do
425+
output = phlex { div(data: { action: Set[nil] }) }
426+
assert_equal_html output, %(<div></div>)
427+
end
428+
429+
test "_, Hash(_, Set(Set(nil)))" do
430+
output = phlex { div(data: { action: Set[Set[nil]] }) }
431+
assert_equal_html output, %(<div></div>)
432+
end
433+
389434
test "_, Hash(_, *invalid*)" do
390435
assert_raises(Phlex::ArgumentError) do
391436
phlex { div(data: { controller: Object.new }) }
392437
end
393438
end
394439

395-
test "_, Set(nil)" do
440+
test "_, Set" do
441+
output = phlex { div(attribute: Set[]) }
442+
assert_equal_html output, %(<div></div>)
443+
end
444+
445+
test "_, Set" do
396446
output = phlex { div(attribute: Set[nil, nil, nil]) }
397-
assert_equal_html output, %(<div attribute=""></div>)
447+
assert_equal_html output, %(<div></div>)
448+
end
449+
450+
test "_, Set(Set)" do
451+
output = phlex { div(attribute: Set[Set[]]) }
452+
assert_equal_html output, %(<div></div>)
453+
end
454+
455+
test "_, Set(Set(nil))" do
456+
output = phlex { div(attribute: Set[Set[nil, nil, nil]]) }
457+
assert_equal_html output, %(<div></div>)
458+
end
459+
460+
test "_, Set(Set, nil)" do
461+
output = phlex { div(attribute: Set[Set[], nil]) }
462+
assert_equal_html output, %(<div></div>)
463+
end
464+
465+
test "_, Set(Set(nil), nil)" do
466+
output = phlex { div(attribute: Set[Set[nil], nil]) }
467+
assert_equal_html output, %(<div></div>)
398468
end
399469

400470
test "_, Set(String)" do
@@ -540,15 +610,15 @@
540610

541611
test ":srcset on img with an Array" do
542612
output = phlex { img(srcset: []) }
543-
assert_equal_html output, %(<img srcset="">)
613+
assert_equal_html output, %(<img>)
544614

545615
output = phlex { img(srcset: ["image.jpg 1x", "image@2x.jpg 2x"]) }
546616
assert_equal_html output, %(<img srcset="image.jpg 1x, image@2x.jpg 2x">)
547617
end
548618

549619
test ":media on link with an Array" do
550620
output = phlex { link(media: []) }
551-
assert_equal_html output, %(<link media="">)
621+
assert_equal_html output, %(<link>)
552622

553623
output = phlex { link(media: ["screen", "print"]) }
554624
assert_equal_html output, %(<link media="screen, print">)
@@ -559,15 +629,15 @@
559629

560630
test ":sizes on link with an Array" do
561631
output = phlex { link(sizes: []) }
562-
assert_equal_html output, %(<link sizes="">)
632+
assert_equal_html output, %(<link>)
563633

564634
output = phlex { link(sizes: ["16x16", "32x32", "64x64"]) }
565635
assert_equal_html output, %(<link sizes="16x16, 32x32, 64x64">)
566636
end
567637

568638
test ":imagesrcset on link with an Array when rel is preload and as is image" do
569639
output = phlex { link(imagesrcset: [], rel: "preload", as: "image") }
570-
assert_equal_html output, %(<link imagesrcset="" rel="preload" as="image">)
640+
assert_equal_html output, %(<link rel="preload" as="image">)
571641

572642
output = phlex { link(imagesrcset: ["image.jpg 1x", "image@2x.jpg 2x"], rel: "preload", as: "image") }
573643
assert_equal_html output, %(<link imagesrcset="image.jpg 1x, image@2x.jpg 2x" rel="preload" as="image">)
@@ -581,7 +651,7 @@
581651

582652
test ":accept on input with array when type is file" do
583653
output = phlex { input(accept: [], type: "file") }
584-
assert_equal_html output, %(<input accept="" type="file">)
654+
assert_equal_html output, %(<input type="file">)
585655

586656
output = phlex { input(accept: ["image/jpeg", "image/png"], type: "file") }
587657
assert_equal_html output, %(<input accept="image/jpeg, image/png" type="file">)

0 commit comments

Comments
 (0)