-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require "../../spec_helper" | ||
|
||
private class Example | ||
include Blueprint::HTML | ||
|
||
private def blueprint | ||
svg width: 30, height: 10 do | ||
g fill: :red do | ||
rect x: 0, y: 0, width: 10, height: 10 | ||
rect x: 20, y: 0, width: 10, height: 10 | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe Blueprint::HTML do | ||
it "allows SVG rendering" do | ||
example = Example.new | ||
|
||
example.to_html.should eq <<-HTML.strip.gsub(/\R\s+/, "") | ||
<svg width="30" height="10"> | ||
<g fill="red"> | ||
<rect x="0" y="0" width="10" height="10"></rect> | ||
<rect x="20" y="0" width="10" height="10"></rect> | ||
</g> | ||
</svg> | ||
HTML | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require "../../spec_helper" | ||
|
||
private ELEMENTS = %w(a animate animateMotion animateTransform circle clipPath defs desc discard ellipse feBlend | ||
feColorMatrix feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting feDisplacementMap feDistantLight | ||
feDropShadow feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge feMergeNode feMorphology feOffset | ||
fePointLight feSpecularLighting feSpotLight feTile feTurbulence filter foreignObject g image line linearGradient | ||
marker mask metadata mpath path pattern polygon polyline radialGradient rect script set stop style svg switch symbol | ||
text textPath title tspan use view | ||
) | ||
|
||
private class DummyPage | ||
include Blueprint::HTML | ||
|
||
private def blueprint | ||
svg do | ||
{% for element in ELEMENTS %} | ||
{{element.id}} | ||
{{element.id}}(attribute: "test") | ||
{{element.id}} { "content" } | ||
{{element.id}}(attribute: "test") { "content" } | ||
{% end %} | ||
end | ||
end | ||
end | ||
|
||
describe "Bluprint::SVG::Component" do | ||
it "defines all SVG element helper methods" do | ||
page = DummyPage.new | ||
expected_html = String.build do |io| | ||
io << "<svg>" | ||
ELEMENTS.each do |tag| | ||
io << "<" << tag << ">" << "</" << tag << ">" | ||
io << "<" << tag << " attribute=\"test\">" << "</" << tag << ">" | ||
io << "<" << tag << ">content" << "</" << tag << ">" | ||
io << "<" << tag << " attribute=\"test\">content" << "</" << tag << ">" | ||
end | ||
io << "</svg>" | ||
end | ||
|
||
page.to_html.should eq expected_html | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "../svg/component" | ||
|
||
module Blueprint::HTML | ||
def svg(**attributes, &) | ||
render Blueprint::SVG::Component.new(**attributes) do |component| | ||
with component yield | ||
end | ||
end | ||
|
||
def svg(**attributes) | ||
svg(**attributes) { } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
struct Blueprint::SVG::Component(T) | ||
include Blueprint::HTML | ||
|
||
@attributes : T | ||
|
||
def self.new(**kwargs) | ||
new kwargs | ||
end | ||
|
||
{% for tag in %w(a animate animateMotion animateTransform circle clipPath defs desc discard ellipse feBlend feColorMatrix feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting feDisplacementMap feDistantLight feDropShadow feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge feMergeNode feMorphology feOffset fePointLight feSpecularLighting feSpotLight feTile feTurbulence filter foreignObject g image line linearGradient marker mask metadata mpath path pattern polygon polyline radialGradient rect script set stop style svg switch symbol text textPath title tspan use view) %} | ||
register_element {{tag}} | ||
{% end %} | ||
|
||
def initialize(attributes : T) | ||
{% T.raise "Expected T be NamedTuple, but got #{T}." unless T <= NamedTuple %} | ||
@attributes = attributes | ||
end | ||
|
||
def blueprint(&) | ||
element :svg, **@attributes do | ||
yield | ||
end | ||
end | ||
end |