Skip to content

Commit

Permalink
perf: Improve performance when rendering elements (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv authored Oct 19, 2024
1 parent 3ae768b commit 83d4c90
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/blueprint/html/element_registrar.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,45 @@ module Blueprint::HTML::ElementRegistrar
{% tag ||= method_name.tr("_", "-") %}

private def {{method_name.id}}(**attributes, &block) : Nil
element({{tag}}, **attributes) { yield }
@buffer << "<{{tag.id}}"
append_attributes(attributes)
@buffer << ">"
capture_content { yield }
@buffer << "</{{tag.id}}>"
end

private def {{method_name.id}}(**attributes) : Nil
element({{tag}}, "", **attributes)
@buffer << "<{{tag.id}}"
append_attributes(attributes)
@buffer << "></{{tag.id}}>"
end

private def {{method_name.id}}(__content__, **attributes) : Nil
element({{tag}}, __content__, **attributes)
@buffer << "<{{tag.id}}"
append_attributes(attributes)
@buffer << ">"
append_to_buffer(__content__)
@buffer << "</{{tag.id}}>"
end
end

macro register_empty_element(method_name, tag = nil)
{% tag ||= method_name.tr("_", "-") %}

private def {{method_name.id}}(**attributes) : Nil
element({{tag}}, "", **attributes)
@buffer << "<{{tag.id}}"
append_attributes(attributes)
@buffer << "></{{tag.id}}>"
end
end

macro register_void_element(method_name, tag = nil)
{% tag ||= method_name.tr("_", "-") %}

private def {{method_name.id}}(**attributes) : Nil
void_element({{tag}}, **attributes)
@buffer << "<{{tag.id}}"
append_attributes(attributes)
@buffer << ">"
end
end
end

0 comments on commit 83d4c90

Please sign in to comment.