Skip to content

Comma-separated tokens #904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
36 changes: 36 additions & 0 deletions lib/phlex/sgml/elements.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
# frozen_string_literal: true

module Phlex::SGML::Elements
COMMA_SEPARATED_TOKENS = {
img: <<~RUBY,
if Array === (tokens = attributes[:srcset])
attributes[:srcset] = __nested_tokens__(tokens, ", ")
end
RUBY
link: <<~RUBY,
if Array === (media_tokens = attributes[:href])
attributes[:media] = __nested_tokens__(media_tokens, ", ")
end

if Array === (sizes_tokens = attributes[:sizes])
attributes[:sizes] = __nested_tokens__(sizes_tokens, ", ")
end
RUBY
input: <<~RUBY,
input_type = attributes[: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 Array === (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
Expand All @@ -23,6 +56,7 @@ def #{method_name}(**attributes)
if block_given # with content block
buffer << "<#{tag}"
begin
#{COMMA_SEPARATED_TOKENS[method_name]}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This outputs nothing for most elements and otherwise outputs the code to pre-process one or two values if they exist before caching.

buffer << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes))
ensure
buffer << ">"
Expand Down Expand Up @@ -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 << "></#{tag}>"
Expand Down Expand Up @@ -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 << ">"
Expand Down