Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 4408438

Browse files
committed
Add SVG Support. closes #118. Thanks @jiayp!
1 parent ab2f972 commit 4408438

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

lib/react/api.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def self.create_element(type, properties = {}, &block)
7474
params << @@component_classes[type]
7575
elsif type.kind_of?(Class)
7676
params << create_native_react_class(type)
77-
elsif HTML_TAGS.include?(type)
77+
elsif React.html_tags?(type)
7878
params << type
7979
elsif type.is_a? String
8080
return React::Element.new(type)
@@ -109,7 +109,7 @@ def self.convert_props(properties)
109109
elsif ["style", "dangerously_set_inner_HTML"].include? key
110110
props[lower_camelize(key)] = value.to_n
111111
else
112-
props[React::ATTRIBUTES.include?(lower_camelize(key)) ? lower_camelize(key) : key] = value
112+
props[React.html_attrs?(lower_camelize(key)) ? lower_camelize(key) : key] = value
113113
end
114114
end
115115
props.shallow_to_n

lib/react/component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def method_missing(n, *args, &block)
196196
node_only = true
197197
name = name.gsub(/_as_node$/, "")
198198
end
199-
unless (HTML_TAGS.include?(name) || name == 'present' || name == '_p_tag' || (name = component?(name, self)))
199+
unless (React.html_tags?(name) || name == 'present' || name == '_p_tag' || (name = component?(name, self)))
200200
return super
201201
end
202202

lib/react/top_level.rb

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ module React
1010
main map mark menu menuitem meta meter nav noscript object ol optgroup option
1111
output p param picture pre progress q rp rt ruby s samp script section select
1212
small source span strong style sub summary sup table tbody td textarea tfoot th
13-
thead time title tr track u ul var video wbr)
13+
thead time title tr track u ul var video wbr) +
14+
# The SVG Tags
15+
%w(circle clipPath defs ellipse g line linearGradient mask path pattern polygon polyline
16+
radialGradient rect stop svg text tspan)
1417
ATTRIBUTES = %w(accept acceptCharset accessKey action allowFullScreen allowTransparency alt
1518
async autoComplete autoPlay cellPadding cellSpacing charSet checked classID
1619
className cols colSpan content contentEditable contextMenu controls coords
@@ -21,7 +24,38 @@ module React
2124
muted name noValidate open pattern placeholder poster preload radioGroup
2225
readOnly rel required role rows rowSpan sandbox scope scrolling seamless
2326
selected shape size sizes span spellCheck src srcDoc srcSet start step style
24-
tabIndex target title type useMap value width wmode dangerouslySetInnerHTML)
27+
tabIndex target title type useMap value width wmode dangerouslySetInnerHTML) +
28+
#SVG ATTRIBUTES
29+
%w(clipPath cx cy d dx dy fill fillOpacity fontFamily
30+
fontSize fx fy gradientTransform gradientUnits markerEnd
31+
markerMid markerStart offset opacity patternContentUnits
32+
patternUnits points preserveAspectRatio r rx ry spreadMethod
33+
stopColor stopOpacity stroke strokeDasharray strokeLinecap
34+
strokeOpacity strokeWidth textAnchor transform version
35+
viewBox x1 x2 x xlinkActuate xlinkArcrole xlinkHref xlinkRole
36+
xlinkShow xlinkTitle xlinkType xmlBase xmlLang xmlSpace y1 y2 y)
37+
38+
def self.html_tags?(name)
39+
tags = HTML_TAGS
40+
`
41+
for(var i = 0; i < tags.length; i++){
42+
if(tags[i] === name)
43+
return true;
44+
}
45+
return false;
46+
`
47+
end
48+
49+
def self.html_attrs?(name)
50+
attrs = ATTRIBUTES
51+
`
52+
for(var i = 0; i < attrs.length; i++){
53+
if(attrs[i] === name)
54+
return true;
55+
}
56+
return false;
57+
`
58+
end
2559

2660
def self.create_element(type, properties = {}, &block)
2761
React::API.create_element(type, properties, &block)

spec/react/top_level_component_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,25 @@ def render_top_level(controller, component_name)
6060
it 'can find the correct component when the name is fully qualified' do
6161
expect(render_top_level("Controller", "::Components::Component1")).to eq('<span>Components::Component1</span>')
6262
end
63+
64+
it 'React.html_tags? will return true for normal html tags' do
65+
expect(React.html_tags?('a')).to eq(true)
66+
expect(React.html_tags?('div')).to eq(true)
67+
end
68+
69+
it 'React.html_tags? will return true for svg element names' do
70+
expect(React.html_tags?('svg')).to eq(true)
71+
expect(React.html_tags?('circle')).to eq(true)
72+
end
73+
74+
it 'React.html_attrs? will return true for normal html attribute names' do
75+
expect(React.html_attrs?('id')).to eq(true)
76+
expect(React.html_attrs?('data')).to eq(true)
77+
end
78+
79+
it 'React.html_attrs? will return true for svg attribute names' do
80+
expect(React.html_attrs?('cx')).to eq(true)
81+
expect(React.html_attrs?('strokeWidth')).to eq(true)
82+
end
6383
end
6484
end

0 commit comments

Comments
 (0)