deface 0.7.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,367 @@
1
+ require 'spec_helper'
2
+
3
+ class Dummy
4
+ extend Deface::Applicator::ClassMethods
5
+ extend Deface::Search::ClassMethods
6
+
7
+ def self.all
8
+ Rails.application.config.deface.overrides.all
9
+ end
10
+ end
11
+
12
+
13
+ def attributes_to_sorted_array(src)
14
+ Nokogiri::HTML::DocumentFragment.parse(src).children.first.attributes
15
+ end
16
+
17
+ module Deface
18
+ describe Applicator do
19
+ include_context "mock Rails.application"
20
+ before { Dummy.all.clear }
21
+
22
+ describe "with a single remove override defined" do
23
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove => "p", :text => "<h1>Argh!</h1>") }
24
+ let(:source) { "<p>test</p><%= raw(text) %>" }
25
+
26
+ it "should return modified source" do
27
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<%= raw(text) %>"
28
+ end
29
+
30
+ end
31
+
32
+ describe "with a single remove override with closing_selector defined" do
33
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove => "h1", :closing_selector => "h2") }
34
+ let(:source) { "<h2>I should be safe</h2><span>Before!</span><h1>start</h1><p>some junk</p><div>more junk</div><h2>end</h2><span>After!</span>" }
35
+
36
+ it "should return modified source" do
37
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<h2>I should be safe</h2><span>Before!</span><span>After!</span>"
38
+ end
39
+ end
40
+
41
+ describe "with a single replace override defined" do
42
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "p", :text => "<h1>Argh!</h1>") }
43
+ let(:source) { "<p>test</p>" }
44
+
45
+ it "should return modified source" do
46
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<h1>Argh!</h1>"
47
+ end
48
+ end
49
+
50
+ describe "with a single replace override with closing_selector defined" do
51
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :closing_selector => "h2", :text => "<span>Argh!</span>") }
52
+ let(:source) { "<h1>start</h1><p>some junk</p><div>more junk</div><h2>end</h2>" }
53
+
54
+ it "should return modified source" do
55
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<span>Argh!</span>"
56
+ end
57
+ end
58
+
59
+ describe "with a single replace_contents override defined" do
60
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace_contents => "p", :text => "<h1>Argh!</h1>") }
61
+ let(:source) { "<p><span>Hello</span>I am not a <em>pirate</em></p>" }
62
+
63
+ it "should return modified source" do
64
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<p><h1>Argh!</h1></p>"
65
+ end
66
+ end
67
+
68
+ describe "with a single replace_contents override with closing_selector defined" do
69
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace_contents => "h1", :closing_selector => "h2", :text => "<span>Argh!</span>") }
70
+ let(:source) { "<h1>start</h1><p>some junk</p><div>more junk</div><h2>end</h2>" }
71
+
72
+ it "should return modified source" do
73
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<h1>start</h1><span>Argh!</span><h2>end</h2>"
74
+ end
75
+ end
76
+
77
+ describe "with a single insert_after override defined" do
78
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "img.button", :text => "<% help %>") }
79
+ let(:source) { "<div><img class=\"button\" src=\"path/to/button.png\"></div>" }
80
+
81
+ it "should return modified source" do
82
+ Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "<div><img class=\"button\" src=\"path/to/button.png\"><% help %></div>"
83
+ end
84
+ end
85
+
86
+ describe "with a single insert_before override defined" do
87
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "ul li:last", :text => "<%= help %>") }
88
+ let(:source) { "<ul><li>first</li><li>second</li><li>third</li></ul>" }
89
+
90
+ it "should return modified source" do
91
+ Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "<ul><li>first</li><li>second</li><li>third</li><%= help %></ul>"
92
+ end
93
+ end
94
+
95
+ describe "with a single insert_top override defined" do
96
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_top => "ul", :text => "<li>me first</li>") }
97
+ let(:source) { "<ul><li>first</li><li>second</li><li>third</li></ul>" }
98
+
99
+ it "should return modified source" do
100
+ Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "<ul><li>me first</li><li>first</li><li>second</li><li>third</li></ul>"
101
+ end
102
+ end
103
+
104
+ describe "with a single insert_top override defined when targetted elemenet has no children" do
105
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_top => "ul", :text => "<li>first</li><li>second</li><li>third</li>") }
106
+ let(:source) { "<ul></ul>" }
107
+
108
+ it "should return modified source" do
109
+ Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "<ul><li>first</li><li>second</li><li>third</li></ul>"
110
+ end
111
+ end
112
+
113
+ describe "with a single insert_bottom override defined" do
114
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_bottom => "ul", :text => "<li>I'm always last</li>") }
115
+ let(:source) { "<ul><li>first</li><li>second</li><li>third</li></ul>" }
116
+
117
+ it "should return modified source" do
118
+ Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "<ul><li>first</li><li>second</li><li>third</li><li>I'm always last</li></ul>"
119
+ end
120
+ end
121
+
122
+ describe "with a single insert_bottom override defined when targetted elemenet has no children" do
123
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_bottom => "ul", :text => "<li>I'm always last</li>") }
124
+ let(:source) { "<ul></ul>" }
125
+
126
+ it "should return modified source" do
127
+ Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "<ul><li>I'm always last</li></ul>"
128
+ end
129
+ end
130
+
131
+ describe "with a single set_attributes override (containing only text) defined" do
132
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :set_attributes => 'img',
133
+ :attributes => {:class => 'pretty', :alt => 'something interesting'}) }
134
+ let(:source) { "<img class=\"button\" src=\"path/to/button.png\">" }
135
+
136
+ it "should return modified source" do
137
+ attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"}))
138
+
139
+ attrs["class"].value.should == "pretty"
140
+ attrs["alt"].value.should == "something interesting"
141
+ attrs["src"].value.should == "path/to/button.png"
142
+ end
143
+ end
144
+
145
+ describe "with a single set_attributes override (containing erb) defined" do
146
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :set_attributes => 'img',
147
+ :attributes => {:class => 'pretty', 'data-erb-alt' => '<%= something_interesting %>'}) }
148
+ let(:source) { "<img class=\"button\" src=\"path/to/button.png\">" }
149
+
150
+ it "should return modified source" do
151
+ attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"}))
152
+
153
+ attrs["class"].value.should == "pretty"
154
+ attrs["alt"].value.should == "<%= something_interesting %>"
155
+ attrs["src"].value.should == "path/to/button.png"
156
+ end
157
+ end
158
+
159
+ describe "with a single set_attributes override (containing erb) defined targetting an existing pseudo attribute" do
160
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :set_attributes => 'img',
161
+ :attributes => {:class => '<%= get_some_other_class %>', :alt => 'something interesting'}) }
162
+ let(:source) { "<img class=\"<%= get_class %>\" src=\"path/to/button.png\">" }
163
+
164
+ it "should return modified source" do
165
+ attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"}))
166
+
167
+ attrs["class"].value.should == "<%= get_some_other_class %>"
168
+ attrs["alt"].value.should == "something interesting"
169
+ attrs["src"].value.should == "path/to/button.png"
170
+ end
171
+ end
172
+
173
+ describe "with a single add_to_attributes override (containing only text) defined" do
174
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :add_to_attributes => 'img',
175
+ :attributes => {:class => 'pretty', :alt => 'something interesting'}) }
176
+ let(:source) { "<img class=\"button\" src=\"path/to/button.png\">" }
177
+
178
+ it "should return modified source" do
179
+ attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"}))
180
+
181
+ attrs["class"].value.should == "button pretty"
182
+ attrs["alt"].value.should == "something interesting"
183
+ end
184
+ end
185
+
186
+ describe "with a single add_to_attributes override (containing erb) defined" do
187
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :add_to_attributes => 'img',
188
+ :attributes => {:class => '<%= add_class %>'}) }
189
+ let(:source) { "<img class=\"button\" src=\"path/to/button.png\">" }
190
+
191
+ it "should return modified source" do
192
+ attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"}))
193
+
194
+ attrs["class"].value.should == "button <%= add_class %>"
195
+ attrs["src"].value.should == "path/to/button.png"
196
+ end
197
+ end
198
+
199
+ describe "with a single add_to_attributes override (containing erb) defined using pseudo attribute name" do
200
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :add_to_attributes => 'img',
201
+ :attributes => {'data-erb-class' => '<%= add_class %>'}) }
202
+ let(:source) { "<img class=\"button\" src=\"path/to/button.png\">" }
203
+
204
+ it "should return modified source" do
205
+ attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"}))
206
+
207
+ attrs["class"].value.should == "button <%= add_class %>"
208
+ attrs["src"].value.should == "path/to/button.png"
209
+ end
210
+ end
211
+
212
+ describe "with a single add_to_attributes override (containing erb) defined targetting an existing pseudo attribute" do
213
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :add_to_attributes => 'img',
214
+ :attributes => {:class => '<%= get_some_other_class %>'}) }
215
+ let(:source) { "<img class=\"<%= get_class %>\" src=\"path/to/button.png\">" }
216
+
217
+ it "should return modified source" do
218
+ attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"}))
219
+
220
+ attrs["class"].value.should == "<%= get_class %> <%= get_some_other_class %>"
221
+ attrs["src"].value.should == "path/to/button.png"
222
+ end
223
+ end
224
+
225
+ describe "with a single remove_from_attributes override (containing only text) defined" do
226
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img',
227
+ :attributes => {:class => 'pretty'}) }
228
+ let(:source) { "<img class=\"pretty button\" src=\"path/to/button.png\">" }
229
+
230
+ it "should return modified source" do
231
+ attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"}))
232
+
233
+ attrs["class"].value.should == "button"
234
+ end
235
+ end
236
+
237
+ describe "with a single remove_from_attributes override (containing erb) defined" do
238
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img',
239
+ :attributes => {:class => '<%= add_class %>'}) }
240
+ let(:source) { "<img class=\"button <%= add_class %>\" src=\"path/to/button.png\">" }
241
+
242
+ it "should return modified source" do
243
+ attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"}))
244
+
245
+ attrs["class"].value.should == "button"
246
+ attrs["src"].value.should == "path/to/button.png"
247
+ end
248
+ end
249
+
250
+ describe "with a single remove_from_attributes override (containing erb) defined using pseudo attribute name" do
251
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img',
252
+ :attributes => {'data-erb-class' => '<%= add_class %>'}) }
253
+ let(:source) { "<img class=\"button <%= add_class %>\" src=\"path/to/button.png\">" }
254
+
255
+ it "should return modified source" do
256
+ attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"}))
257
+
258
+ attrs["class"].value.should == "button"
259
+ attrs["src"].value.should == "path/to/button.png"
260
+ end
261
+ end
262
+
263
+ describe "with a single remove_from_attributes override (containing only text) defined where value is not present in attribute" do
264
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img',
265
+ :attributes => {:class => 'pretty'}) }
266
+ let(:source) { "<img class=\"button\" src=\"path/to/button.png\">" }
267
+
268
+ it "should return unmodified source" do
269
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<img class=\"button\" src=\"path/to/button.png\">"
270
+ end
271
+ end
272
+
273
+ describe "with a single remove_from_attributes override (containing only text) defined where value is not present in attribute" do
274
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img',
275
+ :attributes => {:class => 'pretty'}) }
276
+ let(:source) { "<img src=\"path/to/button.png\">" }
277
+
278
+ it "should return unmodified source" do
279
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<img src=\"path/to/button.png\">"
280
+ end
281
+ end
282
+
283
+ describe "with a single remove_from_attributes override (containing erb) defined targetting an existing pseudo attribute" do
284
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img',
285
+ :attributes => {:class => '<%= get_some_other_class %>'}) }
286
+ let(:source) { "<img class=\"<%= get_class %> <%= get_some_other_class %>\" src=\"path/to/button.png\">" }
287
+
288
+ it "should return modified source" do
289
+ attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"}))
290
+
291
+ attrs["class"].value.should == "<%= get_class %>"
292
+ attrs["src"].value.should == "path/to/button.png"
293
+ end
294
+ end
295
+
296
+ describe "with a single set_attributes override (containing a pseudo attribute with erb) defined targetting an existing pseudo attribute" do
297
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :set_attributes => 'img',
298
+ :attributes => {'class' => '<%= hello_world %>'}) }
299
+ let(:source) { "<div><img class=\"<%= hello_moon %>\" src=\"path/to/button.png\"></div>" }
300
+
301
+ it "should return modified source" do
302
+ Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "<div><img src=\"path/to/button.png\" class=\"<%= hello_world %>\"></div>"
303
+ end
304
+ end
305
+
306
+ describe "with a single html surround override defined" do
307
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :surround => "p", :text => "<h1>It's behind you!</h1><div><%= render_original %></div>") }
308
+ let(:source) { "<p>test</p>" }
309
+
310
+ it "should return modified source" do
311
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<h1>It's behind you!</h1><div><p>test</p></div>"
312
+ end
313
+ end
314
+
315
+ describe "with a single erb surround override defined" do
316
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :surround => "p", :text => "<% some_method('test') do %><%= render_original %><% end %>") }
317
+ let(:source) { "<span><p>test</p></span>" }
318
+
319
+ it "should return modified source" do
320
+ Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", '').should == "<span><% some_method('test') do %><p>test</p><% end %></span>"
321
+ end
322
+ end
323
+
324
+ describe "with a single html surround_contents override defined" do
325
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :surround_contents => "div", :text => "<span><%= render_original %></span>") }
326
+ let(:source) { "<h4>yay!</h4><div><p>test</p></div>" }
327
+
328
+ it "should return modified source" do
329
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<h4>yay!</h4><div><span><p>test</p></span></div>"
330
+ end
331
+ end
332
+
333
+ describe "with a single erb surround_contents override defined" do
334
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :surround_contents => "p", :text => "<% if 1==1 %><%= render_original %><% end %>") }
335
+ let(:source) { "<p>test</p>" }
336
+
337
+ it "should return modified source" do
338
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<p><% if 1==1 %>test<% end %></p>"
339
+ end
340
+ end
341
+
342
+ describe "with a single disabled override defined" do
343
+ before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove => "p", :text => "<h1>Argh!</h1>", :disabled => true) }
344
+ let(:source) { "<p>test</p><%= raw(text) %>" }
345
+
346
+
347
+ it "should return unmodified source" do
348
+ Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<p>test</p><%= raw(text) %>"
349
+ end
350
+ end
351
+
352
+ describe "with mulitple sequenced overrides defined" do
353
+ before do
354
+ Deface::Override.new(:virtual_path => "posts/index", :name => "third", :insert_after => "li:contains('second')", :text => "<li>third</li>", :sequence => {:after => "second"})
355
+ Deface::Override.new(:virtual_path => "posts/index", :name => "second", :insert_after => "li", :text => "<li>second</li>", :sequence => {:after => "first"})
356
+ Deface::Override.new(:virtual_path => "posts/index", :name => "first", :replace => "li", :text => "<li>first</li>")
357
+ end
358
+
359
+ let(:source) { "<ul><li>replaced</li></ul>" }
360
+
361
+ it "should return modified source" do
362
+ Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "<ul><li>first</li><li>second</li><li>third</li></ul>"
363
+ end
364
+ end
365
+
366
+ end
367
+ end
@@ -4,7 +4,7 @@ module Deface
4
4
  describe Environment do
5
5
  include_context "mock Rails"
6
6
 
7
- before(:each) do
7
+ before(:each) do
8
8
  #declare this override (early) before Rails.application.deface is present
9
9
  silence_warnings do
10
10
  Deface::Override._early.clear
@@ -23,7 +23,7 @@ module Deface
23
23
 
24
24
  it "should return all overrides" do
25
25
  Rails.application.config.deface.overrides.all.size.should == 2
26
- Rails.application.config.deface.overrides.all.should == Deface::Override.all
26
+ Rails.application.config.deface.overrides.all.should == Deface::Override.all
27
27
  end
28
28
 
29
29
  it "should find overrides" do
@@ -35,7 +35,7 @@ module Deface
35
35
  before do
36
36
  Rails.application.stub :root => Pathname.new(File.join(File.dirname(__FILE__), '..', "assets"))
37
37
  Rails.application.stub :paths => {}
38
- Rails.application.stub_chain :railties, :all => []
38
+ Rails.application.stub_chain :railties, :all => []
39
39
  end
40
40
 
41
41
  it "should enumerate_and_load nil when app has no app/overrides path set" do
@@ -74,6 +74,11 @@ module Deface
74
74
  Rails.application.config.deface.overrides.load_all(Rails.application)
75
75
  end
76
76
 
77
+ it "should clear any previously loaded overrides" do
78
+ Rails.application.config.deface.overrides.all.should_receive(:clear)
79
+ Rails.application.config.deface.overrides.load_all(Rails.application)
80
+ end
81
+
77
82
  end
78
83
 
79
84
  describe "enumerate_and_load" do
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ module Deface
4
+ describe HamlConverter do
5
+ include_context "mock Rails.application"
6
+
7
+ def haml_to_erb(src)
8
+ haml_engine = Deface::HamlConverter.new(src)
9
+ haml_engine.render.gsub("\n", "")
10
+ end
11
+
12
+ describe "convert haml to erb" do
13
+ it "should hanlde simple tags" do
14
+ haml_to_erb("%%strong.code#message Hello, World!").should == "<strong class='code' id='message'>Hello, World!</strong>"
15
+ end
16
+
17
+ it "should handle complex tags" do
18
+ haml_to_erb(%q{#content
19
+ .left.column
20
+ %h2 Welcome to our site!
21
+ %p= print_information
22
+ .right.column
23
+ = render :partial => "sidebar"}).should == "<div id='content'> <div class='left column'> <h2>Welcome to our site!</h2> <p> <%= print_information %></p> </div> <div class='right column'> <%= render :partial => \"sidebar\" %> </div></div>"
24
+ end
25
+
26
+ it "should handle erb loud" do
27
+ haml_to_erb("%h3.title= entry.title").should == "<h3 class='title'><%= entry.title %></h3>"
28
+ end
29
+
30
+ it "should handle single erb silent" do
31
+ haml_to_erb("- some_method").should == "<% some_method %>"
32
+ end
33
+
34
+ it "should handle implicitly closed erb loud" do
35
+ haml_to_erb("= if @this == 'this'
36
+ %p hello
37
+ ").should == "<%= if @this == 'this' %><p>hello</p><% end %>"
38
+ end
39
+
40
+ it "should handle implicitly closed erb silent" do
41
+ haml_to_erb("- if foo?
42
+ %p hello
43
+ ").should == "<% if foo? %><p>hello</p><% end %>"
44
+ end
45
+
46
+ it "should handle blocks passed to erb loud" do
47
+ haml_to_erb("= form_for Post.new do |f|
48
+ %p
49
+ = f.text_field :name").should == "<%= form_for Post.new do |f| %><p> <%= f.text_field :name %></p><% end %>"
50
+
51
+ end
52
+
53
+
54
+ it "should handle blocks passed to erb silent" do
55
+ haml_to_erb("- @posts.each do |post|
56
+ %p
57
+ = post.name").should == "<% @posts.each do |post| %><p> <%= post.name %></p><% end %>"
58
+
59
+ end
60
+ end
61
+ end
62
+ end