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

Commit 5e8d9a1

Browse files
committed
Merge branch '0-8-stable' of https://github.com/reactrb/reactrb into 0-8-stable
2 parents 1db5906 + f631ae7 commit 5e8d9a1

File tree

3 files changed

+149
-121
lines changed

3 files changed

+149
-121
lines changed

lib/react/api.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def self.convert_props(properties)
108108
props["className"] = value
109109
elsif ["style", "dangerously_set_inner_HTML"].include? key
110110
props[lower_camelize(key)] = value.to_n
111+
elsif React::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
112+
value.each { |k, v| props["#{key}-#{k.tr('_', '-')}"] = v.to_n }
111113
else
112114
props[React::ATTRIBUTES.include?(lower_camelize(key)) ? lower_camelize(key) : key] = value
113115
end

lib/react/top_level.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module React
2222
readOnly rel required role rows rowSpan sandbox scope scrolling seamless
2323
selected shape size sizes span spellCheck src srcDoc srcSet start step style
2424
tabIndex target title type useMap value width wmode dangerouslySetInnerHTML)
25+
HASH_ATTRIBUTES = %w(data aria)
2526

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

spec/react/dsl_spec.rb

Lines changed: 146 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -2,166 +2,191 @@
22

33
if opal?
44

5-
module TestMod123
6-
class Bar < React::Component::Base
5+
module TestMod123
6+
class Bar < React::Component::Base
7+
end
78
end
8-
end
99

10-
describe 'the React DSL' do
11-
12-
it "will turn the last string in a block into a element" do
13-
stub_const 'Foo', Class.new
14-
Foo.class_eval do
15-
include React::Component
16-
def render
17-
div { "hello" }
10+
describe 'the React DSL' do
11+
it "will turn the last string in a block into a element" do
12+
stub_const 'Foo', Class.new
13+
Foo.class_eval do
14+
include React::Component
15+
def render
16+
div { "hello" }
17+
end
1818
end
19-
end
2019

21-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>hello</div>')
22-
end
20+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>hello</div>')
21+
end
2322

24-
it "has a .span short hand String method" do
25-
stub_const 'Foo', Class.new
26-
Foo.class_eval do
27-
include React::Component
28-
def render
29-
div { "hello".span; "goodby".span }
23+
it "has a .span short hand String method" do
24+
stub_const 'Foo', Class.new
25+
Foo.class_eval do
26+
include React::Component
27+
def render
28+
div { "hello".span; "goodby".span }
29+
end
3030
end
31-
end
3231

33-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div><span>hello</span><span>goodby</span></div>')
34-
end
32+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div><span>hello</span><span>goodby</span></div>')
33+
end
3534

36-
it "has a .br short hand String method" do
37-
stub_const 'Foo', Class.new
38-
Foo.class_eval do
39-
include React::Component
40-
def render
41-
div { "hello".br }
35+
it "has a .br short hand String method" do
36+
stub_const 'Foo', Class.new
37+
Foo.class_eval do
38+
include React::Component
39+
def render
40+
div { "hello".br }
41+
end
4242
end
43-
end
4443

45-
expect(React.render_to_static_markup(React.create_element(Foo)).gsub("<br/>", "<br>")).to eq('<div><span>hello<br></span></div>')
46-
end
44+
expect(React.render_to_static_markup(React.create_element(Foo)).gsub("<br/>", "<br>")).to eq('<div><span>hello<br></span></div>')
45+
end
4746

48-
it "has a .td short hand String method" do
49-
stub_const 'Foo', Class.new
50-
Foo.class_eval do
51-
include React::Component
52-
def render
53-
table { tr { "hello".td } }
47+
it "has a .td short hand String method" do
48+
stub_const 'Foo', Class.new
49+
Foo.class_eval do
50+
include React::Component
51+
def render
52+
table { tr { "hello".td } }
53+
end
5454
end
55-
end
5655

57-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<table><tr><td>hello</td></tr></table>')
58-
end
56+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<table><tr><td>hello</td></tr></table>')
57+
end
5958

60-
it "has a .para short hand String method" do
61-
stub_const 'Foo', Class.new
62-
Foo.class_eval do
63-
include React::Component
64-
def render
65-
div { "hello".para }
59+
it "has a .para short hand String method" do
60+
stub_const 'Foo', Class.new
61+
Foo.class_eval do
62+
include React::Component
63+
def render
64+
div { "hello".para }
65+
end
6666
end
67-
end
6867

69-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div><p>hello</p></div>')
70-
end
68+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div><p>hello</p></div>')
69+
end
7170

72-
it "will treat the component class name as a first class component name" do
73-
stub_const 'Mod::Bar', Class.new
74-
Mod::Bar.class_eval do
75-
include React::Component
76-
def render
77-
"a man walks into a bar"
71+
it "will treat the component class name as a first class component name" do
72+
stub_const 'Mod::Bar', Class.new
73+
Mod::Bar.class_eval do
74+
include React::Component
75+
def render
76+
"a man walks into a bar"
77+
end
7878
end
79-
end
80-
stub_const 'Foo', Class.new(React::Component::Base)
81-
Foo.class_eval do
82-
def render
83-
Mod::Bar()
79+
stub_const 'Foo', Class.new(React::Component::Base)
80+
Foo.class_eval do
81+
def render
82+
Mod::Bar()
83+
end
8484
end
85-
end
8685

87-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<span>a man walks into a bar</span>')
88-
end
86+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<span>a man walks into a bar</span>')
87+
end
8988

90-
it "can add class names by the haml .class notation" do
91-
# stub_const 'Mod::Barz', Class.new(React::Component::Base)
92-
TestMod123::Bar.class_eval do
93-
collect_other_params_as :attributes
94-
def render
95-
"a man walks into a bar".span(attributes)
89+
it "can add class names by the haml .class notation" do
90+
# stub_const 'Mod::Barz', Class.new(React::Component::Base)
91+
TestMod123::Bar.class_eval do
92+
collect_other_params_as :attributes
93+
def render
94+
"a man walks into a bar".span(attributes)
95+
end
9696
end
97-
end
98-
stub_const 'Foo', Class.new(React::Component::Base)
99-
Foo.class_eval do
100-
def render
101-
TestMod123::Bar().the_class
97+
stub_const 'Foo', Class.new(React::Component::Base)
98+
Foo.class_eval do
99+
def render
100+
TestMod123::Bar().the_class
101+
end
102102
end
103-
end
104103

105-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<span class="the-class">a man walks into a bar</span>')
106-
end
104+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<span class="the-class">a man walks into a bar</span>')
105+
end
107106

108-
it "can use the 'class' keyword for classes" do
109-
stub_const 'Foo', Class.new
110-
Foo.class_eval do
111-
include React::Component
112-
def render
113-
span(class: "the-class") { "hello" }
107+
it "can use the 'class' keyword for classes" do
108+
stub_const 'Foo', Class.new
109+
Foo.class_eval do
110+
include React::Component
111+
def render
112+
span(class: "the-class") { "hello" }
113+
end
114114
end
115-
end
116115

117-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<span class="the-class">hello</span>')
118-
end
116+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<span class="the-class">hello</span>')
117+
end
119118

120-
it "can generate a unrendered node using the .as_node method" do # div { "hello" }.as_node
121-
stub_const 'Foo', Class.new #(React::Component::Base)
122-
Foo.class_eval do
123-
include React::Component
124-
def render
125-
span { "hello".span.as_node.class.name }.as_node.render
119+
it "can generate a unrendered node using the .as_node method" do # div { "hello" }.as_node
120+
stub_const 'Foo', Class.new #(React::Component::Base)
121+
Foo.class_eval do
122+
include React::Component
123+
def render
124+
span { "hello".span.as_node.class.name }.as_node.render
125+
end
126126
end
127-
end
128127

129-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<span>React::Element</span>')
130-
end
128+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<span>React::Element</span>')
129+
end
131130

132-
it "can use the dangerously_set_inner_HTML param" do
133-
stub_const 'Foo', Class.new
134-
Foo.class_eval do
135-
include React::Component
136-
def render
137-
div(dangerously_set_inner_HTML: { __html: "Hello&nbsp;&nbsp;Goodby" })
131+
it "can use the dangerously_set_inner_HTML param" do
132+
stub_const 'Foo', Class.new
133+
Foo.class_eval do
134+
include React::Component
135+
def render
136+
div(dangerously_set_inner_HTML: { __html: "Hello&nbsp;&nbsp;Goodby" })
137+
end
138138
end
139-
end
140139

141-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>Hello&nbsp;&nbsp;Goodby</div>')
142-
end
140+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>Hello&nbsp;&nbsp;Goodby</div>')
141+
end
143142

144-
it "will remove all elements passed as params from the rendering buffer" do
145-
stub_const 'X2', Class.new
146-
X2.class_eval do
147-
include React::Component
148-
param :ele
149-
def render
150-
div do
151-
ele.render
152-
ele.render
143+
it 'should convert a hash param to hyphenated html attributes if in React::HASH_ATTRIBUTES' do
144+
stub_const 'Foo', Class.new
145+
Foo.class_eval do
146+
include React::Component
147+
def render
148+
div(data: { foo: :bar }, aria: { foo_bar: :foo })
153149
end
154150
end
151+
152+
expect(React.render_to_static_markup(React.create_element(Foo)))
153+
.to eq('<div data-foo="bar" aria-foo-bar="foo"></div>')
155154
end
156-
stub_const 'Test', Class.new
157-
Test.class_eval do
158-
include React::Component
159-
def render
160-
X2(ele: b { "hello" })
155+
156+
it 'should not convert a hash param to hyphenated html attributes if not in React::HASH_ATTRIBUTES' do
157+
stub_const 'Foo', Class.new
158+
Foo.class_eval do
159+
include React::Component
160+
def render
161+
div(title: { bar: :foo })
162+
end
161163
end
164+
165+
expect(React.render_to_static_markup(React.create_element(Foo)))
166+
.to eq('<div title="{&quot;bar&quot;=&gt;&quot;foo&quot;}"></div>')
162167
end
163168

164-
expect(React.render_to_static_markup(React.create_element(Test))).to eq('<div><b>hello</b><b>hello</b></div>')
169+
it "will remove all elements passed as params from the rendering buffer" do
170+
stub_const 'X2', Class.new
171+
X2.class_eval do
172+
include React::Component
173+
param :ele
174+
def render
175+
div do
176+
ele.render
177+
ele.render
178+
end
179+
end
180+
end
181+
stub_const 'Test', Class.new
182+
Test.class_eval do
183+
include React::Component
184+
def render
185+
X2(ele: b { "hello" })
186+
end
187+
end
188+
189+
expect(React.render_to_static_markup(React.create_element(Test))).to eq('<div><b>hello</b><b>hello</b></div>')
190+
end
165191
end
166192
end
167-
end

0 commit comments

Comments
 (0)