From d517bbb2d05e4ac3df9b168c6e9bc80ebb212ffd Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Thu, 8 May 2025 00:02:06 +0100 Subject: [PATCH 1/5] Comma-separated tokens --- lib/phlex/sgml.rb | 12 ++++++------ lib/phlex/sgml/elements.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/lib/phlex/sgml.rb b/lib/phlex/sgml.rb index 655ddd99..ac09d671 100644 --- a/lib/phlex/sgml.rb +++ b/lib/phlex/sgml.rb @@ -604,7 +604,7 @@ def json_escape(string) end end - private def __nested_tokens__(tokens) + private def __nested_tokens__(tokens, sep = " ") buffer = +"" i, length = 0, tokens.length @@ -615,28 +615,28 @@ def json_escape(string) case token when String if i > 0 - buffer << " " << token + buffer << sep << token else buffer << token end when Symbol if i > 0 - buffer << " " << token.name.tr("_", "-") + buffer << sep << token.name.tr("_", "-") else buffer << token.name.tr("_", "-") end when Integer, Float, Phlex::SGML::SafeObject if i > 0 - buffer << " " << token.to_s + buffer << sep << token.to_s else buffer << token.to_s end when Array if token.length > 0 if i > 0 - buffer << " " << __nested_tokens__(token) + buffer << sep << __nested_tokens__(token, sep) else - buffer << __nested_tokens__(token) + buffer << __nested_tokens__(token, sep) end end when nil diff --git a/lib/phlex/sgml/elements.rb b/lib/phlex/sgml/elements.rb index e13b1466..063b1a40 100644 --- a/lib/phlex/sgml/elements.rb +++ b/lib/phlex/sgml/elements.rb @@ -1,6 +1,39 @@ # frozen_string_literal: true module Phlex::SGML::Elements + COMMA_SEPARATED_TOKENS = { + img: <<~RUBY, + if (tokens = attributes[:srcset]) + attributes[:srcset] = __nested_tokens__(tokens, ", ") + end + RUBY + link: <<~RUBY, + if (media_tokens = attributes[:href]) + attributes[:media] = __nested_tokens__(media_tokens, ", ") + end + + if (sizes_tokens = attributes[:sizes]) + attributes[:sizes] = __nested_tokens__(sizes_tokens, ", ") + end + RUBY + input: <<~RUBY, + input_type = attributes[:type] + + if (tokens = attributes[:href] && (:file == input_type || "file" == input_type)) + attributes[:accept] = __nested_tokens__(tokens, ", ") + end + RUBY + meta: <<~RUBY, + http_equiv = attributes[:http_equiv] || attributes["http-equiv"] + + if (tokens = attributes[:content] && ( + :content_security_policy == http_equiv || "content-security-policy" == http_equiv)) + + attributes[:content] = __nested_tokens__(tokens, ", ") + end + RUBY + }.freeze + def __registered_elements__ @__registered_elements__ ||= {} end @@ -23,6 +56,7 @@ def #{method_name}(**attributes) if block_given # with content block buffer << "<#{tag}" begin + #{COMMA_SEPARATED_TOKENS[method_name]} buffer << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) ensure buffer << ">" @@ -53,6 +87,7 @@ def #{method_name}(**attributes) else # without content buffer << "<#{tag}" begin + #{COMMA_SEPARATED_TOKENS[method_name]} buffer << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) ensure buffer << ">" @@ -114,6 +149,7 @@ def #{method_name}(**attributes) if attributes.length > 0 # with attributes buffer << "<#{tag}" begin + #{COMMA_SEPARATED_TOKENS[method_name]} buffer << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) ensure buffer << ">" From d742aba07c9c00102e97a0f575286e3ca351ccca Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Thu, 8 May 2025 00:15:50 +0100 Subject: [PATCH 2/5] Update elements.rb --- lib/phlex/sgml/elements.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/phlex/sgml/elements.rb b/lib/phlex/sgml/elements.rb index 063b1a40..0b3c1dc4 100644 --- a/lib/phlex/sgml/elements.rb +++ b/lib/phlex/sgml/elements.rb @@ -3,31 +3,31 @@ module Phlex::SGML::Elements COMMA_SEPARATED_TOKENS = { img: <<~RUBY, - if (tokens = attributes[:srcset]) + if Array === (tokens = attributes[:srcset]) attributes[:srcset] = __nested_tokens__(tokens, ", ") end RUBY link: <<~RUBY, - if (media_tokens = attributes[:href]) + if Array === (media_tokens = attributes[:href]) attributes[:media] = __nested_tokens__(media_tokens, ", ") end - if (sizes_tokens = attributes[:sizes]) + if Array === (sizes_tokens = attributes[:sizes]) attributes[:sizes] = __nested_tokens__(sizes_tokens, ", ") end RUBY input: <<~RUBY, input_type = attributes[:type] - if (tokens = attributes[:href] && (:file == input_type || "file" == input_type)) + if Array === (tokens = attributes[:href]) && (:file == input_type || "file" == input_type) attributes[:accept] = __nested_tokens__(tokens, ", ") end RUBY meta: <<~RUBY, http_equiv = attributes[:http_equiv] || attributes["http-equiv"] - if (tokens = attributes[:content] && ( - :content_security_policy == http_equiv || "content-security-policy" == http_equiv)) + if Array === (tokens = attributes[:content]) && ( + :content_security_policy == http_equiv || "content-security-policy" == http_equiv) attributes[:content] = __nested_tokens__(tokens, ", ") end From 60ccf99ae2e2012e61c02519e5ab1a006059fbd6 Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Thu, 8 May 2025 09:59:44 +0100 Subject: [PATCH 3/5] Update elements.rb --- lib/phlex/sgml/elements.rb | 39 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/phlex/sgml/elements.rb b/lib/phlex/sgml/elements.rb index 0b3c1dc4..735ac3c7 100644 --- a/lib/phlex/sgml/elements.rb +++ b/lib/phlex/sgml/elements.rb @@ -3,33 +3,44 @@ module Phlex::SGML::Elements COMMA_SEPARATED_TOKENS = { img: <<~RUBY, - if Array === (tokens = attributes[:srcset]) - attributes[:srcset] = __nested_tokens__(tokens, ", ") + if Array === (srcset_attribute = attributes[:srcset]) + attributes[:srcset] = __nested_tokens__(srcset_attribute, ", ") end RUBY link: <<~RUBY, - if Array === (media_tokens = attributes[:href]) - attributes[:media] = __nested_tokens__(media_tokens, ", ") + if Array === (media_attribute = attributes[:media]) + attributes[:media] = __nested_tokens__(media_attribute, ", ") end - if Array === (sizes_tokens = attributes[:sizes]) - attributes[:sizes] = __nested_tokens__(sizes_tokens, ", ") + if Array === (sizes_attribute = attributes[:sizes]) + attributes[:sizes] = __nested_tokens__(sizes_attribute, ", ") + end + + if Array === (imagesrcset_attribute = attributes[:imagesrcset]) + rel_attribute = attributes[:rel] || attributes["rel"] + as_attribute = attributes[:as] || attributes["as"] + + if (:preload == rel_attribute || "preload" == rel_attribute) && (:image == as_attribute || "image" == as_attribute) + attributes[:imagesrcset] = __nested_tokens__(imagesrcset_attribute, ", ") + end end RUBY input: <<~RUBY, - input_type = attributes[:type] + if Array === (accept_attribute = attributes[:accept]) + type_attribute = attributes[:type] || attributes["type"] - if Array === (tokens = attributes[:href]) && (:file == input_type || "file" == input_type) - attributes[:accept] = __nested_tokens__(tokens, ", ") + if :file == type_attribute || "file" == type_attribute + attributes[:accept] = __nested_tokens__(accept_attribute, ", ") + end end RUBY meta: <<~RUBY, - http_equiv = attributes[:http_equiv] || attributes["http-equiv"] - - if Array === (tokens = attributes[:content]) && ( - :content_security_policy == http_equiv || "content-security-policy" == http_equiv) + if Array === (contents_attribute = attributes[:content]) + http_equiv_attribute = attributes[:http_equiv] || attributes["http-equiv"] - attributes[:content] = __nested_tokens__(tokens, ", ") + if :content_security_policy == http_equiv_attribute || "content-security-policy" == http_equiv_attribute + attributes[:content] = __nested_tokens__(contents_attribute, ", ") + end end RUBY }.freeze From a797672c82916aa003751f4ec8a6b00fdd461a1d Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Thu, 8 May 2025 10:44:01 +0100 Subject: [PATCH 4/5] Test comma-sep-lists --- lib/phlex/sgml/elements.rb | 9 ----- quickdraw/sgml/attributes.test.rb | 55 +++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/lib/phlex/sgml/elements.rb b/lib/phlex/sgml/elements.rb index 735ac3c7..fe96e524 100644 --- a/lib/phlex/sgml/elements.rb +++ b/lib/phlex/sgml/elements.rb @@ -34,15 +34,6 @@ module Phlex::SGML::Elements end end RUBY - meta: <<~RUBY, - if Array === (contents_attribute = attributes[:content]) - http_equiv_attribute = attributes[:http_equiv] || attributes["http-equiv"] - - if :content_security_policy == http_equiv_attribute || "content-security-policy" == http_equiv_attribute - attributes[:content] = __nested_tokens__(contents_attribute, ", ") - end - end - RUBY }.freeze def __registered_elements__ diff --git a/quickdraw/sgml/attributes.test.rb b/quickdraw/sgml/attributes.test.rb index ea211899..0f43d85f 100644 --- a/quickdraw/sgml/attributes.test.rb +++ b/quickdraw/sgml/attributes.test.rb @@ -528,6 +528,61 @@ end end +test ":srcset on img with an Array" do + output = phlex { img(srcset: []) } + assert_equal_html output, %() + + output = phlex { img(srcset: ["image.jpg 1x", "image@2x.jpg 2x"]) } + assert_equal_html output, %() +end + +test ":media on link with an Array" do + output = phlex { link(media: []) } + assert_equal_html output, %() + + output = phlex { link(media: ["screen", "print"]) } + assert_equal_html output, %() + + output = phlex { link(media: ["(max-width: 600px)", "(min-width: 1200px)"]) } + assert_equal_html output, %() +end + +test ":sizes on link with an Array" do + output = phlex { link(sizes: []) } + assert_equal_html output, %() + + output = phlex { link(sizes: ["16x16", "32x32", "64x64"]) } + assert_equal_html output, %() +end + +test ":imagesrcset on link with an Array when rel is preload and as is image" do + output = phlex { link(imagesrcset: [], rel: "preload", as: "image") } + assert_equal_html output, %() + + output = phlex { link(imagesrcset: ["image.jpg 1x", "image@2x.jpg 2x"], rel: "preload", as: "image") } + assert_equal_html output, %() + + output = phlex { link(imagesrcset: ["image.jpg 1x", "image@2x.jpg 2x"], rel: :preload, as: :image) } + assert_equal_html output, %() + + output = phlex { link(:imagesrcset => ["image.jpg 1x", "image@2x.jpg 2x"], "rel" => "preload", "as" => "image") } + assert_equal_html output, %() +end + +test ":accept on input with array when type is file" do + output = phlex { input(accept: [], type: "file") } + assert_equal_html output, %() + + output = phlex { input(accept: ["image/jpeg", "image/png"], type: "file") } + assert_equal_html output, %() + + output = phlex { input(accept: ["image/jpeg", "image/png"], type: :file) } + assert_equal_html output, %() + + output = phlex { input(:accept => ["image/jpeg", "image/png"], "type" => "file") } + assert_equal_html output, %() +end + # This is just for coverage. Phlex::HTML.call do |c| c.__send__(:__styles__, nil) From bf3c24f86619c8adfcf269892698278d22701aa9 Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Thu, 8 May 2025 10:45:21 +0100 Subject: [PATCH 5/5] Faster conditional order --- lib/phlex/sgml/elements.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/phlex/sgml/elements.rb b/lib/phlex/sgml/elements.rb index fe96e524..b3b595e6 100644 --- a/lib/phlex/sgml/elements.rb +++ b/lib/phlex/sgml/elements.rb @@ -20,7 +20,7 @@ module Phlex::SGML::Elements rel_attribute = attributes[:rel] || attributes["rel"] as_attribute = attributes[:as] || attributes["as"] - if (:preload == rel_attribute || "preload" == rel_attribute) && (:image == as_attribute || "image" == as_attribute) + if ("preload" == rel_attribute || :preload == rel_attribute) && ("image" == as_attribute || :image == as_attribute) attributes[:imagesrcset] = __nested_tokens__(imagesrcset_attribute, ", ") end end @@ -29,7 +29,7 @@ module Phlex::SGML::Elements if Array === (accept_attribute = attributes[:accept]) type_attribute = attributes[:type] || attributes["type"] - if :file == type_attribute || "file" == type_attribute + if "file" == type_attribute || :file == type_attribute attributes[:accept] = __nested_tokens__(accept_attribute, ", ") end end