Skip to content

Commit 4057ab6

Browse files
committed
feat: Bring back process_attributes callback for attribute processing
1 parent 5f59671 commit 4057ab6

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

lib/phlex/html.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def tag(name, **attributes, &)
5555
raise Phlex::ArgumentError.new("Expected the tag name to be a Symbol.")
5656
end
5757

58+
attributes = process_attributes(attributes) if respond_to?(:process_attributes)
59+
5860
if (tag = StandardElements.__registered_elements__[name]) || (tag = name.name.tr("_", "-")).include?("-")
5961
if attributes.length > 0 # with attributes
6062
if block_given # with content block

lib/phlex/sgml.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ def json_escape(string)
462462
private def __render_attributes__(attributes)
463463
state = @_state
464464
return unless state.should_render?
465+
466+
attributes = process_attributes(attributes) if respond_to?(:process_attributes)
465467
state.buffer << (Phlex::ATTRIBUTE_CACHE[attributes] ||= Phlex::SGML::Attributes.generate_attributes(attributes))
466468
end
467469

lib/phlex/sgml/attributes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ module Phlex::SGML::Attributes
66
UNSAFE_ATTRIBUTES = Set.new(%w[srcdoc sandbox http-equiv]).freeze
77
REF_ATTRIBUTES = Set.new(%w[href src action formaction lowsrc dynsrc background ping]).freeze
88

9-
def generate_attributes(attributes, buffer = +"")
9+
def generate_attributes(attributes, buffer = +"", callback: nil)
10+
callback&.(attributes)
11+
1012
attributes.each do |k, v|
1113
next unless v
1214

lib/phlex/sgml/elements.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def #{method_name}(**attributes)
5454
return nil
5555
end
5656
57+
attributes = process_attributes(attributes) if respond_to?(:process_attributes)
58+
5759
if attributes.length > 0 # with attributes
5860
if block_given # with content block
5961
buffer << "<#{tag}"
@@ -147,6 +149,7 @@ def #{method_name}(**attributes)
147149
return unless state.should_render?
148150
149151
buffer = state.buffer
152+
attributes = process_attributes(attributes) if respond_to?(:process_attributes)
150153
151154
if attributes.length > 0 # with attributes
152155
buffer << "<#{tag}"

quickdraw/sgml/attributes.test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@
44

55
include SGMLHelper
66

7+
test 'proscess_attributes callback' do
8+
component = Class.new(Phlex::HTML) do
9+
def initialize(name)
10+
@name = name
11+
@count = 0
12+
end
13+
14+
def process_attributes(attrs)
15+
attrs.tap do
16+
it[:age] = 48 if it[:name] == 'Joel'
17+
end
18+
end
19+
20+
def view_template
21+
i(class: "foo", name: @name) { "hello" }
22+
end
23+
end
24+
25+
assert_equal_html component.call('Joel'), %(<i class="foo" name="Joel" age="48">hello</i>)
26+
assert_equal_html component.call('Sam'), %(<i class="foo" name="Sam">hello</i>)
27+
end
28+
729
test "id attributes must be lower case symbols" do
830
assert_raises(Phlex::ArgumentError) { phlex { div("id" => "abc") } }
931
assert_raises(Phlex::ArgumentError) { phlex { div("ID" => "abc") } }

0 commit comments

Comments
 (0)