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: Add unsafe_raw to render content without escaping #58

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
17 changes: 17 additions & 0 deletions spec/blueprint/html/utils_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ private class DummyPage

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

unsafe_raw "<script>Dangerous script</script>"
div { unsafe_raw { "<script>Another dangerous script</script>" } }
end
end

Expand Down Expand Up @@ -57,4 +60,18 @@ describe "Blueprint::HTML utils" do
page.to_html.should contain("<i>Hi</i> User")
end
end

describe "#unsafe_raw" do
it "renders content passed via argument without escaping" do
page = DummyPage.new

page.to_html.should contain("<script>Dangerous script</script>")
end

it "renders content passed via block without escaping" do
page = DummyPage.new

page.to_html.should contain("<div><script>Another dangerous script</script></div>")
end
end
end
8 changes: 8 additions & 0 deletions src/blueprint/html/utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading