Skip to content

Commit 6daeb9d

Browse files
committed
added those forgotten files
1 parent 50710d0 commit 6daeb9d

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'hyperstack/internal/component/haml'
2+
# to allow for easier testing we include the internal mixins
3+
# from hyperstack/internal/component/haml
4+
# see spec/deprecated_features/haml_spec
5+
module Hyperstack
6+
module Internal
7+
module Component
8+
module Tags
9+
include HAMLTagInstanceMethods
10+
end
11+
end
12+
end
13+
module Component
14+
class Element
15+
include HAMLElementInstanceMethods
16+
end
17+
end
18+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'hyperstack/ext/component/string'
2+
# see hyperstack/component/haml for this is to be included.
3+
module Hyperstack
4+
module Internal
5+
module Component
6+
module HAMLTagInstanceMethods
7+
def self.included(base)
8+
base.const_get('HTML_TAGS').each do |tag|
9+
if tag == 'p'
10+
base.define_method(tag) do |*params, &children|
11+
if children || params.count == 0 || (params.count == 1 && params.first.is_a?(Hash))
12+
RenderingContext.render(tag, *params, &children)
13+
else
14+
Kernel.p(*params)
15+
end
16+
end
17+
else
18+
base.alias_method tag, tag.upcase
19+
end
20+
end
21+
end
22+
end
23+
24+
module HAMLElementInstanceMethods
25+
def method_missing(class_name, args = {}, &new_block)
26+
return dup.render.method_missing(class_name, args, &new_block) unless rendered?
27+
Hyperstack::Internal::Component::RenderingContext.replace(
28+
self,
29+
Hyperstack::Internal::Component::RenderingContext.build do
30+
Hyperstack::Internal::Component::RenderingContext.render(type, build_new_properties(class_name, args), &new_block)
31+
end
32+
)
33+
end
34+
35+
def rendered?
36+
Hyperstack::Internal::Component::RenderingContext.rendered? self
37+
end
38+
39+
def haml_class_name(class_name)
40+
class_name.gsub(/__|_/, '__' => '_', '_' => '-')
41+
end
42+
43+
private
44+
45+
def build_new_properties(class_name, args)
46+
class_name = haml_class_name(class_name)
47+
new_props = @properties.dup
48+
new_props[:className] = "\
49+
#{class_name} #{new_props[:className]} #{args.delete(:class)} #{args.delete(:className)}\
50+
".split(' ').uniq.join(' ')
51+
new_props.merge! args
52+
end
53+
end
54+
end
55+
end
56+
end
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
require 'spec_helper'
2+
3+
describe 'HAML notation', js: true do
4+
before(:each) do
5+
on_client do
6+
# simulate requiring 'hyperstack/component/haml'
7+
module Hyperstack::Internal::Component::Tags
8+
include Hyperstack::Internal::Component::HAMLTagInstanceMethods
9+
end
10+
class Hyperstack::Component::Element
11+
include Hyperstack::Internal::Component::HAMLElementInstanceMethods
12+
end
13+
end
14+
end
15+
it "can add class names by the haml .class notation" do
16+
mount 'Foo' do
17+
module Mod
18+
class Bar
19+
include Hyperstack::Component
20+
collect_other_params_as :attributes
21+
def render
22+
"a man walks into a bar".span(params.attributes)
23+
end
24+
end
25+
end
26+
class Foo < HyperComponent
27+
def render
28+
div.div_class do
29+
Mod::Bar().the_class.other_class
30+
end
31+
end
32+
end
33+
end
34+
expect(page.body).to include('<div class="div-class"><span class="other-class the-class">a man walks into a bar</span></div>')
35+
end
36+
37+
it 'redefines `p` to make method missing work' do
38+
mount 'Foo' do
39+
class Foo
40+
include Hyperstack::Component
41+
42+
def render
43+
DIV {
44+
p(class_name: 'foo')
45+
p {}
46+
div { 'lorem ipsum' }
47+
p(id: '10')
48+
}
49+
end
50+
end
51+
end
52+
expect(page.body).to include('<div><p class="foo"></p><p></p><div>lorem ipsum</div><p id="10"></p></div>')
53+
end
54+
55+
it 'only overrides `p` in render context' do
56+
mount 'Foo' do
57+
class Foo
58+
include Hyperstack::Component
59+
60+
def self.result
61+
@@result ||= 'ooopsy'
62+
end
63+
64+
def self.result_two
65+
@@result_two ||= 'ooopsy'
66+
end
67+
68+
before_mount do
69+
@@result = p 'first'
70+
end
71+
72+
after_mount do
73+
@@result_two = p 'second'
74+
end
75+
76+
def render
77+
p do
78+
'third'
79+
end
80+
end
81+
end
82+
end
83+
expect_evaluate_ruby('Kernel.p "first"').to eq('first')
84+
expect_evaluate_ruby('p "second"').to eq('second')
85+
expect_evaluate_ruby('Foo.result').to eq('first')
86+
expect_evaluate_ruby('Foo.result_two').to eq('second')
87+
expect(page.body[-40..-10]).to include("<p>third</p>")
88+
expect(page.body[-40..-10]).not_to include("<p>first</p>")
89+
end
90+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Unparser
2+
class Emitter
3+
# Emitter for send
4+
class Send < self
5+
def local_variable_clash?
6+
selector =~ /^[A-Z]/ || local_variable_scope.local_variable_defined_for_node?(node, selector)
7+
end
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)