Skip to content

Commit

Permalink
feat: Allow passing comment via argument
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv committed Sep 9, 2024
1 parent 3cf6f16 commit 8d5c295
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
12 changes: 11 additions & 1 deletion spec/blueprint/html/safety_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ private class DummyPage
render(DummyComponent.new) { "<script>alert('DummyComponent')</script>" }
div(class: "some-class\" onblur=\"alert('Attribute')")
comment { "--><script>alert('Plain Text')</script><!--" }
comment "--><script>alert('Another plain text')</script><!--"
v_btn "<script>alert('content')</script>"
v_btn(class: "some-class\" onclick=\"alert('Attribute')") { "<script>alert('hello')</script>" }
end
Expand Down Expand Up @@ -71,7 +72,7 @@ describe "Blueprint::HTML safety" do
page.to_html.should contain(expected_html)
end

it "escapes comment contents" do
it "escapes comment content passed via block" do
page = DummyPage.new
expected_html = <<-HTML.strip
<!----&gt;&lt;script&gt;alert(&#39;Plain Text&#39;)&lt;/script&gt;&lt;!---->
Expand All @@ -80,6 +81,15 @@ describe "Blueprint::HTML safety" do
page.to_html.should contain(expected_html)
end

it "escapes comment content passed via argument" do
page = DummyPage.new
expected_html = <<-HTML.strip
<!----&gt;&lt;script&gt;alert(&#39;Another plain text&#39;)&lt;/script&gt;&lt;!---->
HTML

page.to_html.should contain(expected_html)
end

it "escapes custom tag content passed via argument" do
page = DummyPage.new
expected_html = <<-HTML.strip
Expand Down
9 changes: 8 additions & 1 deletion spec/blueprint/html/utils_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ private class DummyPage
plain "User"

comment { "This is an html comment" }
comment "This is another html comment"
end
end

Expand All @@ -36,11 +37,17 @@ describe "Blueprint::HTML utils" do
end

describe "#comment" do
it "renders an html comment" do
it "renders an html comment passed via block" do
page = DummyPage.new

page.to_html.should contain("<!--This is an html comment-->")
end

it "renders an html comment passed via argument" do
page = DummyPage.new

page.to_html.should contain("<!--This is another html comment-->")
end
end

describe "#whitespace" do
Expand Down
6 changes: 6 additions & 0 deletions src/blueprint/html/utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ module Blueprint::HTML
@buffer << "-->"
end

private def comment(content : String) : Nil
@buffer << "<!--"
::HTML.escape(content, @buffer)
@buffer << "-->"
end

private def whitespace : Nil
@buffer << " "
end
Expand Down

0 comments on commit 8d5c295

Please sign in to comment.