Skip to content
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
22 changes: 21 additions & 1 deletion spec/blueprint/html/attributes_handling_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe "attributes handling" do
actual_html.should eq expected_html
end

it "replaces `_` by `-` on attribute names" do
it "replaces `_` by `-` on Symbol attribute names" do
actual_html = Blueprint::HTML.build do
section v_model: "user.name", "@click": "doSomething" do
"Blueprint"
Expand All @@ -43,6 +43,26 @@ describe "attributes handling" do
actual_html.should eq expected_html
end

it "accepts Hash attributes" do
actual_html = Blueprint::HTML.build do
input({"data-on:mycustomevent__window" => "$result = evt.detail.value", "data-bind:foo" => true})

div(id: "myDiv", aria: {enabled: "true"}, data: {"on:mycustomevent__window" => "$result = evt.detail.value"}) do
"Hello"
end
end

expected_html = normalize_html <<-HTML
<input data-on:mycustomevent__window="$result = evt.detail.value" data-bind:foo>

<div id="myDiv" aria-enabled="true" data-on:mycustomevent__window="$result = evt.detail.value">
Hello
</div>
HTML

actual_html.should eq expected_html
end

it "accepts boolean attributes" do
actual_html = Blueprint::HTML.build do
input disabled: true, checked: false, outline: "true", border: "false"
Expand Down
10 changes: 7 additions & 3 deletions src/blueprint/html/attributes_renderer.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Blueprint::HTML::AttributesRenderer
extend self

def render(attributes : NamedTuple, to buffer : String::Builder) : Nil
def render(attributes : NamedTuple | Hash, to buffer : String::Builder) : Nil
attributes.each { |name, value| append_attribute(buffer, name, value) }
end

Expand All @@ -15,7 +15,7 @@ module Blueprint::HTML::AttributesRenderer
buffer << parse_name(name)
end

private def append_attribute(buffer : String::Builder, name, value : NamedTuple) : Nil
private def append_attribute(buffer : String::Builder, name, value : NamedTuple | Hash) : Nil
name_prefix = parse_name(name)

value.each do |attr_name, attr_value|
Expand Down Expand Up @@ -51,7 +51,11 @@ module Blueprint::HTML::AttributesRenderer
append_value buffer, value.to_s
end

private def parse_name(name) : String
private def parse_name(name : Symbol) : String
name.to_s.gsub("_", "-")
end

private def parse_name(name : String) : String
name
end
end
18 changes: 17 additions & 1 deletion src/blueprint/html/element_registrar.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ module Blueprint::HTML::ElementRegistrar
{% tag ||= method_name.tr("_", "-") %}

def {{method_name.id}}(**attributes, &block) : Nil
{{method_name.id}}(attributes) { yield }
end

def {{method_name.id}}(**attributes) : Nil
{{method_name.id}}(attributes)
end

def {{method_name.id}}(attributes : NamedTuple | Hash, &block) : Nil
@buffer << "<{{tag.id}}"
AttributesRenderer.render(attributes, to: @buffer)
@buffer << ">"
BufferRenderer.render(to: @buffer) { yield }
@buffer << "</{{tag.id}}>"
end

def {{method_name.id}}(**attributes) : Nil
def {{method_name.id}}(attributes : NamedTuple | Hash) : Nil
@buffer << "<{{tag.id}}"
AttributesRenderer.render(attributes, to: @buffer)
@buffer << "></{{tag.id}}>"
Expand All @@ -21,6 +29,10 @@ module Blueprint::HTML::ElementRegistrar
{% tag ||= method_name.tr("_", "-") %}

def {{method_name.id}}(**attributes) : Nil
{{method_name.id}}(attributes)
end

def {{method_name.id}}(attributes : NamedTuple | Hash) : Nil
@buffer << "<{{tag.id}}"
AttributesRenderer.render(attributes, to: @buffer)
@buffer << "></{{tag.id}}>"
Expand All @@ -31,6 +43,10 @@ module Blueprint::HTML::ElementRegistrar
{% tag ||= method_name.tr("_", "-") %}

def {{method_name.id}}(**attributes) : Nil
{{method_name.id}}(attributes)
end

def {{method_name.id}}(attributes : NamedTuple | Hash) : Nil
@buffer << "<{{tag.id}}"
AttributesRenderer.render(attributes, to: @buffer)
@buffer << ">"
Expand Down