Skip to content
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

feat: Allow passing comment via argument #57

Merged
merged 1 commit into from
Sep 9, 2024
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
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
Loading