From 8f930e2d0f9a52f111abf6c6c00d670178e03061 Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Mon, 9 Sep 2024 10:50:53 -0300 Subject: [PATCH] feat: Add unsafe_raw to render content without escaping --- spec/blueprint/html/utils_spec.cr | 17 +++++++++++++++++ src/blueprint/html/utils.cr | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/spec/blueprint/html/utils_spec.cr b/spec/blueprint/html/utils_spec.cr index ce98b57..41b1300 100644 --- a/spec/blueprint/html/utils_spec.cr +++ b/spec/blueprint/html/utils_spec.cr @@ -16,6 +16,9 @@ private class DummyPage comment { "This is an html comment" } comment "This is another html comment" + + unsafe_raw "" + div { unsafe_raw { "" } } end end @@ -57,4 +60,18 @@ describe "Blueprint::HTML utils" do page.to_html.should contain("Hi User") end end + + describe "#unsafe_raw" do + it "renders content passed via argument without escaping" do + page = DummyPage.new + + page.to_html.should contain("") + end + + it "renders content passed via block without escaping" do + page = DummyPage.new + + page.to_html.should contain("
") + end + end end diff --git a/src/blueprint/html/utils.cr b/src/blueprint/html/utils.cr index f6c97fc..a907bb5 100644 --- a/src/blueprint/html/utils.cr +++ b/src/blueprint/html/utils.cr @@ -22,4 +22,12 @@ module Blueprint::HTML private def whitespace : Nil @buffer << " " end + + def unsafe_raw(content : String) : Nil + @buffer << content + end + + def unsafe_raw(&) : Nil + @buffer << yield + end end