8000 Root hash attributes by willcosgrove · Pull Request #889 · yippee-fun/phlex · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Root hash attributes #889

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 3 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions lib/phlex/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def mix(*args)
[old] + new.to_a
in [String, String]
"#{old} #{new}"
in [_, Hash]
{ _: old, **new }
in [Hash, _]
{ **old, _: new }
in [_, nil]
old
else
Expand Down
24 changes: 17 additions & 7 deletions lib/phlex/sgml.rb
10000
Original file line number Diff line number Diff line change
Expand Up @@ -566,14 +566,20 @@ def __nested_attributes__(attributes, base_name, buffer = +"")
attributes.each do |k, v|
next unless v

name = case k
when String then k
when Symbol then k.name.tr("_", "-")
else raise Phlex::ArgumentError.new("Attribute keys should be Strings or Symbols")
end
if (root_key = (:_ == k))
name = ""
original_base_name = base_name
base_name = base_name.delete_suffix("-")
else
name = case k
when String then k
when Symbol then k.name.tr("_", "-")
else raise Phlex::ArgumentError.new("Attribute keys should be Strings or Symbols")
end

if name.match?(/[<>&"']/)
raise Phlex::ArgumentError.new("Unsafe attribute name detected: #{k}.")
if name.match?(/[<>&"']/)
raise Phlex::ArgumentError.new("Unsafe attribute name detected: #{k}.")
end
end

case v
Expand All @@ -597,6 +603,10 @@ def __nested_attributes__(attributes, base_name, buffer = +"")
raise Phlex::ArgumentError.new("Invalid attribute value #{v.inspect}.")
end

if root_key
base_name = original_base_name
end

buffer
end
end
Expand Down
2 changes: 1 addition & 1 deletion quickdraw/helpers/mix.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
{ data: { controller: "bar" } },
)

assert_equal output, { data: { controller: "bar" } }
assert_equal output, { data: { _: ["foo"], controller: "bar" } }
end

test "array + string" do
Expand Down
8 changes: 8 additions & 0 deletions quickdraw/sgml/attributes.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@
assert_raises(Phlex::ArgumentError) { phlex { div(attribute: { ">" => "a" }) } }
end

test "_, Hash(:_, _)" do
by_itself = phlex { div(attribute: { _: "world" }) }
assert_equal_html by_itself, %(<div attribute="world"></div>)

with_others = phlex { div(data: { _: "test", controller: "hello" }) }
assert_equal_html with_others, %(<div data="test" data-controller="hello"></div>)
end

test "_, Hash(*invalid*, _)" do
assert_raises(Phlex::ArgumentError) { phlex { div(attribute: { Object.new => "a" }) } }
end
Expand Down
0