Skip to content

Commit

Permalink
[BREAKING] refactor: Rename #to_html/#render_to to #to_s
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv committed Sep 13, 2024
1 parent 786862e commit 92157a4
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Alert
end
end
Alert.new.to_html
Alert.new.to_s
```

Output:
Expand Down
8 changes: 4 additions & 4 deletions benchmark/benchmark.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ require "./ecr"

require "../src/blueprint/version"

blueprint_html = BlueprintHTML::Page.new.to_html
blueprint_raw_html = BlueprintRawHTML::Page.new.to_html
blueprint_html = BlueprintHTML::Page.new.to_s
blueprint_raw_html = BlueprintRawHTML::Page.new.to_s
ecr = ECR::Page.new.to_s # ECR is here just to have a base value to compare

raise "Different results" if blueprint_html != blueprint_raw_html && blueprint_html != ecr

Benchmark.ips do |x|
x.report("Blueprint::HTML #{Blueprint::VERSION}") { BlueprintHTML::Page.new.to_html }
x.report("Blueprint::RawHTML #{Blueprint::VERSION}") { BlueprintRawHTML::Page.new.to_html }
x.report("Blueprint::HTML #{Blueprint::VERSION}") { BlueprintHTML::Page.new.to_s }
x.report("Blueprint::RawHTML #{Blueprint::VERSION}") { BlueprintRawHTML::Page.new.to_s }
x.report("ECR") { ECR::Page.new.to_s }
end
12 changes: 6 additions & 6 deletions spec/blueprint/html/attributes_handling_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe "attributes handling" do
<div class="hello" id="first">Normal attributes</div>
HTML

page.to_html.should contain(div)
page.to_s.should contain(div)
end

it "converts attribute values to string" do
Expand All @@ -29,7 +29,7 @@ describe "attributes handling" do
<span id="421" float="2.4">Non-string attribute values</span>
HTML

page.to_html.should contain(span)
page.to_s.should contain(span)
end

it "replaces `_` by `-` on attribute names" do
Expand All @@ -38,7 +38,7 @@ describe "attributes handling" do
<section v-model="user.name" @click="doSomething">Transform attribute name</section>
HTML

page.to_html.should contain(section)
page.to_s.should contain(section)
end

it "accepts boolean attributes" do
Expand All @@ -47,7 +47,7 @@ describe "attributes handling" do
<input disabled outline="true" border="false">
HTML

page.to_html.should contain(input)
page.to_s.should contain(input)
end

it "expands nested attributes" do
Expand All @@ -56,7 +56,7 @@ describe "attributes handling" do
<nav aria-target="#home" aria-selected="false" aria-enabled>Nested attributes</nav>
HTML

page.to_html.should contain(nav)
page.to_s.should contain(nav)
end

it "flattens, compacts and joins array attributes" do
Expand All @@ -65,6 +65,6 @@ describe "attributes handling" do
<div class="a b c d">Array attributes</div>
HTML

page.to_html.should contain(nav)
page.to_s.should contain(nav)
end
end
2 changes: 1 addition & 1 deletion spec/blueprint/html/building_style_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe "style building" do
end

it "allows to build style inside components" do
actual_html = ButtonComponent.new.to_html { "Build Style!" }
actual_html = ButtonComponent.new.to_s { "Build Style!" }
expected_html = normalize_html <<-HTML
<a class="btn btn-xs btn-outline btn-blue">Build Style!</a>
HTML
Expand Down
6 changes: 3 additions & 3 deletions spec/blueprint/html/component_rendering_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe "component rendering" do
<header>Basic component</header>
HTML

page.to_html.should contain(basic_component)
page.to_s.should contain(basic_component)
end

it "can provide content to another blueprints" do
Expand All @@ -86,7 +86,7 @@ describe "component rendering" do
</div>
HTML

page.to_html.should contain(content_component)
page.to_s.should contain(content_component)
end

it "can use another blueprint methods" do
Expand All @@ -99,6 +99,6 @@ describe "component rendering" do
</div>
HTML

page.to_html.should contain(complex_component)
page.to_s.should contain(complex_component)
end
end
6 changes: 3 additions & 3 deletions spec/blueprint/html/components_registration_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe "components registration" do
<div id="required-block">Component with required block</div>
HTML

page.to_html.should contain expected_html
page.to_s.should contain expected_html
end

it "allows component helper definition without required block" do
Expand All @@ -69,7 +69,7 @@ describe "components registration" do
<h1>Component without block</h1>
HTML

page.to_html.should contain expected_html
page.to_s.should contain expected_html
end

it "allows component helper definition with optional block" do
Expand All @@ -79,6 +79,6 @@ describe "components registration" do
<div id="optional-block"></div>
HTML

page.to_html.should contain expected_html
page.to_s.should contain expected_html
end
end
6 changes: 3 additions & 3 deletions spec/blueprint/html/conditional_rendering_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ describe "conditional rendering" do
<div></div>
HTML

page.to_html.should eq expected_html
page.to_s.should eq expected_html
end
end

context "when blueprint `#render?` returns false" do
it "doesn't render the blueprint" do
page = NoRenderPage.new
page.to_html.should eq ""
page.to_s.should eq ""

page = NoRenderPage.new
html = page.to_html { "This page will not be rendered" }
html = page.to_s { "This page will not be rendered" }
html.should eq ""
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/blueprint/html/custom_elements_registration_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe "custom elements registration" do
<v-btn href="#home" data-id="12" data-visible>Home</v-btn>
HTML

page.to_html.should contain expected_html
page.to_s.should contain expected_html
end

it "allows passing content as first argument" do
Expand All @@ -32,7 +32,7 @@ describe "custom elements registration" do
<v-btn href="#contact">Contact</v-btn>
HTML

page.to_html.should contain expected_html
page.to_s.should contain expected_html
end

it "allows empty custom elements" do
Expand All @@ -41,7 +41,7 @@ describe "custom elements registration" do
<v-btn></v-btn>
HTML

page.to_html.should contain expected_html
page.to_s.should contain expected_html
end

it "allows defining custom tags" do
Expand All @@ -50,6 +50,6 @@ describe "custom elements registration" do
<MyCard></MyCard>
HTML

page.to_html.should contain expected_html
page.to_s.should contain expected_html
end
end
2 changes: 1 addition & 1 deletion spec/blueprint/html/enveloping_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ describe "enveloping" do
</html>
HTML

page.to_html.should eq expected_html
page.to_s.should eq expected_html
end
end
2 changes: 1 addition & 1 deletion spec/blueprint/html/helpers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe "helpers" do
<div class="a b c e">Tokens</div>
HTML

page.to_html.should contain expected_html
page.to_s.should contain expected_html
end
end
end
18 changes: 9 additions & 9 deletions spec/blueprint/html/safety_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe "safety" do
<span>&lt;script&gt;alert(&#39;hello&#39;)&lt;/script&gt;</span>
HTML

page.to_html.should contain(expected_html)
page.to_s.should contain(expected_html)
end

it "escapes content passed to tags via argument" do
Expand All @@ -42,7 +42,7 @@ describe "safety" do
<span>&lt;script&gt;alert(&#39;content&#39;)&lt;/script&gt;</span>
HTML

page.to_html.should contain(expected_html)
page.to_s.should contain(expected_html)
end

it "escapes plain text" do
Expand All @@ -51,7 +51,7 @@ describe "safety" do
&lt;script&gt;alert(&#39;Plain Text&#39;)&lt;/script&gt;
HTML

page.to_html.should contain(expected_html)
page.to_s.should contain(expected_html)
end

it "escapes content passed to blueprints" do
Expand All @@ -60,7 +60,7 @@ describe "safety" do
&lt;script&gt;alert(&#39;ExampleComponent&#39;)&lt;/script&gt;
HTML

page.to_html.should contain(expected_html)
page.to_s.should contain(expected_html)
end

it "escapes attribute values" do
Expand All @@ -69,7 +69,7 @@ describe "safety" do
<div class="some-class&quot; onblur=&quot;alert(&#39;Attribute&#39;)"></div>
HTML

page.to_html.should contain(expected_html)
page.to_s.should contain(expected_html)
end

it "escapes comment content passed via block" do
Expand All @@ -78,7 +78,7 @@ describe "safety" do
<!----&gt;&lt;script&gt;alert(&#39;Plain Text&#39;)&lt;/script&gt;&lt;!---->
HTML

page.to_html.should contain(expected_html)
page.to_s.should contain(expected_html)
end

it "escapes comment content passed via argument" do
Expand All @@ -87,7 +87,7 @@ describe "safety" do
<!----&gt;&lt;script&gt;alert(&#39;Another plain text&#39;)&lt;/script&gt;&lt;!---->
HTML

page.to_html.should contain(expected_html)
page.to_s.should contain(expected_html)
end

it "escapes custom tag content passed via argument" do
Expand All @@ -96,7 +96,7 @@ describe "safety" do
<v-btn>&lt;script&gt;alert(&#39;content&#39;)&lt;/script&gt;</v-btn>
HTML

page.to_html.should contain(expected_html)
page.to_s.should contain(expected_html)
end

it "escapes custom tag content passed via block" do
Expand All @@ -105,6 +105,6 @@ describe "safety" do
<v-btn class="some-class&quot; onclick=&quot;alert(&#39;Attribute&#39;)">&lt;script&gt;alert(&#39;hello&#39;)&lt;/script&gt;</v-btn>
HTML

page.to_html.should contain(expected_html)
page.to_s.should contain(expected_html)
end
end
2 changes: 1 addition & 1 deletion spec/blueprint/html/standard_elements_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ describe "standard HTML elements" do
io << "<select attribute=\"test\">content</select>"
end

page.to_html.should eq expected_html
page.to_s.should eq expected_html
end
end
4 changes: 2 additions & 2 deletions spec/blueprint/html/svg_rendering_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe "SVG rendering" do
</svg>
HTML

example.to_html.should eq expected_html
example.to_s.should eq expected_html
end

it "defines all SVG element helper methods" do
Expand All @@ -64,6 +64,6 @@ describe "SVG rendering" do
io << "</svg>"
end

page.to_html.should eq expected_html
page.to_s.should eq expected_html
end
end
16 changes: 8 additions & 8 deletions spec/blueprint/html/utils_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,57 +29,57 @@ describe "utils" do
it "renders plain text passed via argument" do
page = ExamplePage.new

page.to_html.should contain("<div>Hello<b>World</b></div>")
page.to_s.should contain("<div>Hello<b>World</b></div>")
end

it "renders plain text passed via block" do
page = ExamplePage.new

page.to_html.should contain("<span>Plain!</span>")
page.to_s.should contain("<span>Plain!</span>")
end
end

describe "#doctype" do
it "renders HTML 5 doctype declaration" do
page = ExamplePage.new

page.to_html.should contain("<!DOCTYPE html>")
page.to_s.should contain("<!DOCTYPE html>")
end
end

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

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

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

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

describe "#whitespace" do
it "renders an whitespace" do
page = ExamplePage.new

page.to_html.should contain("<i>Hi</i> User")
page.to_s.should contain("<i>Hi</i> User")
end
end

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

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

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

page.to_html.should contain("<div><script>Another dangerous script</script></div>")
page.to_s.should contain("<div><script>Another dangerous script</script></div>")
end
end
end
Loading

0 comments on commit 92157a4

Please sign in to comment.