deface 1.0.0.rc1 → 1.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +9 -1
- data/Gemfile +2 -1
- data/README.markdown +11 -9
- data/deface.gemspec +3 -2
- data/gemfiles/haml4_0.gemfile +5 -0
- data/gemfiles/rails3_1.gemfile +2 -2
- data/gemfiles/rails3_2.gemfile +2 -2
- data/lib/deface.rb +1 -0
- data/lib/deface/applicator.rb +3 -3
- data/lib/deface/dsl/loader.rb +2 -1
- data/lib/deface/generators +0 -0
- data/lib/deface/parser.rb +5 -4
- data/lib/deface/precompiler.rb +33 -0
- data/lib/deface/template_helper.rb +1 -1
- data/lib/generators/deface/USAGE +8 -0
- data/lib/generators/deface/override_generator.rb +16 -0
- data/lib/generators/deface/templates/override.html.erb.deface +2 -0
- data/lib/generators/deface/templates/override.html.haml.deface +3 -0
- data/spec/assets/dummy_app/overrides/posts/precompileme/precompileme.html.erb.deface +2 -0
- data/spec/assets/posts/precompileme.html.erb +2 -0
- data/spec/deface/actions/set_attributes_spec.rb +4 -1
- data/spec/deface/dsl/loader_spec.rb +22 -2
- data/spec/deface/override_spec.rb +48 -11
- data/spec/deface/parser_spec.rb +73 -17
- data/spec/deface/precompiler_spec.rb +42 -0
- data/spec/deface/template_helper_spec.rb +2 -2
- data/spec/generators/deface/override_generator_spec.rb +26 -0
- data/spec/spec_helper.rb +16 -1
- data/tasks/precompile.rake +1 -26
- metadata +239 -210
- data/gemfiles/haml3_2.gemfile +0 -5
data/.travis.yml
CHANGED
@@ -3,7 +3,15 @@ rvm:
|
|
3
3
|
- 1.8.7
|
4
4
|
- 1.9.2
|
5
5
|
- 1.9.3
|
6
|
+
- 2.0.0
|
7
|
+
- jruby-18mode
|
8
|
+
- jruby-19mode
|
6
9
|
gemfile:
|
7
10
|
- gemfiles/rails3_1.gemfile
|
8
11
|
- gemfiles/rails3_2.gemfile
|
9
|
-
- gemfiles/
|
12
|
+
- gemfiles/haml4_0.gemfile
|
13
|
+
|
14
|
+
notifications:
|
15
|
+
email:
|
16
|
+
- brian@spreecommerce.com
|
17
|
+
- ryan@spreecommerce.com
|
data/Gemfile
CHANGED
data/README.markdown
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
<p style="float:right;">
|
2
|
-
<a href="http://travis-ci.org
|
3
|
-
<img src="
|
2
|
+
<a href="http://secure.travis-ci.org/spree/deface">
|
3
|
+
<img src="https://secure.travis-ci.org/spree/deface.png?branch=master">
|
4
4
|
</a>
|
5
5
|
</p>
|
6
6
|
|
@@ -185,9 +185,9 @@ You can redefine an existing override by simply declaring a new override with th
|
|
185
185
|
You do not need to resupply all the values originally used, just the ones you want to change:
|
186
186
|
|
187
187
|
```ruby
|
188
|
-
|
189
|
-
|
190
|
-
|
188
|
+
Deface::Override.new(:virtual_path => 'posts/index',
|
189
|
+
:name => 'add_attrs_to_a_link',
|
190
|
+
:disabled => true)
|
191
191
|
```
|
192
192
|
|
193
193
|
### Namespacing
|
@@ -196,9 +196,9 @@ If you want to avoid inadvertently redefining overrides in other engines, you ca
|
|
196
196
|
an override automatically be namespaced to the engine in which it was defined:
|
197
197
|
|
198
198
|
```ruby
|
199
|
-
|
200
|
-
|
201
|
-
|
199
|
+
Deface::Override.new(:virtual_path => 'posts/index',
|
200
|
+
:name => 'add_link',
|
201
|
+
:namespaced => true)
|
202
202
|
```
|
203
203
|
|
204
204
|
So for example if the above override was defined in `MyEngine` it would be automatically named `my_engine_add_link`.
|
@@ -327,7 +327,9 @@ Deface now supports precompiling where all overrides are loaded and applied to t
|
|
327
327
|
|
328
328
|
It's important to disable Deface once precompiling is used to prevent overrides getting applied twice. To disable add the following line to your application's `production.rb` file:
|
329
329
|
|
330
|
-
|
330
|
+
```ruby
|
331
|
+
config.deface.enabled = false
|
332
|
+
```
|
331
333
|
|
332
334
|
NOTE: You can also use precompiling in development mode.
|
333
335
|
|
data/deface.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "deface"
|
3
|
-
s.version = "1.0.0.
|
3
|
+
s.version = "1.0.0.rc2"
|
4
4
|
|
5
5
|
s.authors = ["Brian D Quinn"]
|
6
6
|
s.description = "Deface is a library that allows you to customize ERB & HAML views in a Rails application without editing the underlying view."
|
@@ -15,11 +15,12 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.require_paths = ["lib"]
|
16
16
|
s.summary = "Deface is a library that allows you to customize ERB & HAML views in Rails"
|
17
17
|
|
18
|
-
s.add_dependency('nokogiri', '~> 1.5.
|
18
|
+
s.add_dependency('nokogiri', '~> 1.5.9')
|
19
19
|
s.add_dependency('rails', '~> 3.1')
|
20
20
|
s.add_dependency('colorize', '>= 0.5.8')
|
21
21
|
|
22
22
|
s.add_development_dependency('rspec', '>= 2.11.0')
|
23
23
|
s.add_development_dependency('haml', '>= 3.1.4')
|
24
24
|
s.add_development_dependency('simplecov', '>= 0.6.4')
|
25
|
+
s.add_development_dependency('generator_spec', '~> 0.8.5')
|
25
26
|
end
|
data/gemfiles/rails3_1.gemfile
CHANGED
data/gemfiles/rails3_2.gemfile
CHANGED
data/lib/deface.rb
CHANGED
data/lib/deface/applicator.rb
CHANGED
@@ -7,7 +7,7 @@ module Deface
|
|
7
7
|
overrides = find(details)
|
8
8
|
|
9
9
|
if log && overrides.size > 0
|
10
|
-
Rails.logger.
|
10
|
+
Rails.logger.debug "\e[1;32mDeface:\e[0m #{overrides.size} overrides found for '#{details[:virtual_path]}'"
|
11
11
|
end
|
12
12
|
|
13
13
|
unless overrides.empty?
|
@@ -20,7 +20,7 @@ module Deface
|
|
20
20
|
|
21
21
|
overrides.each do |override|
|
22
22
|
if override.disabled?
|
23
|
-
Rails.logger.
|
23
|
+
Rails.logger.debug("\e[1;32mDeface:\e[0m '#{override.name}' is disabled") if log
|
24
24
|
next
|
25
25
|
end
|
26
26
|
|
@@ -28,7 +28,7 @@ module Deface
|
|
28
28
|
matches = override.matcher.matches(doc, log)
|
29
29
|
|
30
30
|
if log
|
31
|
-
Rails.logger.send(matches.size == 0 ? :error : :
|
31
|
+
Rails.logger.send(matches.size == 0 ? :error : :debug, "\e[1;32mDeface:\e[0m '#{override.name}' matched #{matches.size} times with '#{override.selector}'")
|
32
32
|
end
|
33
33
|
|
34
34
|
if matches.empty?
|
data/lib/deface/dsl/loader.rb
CHANGED
@@ -62,7 +62,8 @@ module Deface
|
|
62
62
|
end
|
63
63
|
|
64
64
|
comment.gsub('<!--', '').gsub('-->', '').strip.scan(/[^\s"']+|"[^"]*"|'[^']*'/).each do |part|
|
65
|
-
|
65
|
+
|
66
|
+
dsl_commands =~ /('|")\z/ || part =~ /\A[^\d:='"%]/ ? dsl_commands << "\n" : dsl_commands << ' '
|
66
67
|
dsl_commands << part
|
67
68
|
end
|
68
69
|
|
File without changes
|
data/lib/deface/parser.rb
CHANGED
@@ -24,7 +24,6 @@ module Deface
|
|
24
24
|
replace_line
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
27
|
i = -1
|
29
28
|
#catch all <% %> inside tags id <p <%= test %>> , not inside attrs
|
30
29
|
replace_line.scan(/(<%.*?%>)/m).each do |match|
|
@@ -51,9 +50,11 @@ module Deface
|
|
51
50
|
# undoes ERB markup generated by Deface::Parser::ERB
|
52
51
|
#
|
53
52
|
def self.undo_erb_markup!(source)
|
54
|
-
replacements = [ {"<code erb-silent>"
|
55
|
-
{"<code erb-
|
56
|
-
{"
|
53
|
+
replacements = [ {"<code erb-silent>" => '<%'},
|
54
|
+
{"<code erb-silent=\"\">" => '<%'},
|
55
|
+
{"<code erb-loud>" => '<%='},
|
56
|
+
{"<code erb-loud=\"\">" => '<%='},
|
57
|
+
{"</code>" => '%>'}]
|
57
58
|
|
58
59
|
replacements.each{ |h| h.each { |replace, with| source.gsub! replace, with } }
|
59
60
|
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Deface
|
2
|
+
class Precompiler
|
3
|
+
|
4
|
+
extend Deface::TemplateHelper
|
5
|
+
|
6
|
+
def self.precompile
|
7
|
+
base_path = Rails.root.join("app/compiled_views")
|
8
|
+
# temporarily configures deface env and loads
|
9
|
+
# all overrides so we can precompile
|
10
|
+
unless Rails.application.config.deface.enabled
|
11
|
+
Rails.application.config.deface = Deface::Environment.new
|
12
|
+
Rails.application.config.deface.overrides.early_check
|
13
|
+
Rails.application.config.deface.overrides.load_all Rails.application
|
14
|
+
end
|
15
|
+
|
16
|
+
Rails.application.config.deface.overrides.all.each do |virtual_path,overrides|
|
17
|
+
template_path = base_path.join( "#{virtual_path}.html.erb")
|
18
|
+
|
19
|
+
FileUtils.mkdir_p template_path.dirname
|
20
|
+
begin
|
21
|
+
source = load_template_source(virtual_path.to_s, false, true)
|
22
|
+
if source.blank?
|
23
|
+
raise "Compiled source was blank for '#{virtual_path}'"
|
24
|
+
end
|
25
|
+
File.open(template_path, 'w') {|f| f.write source }
|
26
|
+
rescue Exception => e
|
27
|
+
puts "Unable to precompile '#{virtual_path}' due to: "
|
28
|
+
puts e.message
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Deface
|
2
2
|
module TemplateHelper
|
3
3
|
|
4
|
-
# used to find source for a partial or template using
|
4
|
+
# used to find source for a partial or template using virtual_path
|
5
5
|
def load_template_source(virtual_path, partial, apply_overrides=true)
|
6
6
|
parts = virtual_path.split("/")
|
7
7
|
prefix = []
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Deface
|
2
|
+
module Generators
|
3
|
+
class OverrideGenerator < Rails::Generators::Base
|
4
|
+
desc "Generates deface overrides"
|
5
|
+
source_root File.expand_path("../templates", __FILE__)
|
6
|
+
class_option :template_engine, :desc => 'Template engine to be invoked (erb or haml).'
|
7
|
+
argument :view, :type => :string
|
8
|
+
argument :name, :type => :string, :default => 'override'
|
9
|
+
|
10
|
+
def copy_template
|
11
|
+
engine = options[:template_engine]
|
12
|
+
copy_file "override.html.#{engine}.deface", "app/overrides/#{view}/#{name}.html.#{engine}.deface"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -54,7 +54,10 @@ module Deface
|
|
54
54
|
let(:source) { "<div><img class=\"<%= hello_moon %>\" src=\"path/to/button.png\"></div>" }
|
55
55
|
|
56
56
|
it "should return modified source" do
|
57
|
-
Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "")
|
57
|
+
tag = Nokogiri::HTML::DocumentFragment.parse(Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", ""))
|
58
|
+
tag = tag.css('img').first
|
59
|
+
tag.attributes['src'].value.should eq "path/to/button.png"
|
60
|
+
tag.attributes['class'].value.should eq "<%= hello_world %>"
|
58
61
|
end
|
59
62
|
end
|
60
63
|
end
|
@@ -53,7 +53,7 @@ describe Deface::DSL::Loader do
|
|
53
53
|
lambda { Deface::DSL::Loader.load(filename) }.should raise_error(
|
54
54
|
"Deface::DSL overrides must be in a sub-directory that matches the views virtual path. Move 'app/overrides/example_name.html.erb.deface' into a sub-directory.")
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
it 'should set the virtual_path for a .deface file in a directory below overrides' do
|
58
58
|
file = mock('deface file')
|
59
59
|
filename = 'app/overrides/path/to/view/example_name.deface'
|
@@ -177,6 +177,26 @@ describe Deface::DSL::Loader do
|
|
177
177
|
the_rest.should == "<h1>Wow!</h1>"
|
178
178
|
end
|
179
179
|
|
180
|
+
it 'should work with multiple commands on one line when command argument is not a normal string' do
|
181
|
+
example = %q{<!-- replace_contents 'h1 .title' closing_selector %q{div#intro} disabled namespaced --><h1>Wow!</h1>}
|
182
|
+
dsl_commands, the_rest = Deface::DSL::Loader.extract_dsl_commands_from_erb(example)
|
183
|
+
dsl_commands.should == "\nreplace_contents 'h1 .title'\nclosing_selector %q{div#intro}\ndisabled\nnamespaced"
|
184
|
+
the_rest.should == "<h1>Wow!</h1>"
|
185
|
+
end
|
186
|
+
|
187
|
+
it 'should work with multiple commands on one line when command argument is an integer' do
|
188
|
+
example = %q{<!-- replace_contents 'h1 .title' disabled sequence 2 namespaced --><h1>Wow!</h1>}
|
189
|
+
dsl_commands, the_rest = Deface::DSL::Loader.extract_dsl_commands_from_erb(example)
|
190
|
+
dsl_commands.should == "\nreplace_contents 'h1 .title'\ndisabled\nsequence 2\nnamespaced"
|
191
|
+
the_rest.should == "<h1>Wow!</h1>"
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'should work with multiple commands on one line when command argument is a hash' do
|
195
|
+
example = %q{<!-- add_to_attributes 'h1 .title' attributes :class => 'pretty'--><h1>Wow!</h1>}
|
196
|
+
dsl_commands, the_rest = Deface::DSL::Loader.extract_dsl_commands_from_erb(example)
|
197
|
+
dsl_commands.should == "\nadd_to_attributes 'h1 .title'\nattributes :class => 'pretty'"
|
198
|
+
the_rest.should == "<h1>Wow!</h1>"
|
199
|
+
end
|
180
200
|
end
|
181
201
|
|
182
202
|
context '.extract_dsl_commands_from_haml' do
|
@@ -202,4 +222,4 @@ describe Deface::DSL::Loader do
|
|
202
222
|
end
|
203
223
|
|
204
224
|
end
|
205
|
-
end
|
225
|
+
end
|
@@ -36,7 +36,12 @@ module Deface
|
|
36
36
|
it "should return parsed nokogiri document when present" do
|
37
37
|
@original = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "<h1>Argh!</h1>", :original => "<p><%= something %></p>")
|
38
38
|
@original.original_source.should be_an_instance_of Nokogiri::HTML::DocumentFragment
|
39
|
-
|
39
|
+
|
40
|
+
if RUBY_PLATFORM == 'java'
|
41
|
+
@original.original_source.to_s.should == "<p><code erb-loud=\"\"> something </code></p>"
|
42
|
+
else
|
43
|
+
@original.original_source.to_s.should == "<p><code erb-loud> something </code></p>"
|
44
|
+
end
|
40
45
|
end
|
41
46
|
end
|
42
47
|
|
@@ -58,8 +63,13 @@ module Deface
|
|
58
63
|
end
|
59
64
|
|
60
65
|
it "should return true when input contains similar (ignoring whitespace)" do
|
61
|
-
|
62
|
-
|
66
|
+
if RUBY_PLATFORM == 'java'
|
67
|
+
@original.validate_original("<p><code erb-loud=\"\"> something </code></p>").should be_true
|
68
|
+
@original.validate_original("<p><code erb-loud=\"\">something\n</code> </p>").should be_true
|
69
|
+
else
|
70
|
+
@original.validate_original("<p><code erb-loud> something </code></p>").should be_true
|
71
|
+
@original.validate_original("<p><code erb-loud>something\n</code> </p>").should be_true
|
72
|
+
end
|
63
73
|
end
|
64
74
|
|
65
75
|
it "should return false when and input contains different string" do
|
@@ -163,13 +173,18 @@ module Deface
|
|
163
173
|
|
164
174
|
it "should not change original parsed source" do
|
165
175
|
@override.source
|
166
|
-
|
176
|
+
|
177
|
+
if RUBY_PLATFORM == 'java'
|
178
|
+
parsed.to_s.gsub(/\n/,'').should == "<div><h1>Manage Posts</h1><code erb-loud=\"\"> some_method </code></div>"
|
179
|
+
else
|
180
|
+
parsed.to_s.gsub(/\n/,'').should == "<div><h1>Manage Posts</h1><code erb-loud> some_method </code></div>"
|
181
|
+
end
|
167
182
|
|
168
183
|
@override.source_argument.should == :copy
|
169
184
|
end
|
170
185
|
|
171
186
|
it "should return copy of content from source document" do
|
172
|
-
@override.source.should == "<h1>Manage Posts</h1
|
187
|
+
@override.source.to_s.strip.should == "<h1>Manage Posts</h1>"
|
173
188
|
end
|
174
189
|
|
175
190
|
it "should return unescaped content for source document" do
|
@@ -193,7 +208,12 @@ module Deface
|
|
193
208
|
|
194
209
|
it "should not change original parsed source" do
|
195
210
|
@override.source
|
196
|
-
|
211
|
+
|
212
|
+
if RUBY_PLATFORM == 'java'
|
213
|
+
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><code erb-silent=\"\"> if true </code><p>True that!</p><code erb-silent=\"\"> end </code><p>Hello</p>"
|
214
|
+
else
|
215
|
+
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><code erb-silent> if true </code><p>True that!</p><code erb-silent> end </code><p>Hello</p>"
|
216
|
+
end
|
197
217
|
|
198
218
|
@override.source_argument.should == :copy
|
199
219
|
end
|
@@ -212,7 +232,11 @@ module Deface
|
|
212
232
|
|
213
233
|
it "should remove cut element from original parsed source" do
|
214
234
|
@override.source
|
215
|
-
|
235
|
+
if RUBY_PLATFORM == 'java'
|
236
|
+
parsed.to_s.gsub(/\n/,'').should == "<div><code erb-loud=\"\"> some_method </code></div>"
|
237
|
+
else
|
238
|
+
parsed.to_s.gsub(/\n/,'').should == "<div><code erb-loud> some_method </code></div>"
|
239
|
+
end
|
216
240
|
|
217
241
|
@override.source_argument.should == :cut
|
218
242
|
end
|
@@ -243,7 +267,11 @@ module Deface
|
|
243
267
|
|
244
268
|
it "should remove cut element from original parsed source" do
|
245
269
|
@override.source
|
246
|
-
|
270
|
+
if RUBY_PLATFORM == 'java'
|
271
|
+
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><code erb-loud=\"\"> hello </code>"
|
272
|
+
else
|
273
|
+
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><code erb-loud> hello </code>"
|
274
|
+
end
|
247
275
|
|
248
276
|
@override.source_argument.should == :cut
|
249
277
|
end
|
@@ -298,9 +326,18 @@ module Deface
|
|
298
326
|
|
299
327
|
it "should return escaped source" do
|
300
328
|
@override.source_element.should be_an_instance_of Nokogiri::HTML::DocumentFragment
|
301
|
-
|
302
|
-
|
303
|
-
|
329
|
+
|
330
|
+
if RUBY_PLATFORM == 'java'
|
331
|
+
source = "<code erb-loud=\"\"> method :opt => 'x' & 'y' </code>"
|
332
|
+
@override.source_element.to_s.should == source
|
333
|
+
#do it twice to ensure it doesn't change as it's destructive
|
334
|
+
@override.source_element.to_s.should == source
|
335
|
+
else
|
336
|
+
source = "<code erb-loud> method :opt => 'x' & 'y' </code>"
|
337
|
+
@override.source_element.to_s.should == source
|
338
|
+
#do it twice to ensure it doesn't change as it's destructive
|
339
|
+
@override.source_element.to_s.should == source
|
340
|
+
end
|
304
341
|
end
|
305
342
|
end
|
306
343
|
|
data/spec/deface/parser_spec.rb
CHANGED
@@ -16,9 +16,16 @@ module Deface
|
|
16
16
|
it "should parse html document" do
|
17
17
|
parsed = Deface::Parser.convert("<html><head><title>Hello</title></head><body>test</body>")
|
18
18
|
parsed.should be_an_instance_of(Nokogiri::HTML::Document)
|
19
|
-
parsed = parsed.to_s.split("\n")
|
19
|
+
parsed = parsed.to_s.split("\n")
|
20
20
|
|
21
|
-
|
21
|
+
unless RUBY_PLATFORM == 'java'
|
22
|
+
parsed = parsed[1..-1] #ignore doctype added by nokogiri
|
23
|
+
end
|
24
|
+
|
25
|
+
#accounting for qwerks in Nokogir between ruby versions / platforms
|
26
|
+
if RUBY_PLATFORM == 'java'
|
27
|
+
parsed.should == ["<html><head><title>Hello</title></head><body>test</body></html>"]
|
28
|
+
elsif RUBY_VERSION < "1.9"
|
22
29
|
parsed.should == "<html>\n<head><title>Hello</title></head>\n<body>test</body>\n</html>".split("\n")
|
23
30
|
else
|
24
31
|
parsed.should == "<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n<title>Hello</title>\n</head>\n<body>test</body>\n</html>".split("\n")
|
@@ -26,8 +33,14 @@ module Deface
|
|
26
33
|
|
27
34
|
parsed = Deface::Parser.convert("<html><title>test</title></html>")
|
28
35
|
parsed.should be_an_instance_of(Nokogiri::HTML::Document)
|
29
|
-
parsed = parsed.to_s.split("\n")
|
30
|
-
|
36
|
+
parsed = parsed.to_s.split("\n")
|
37
|
+
|
38
|
+
unless RUBY_PLATFORM == 'java'
|
39
|
+
parsed = parsed[1..-1] #ignore doctype added by nokogiri
|
40
|
+
end
|
41
|
+
|
42
|
+
#accounting for qwerks in Nokogir between ruby versions / platforms
|
43
|
+
if RUBY_VERSION < "1.9" || RUBY_PLATFORM == 'java'
|
31
44
|
parsed.should == ["<html><head><title>test</title></head></html>"]
|
32
45
|
else
|
33
46
|
parsed.should == "<html><head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n<title>test</title>\n</head></html>".split("\n")
|
@@ -35,22 +48,34 @@ module Deface
|
|
35
48
|
|
36
49
|
parsed = Deface::Parser.convert("<html><p>test</p></html>")
|
37
50
|
parsed.should be_an_instance_of(Nokogiri::HTML::Document)
|
38
|
-
parsed = parsed.to_s.split("\n")
|
39
|
-
|
51
|
+
parsed = parsed.to_s.split("\n")
|
52
|
+
|
53
|
+
if RUBY_PLATFORM == 'java'
|
54
|
+
parsed.should eq ["<html><head></head><body><p>test</p></body></html>"]
|
55
|
+
else
|
56
|
+
parsed = parsed[1..-1]
|
57
|
+
parsed.should eq ["<html><body><p>test</p></body></html>"]
|
58
|
+
end
|
40
59
|
end
|
41
60
|
|
42
61
|
it "should parse body tag" do
|
43
|
-
|
44
|
-
|
45
|
-
|
62
|
+
tag = Deface::Parser.convert("<body id=\"body\" <%= something %>>test</body>")
|
63
|
+
tag.should be_an_instance_of(Nokogiri::XML::Element)
|
64
|
+
tag.text.should eq 'test'
|
65
|
+
tag.attributes['id'].value.should eq 'body'
|
66
|
+
tag.attributes['data-erb-0'].value.should eq '<%= something %>'
|
46
67
|
end
|
47
68
|
|
48
69
|
it "should convert <% ... %>" do
|
49
|
-
Deface::Parser.convert("<% method_name %>")
|
70
|
+
tag = Deface::Parser.convert("<% method_name %>")
|
71
|
+
tag = tag.css('code').first
|
72
|
+
tag.attributes['erb-silent'].value.should eq ''
|
50
73
|
end
|
51
74
|
|
52
75
|
it "should convert <%= ... %>" do
|
53
|
-
Deface::Parser.convert("<%= method_name %>")
|
76
|
+
tag = Deface::Parser.convert("<%= method_name %>")
|
77
|
+
tag = tag.css('code').first
|
78
|
+
tag.attributes['erb-loud'].value.should eq ''
|
54
79
|
end
|
55
80
|
|
56
81
|
it "should convert first <% ... %> inside html tag" do
|
@@ -70,21 +95,52 @@ module Deface
|
|
70
95
|
end
|
71
96
|
|
72
97
|
it "should convert <% ... %> inside non-quoted attr value" do
|
73
|
-
Deface::Parser.convert("<p id=<% method_name %>></p>")
|
74
|
-
|
98
|
+
tag = Deface::Parser.convert("<p id=<% method_name %>></p>")
|
99
|
+
tag = tag.css('p').first
|
100
|
+
tag.attributes['data-erb-id'].value.should eq '<% method_name %>'
|
101
|
+
|
102
|
+
tag = Deface::Parser.convert("<p id=<% method_name %> alt=\"test\"></p>")
|
103
|
+
tag = tag.css('p').first
|
104
|
+
tag.attributes['data-erb-id'].value.should eq '<% method_name %>'
|
105
|
+
tag.attributes['alt'].value.should eq 'test'
|
75
106
|
end
|
76
107
|
|
77
108
|
it "should convert multiple <% ... %> inside html tag" do
|
78
|
-
Deface::Parser.convert(%q{<p <%= method_name %> alt="<% x = 'y' +
|
79
|
-
\"2\" %>" title='<% method_name %>' <%= other_method %></p>})
|
109
|
+
tag = Deface::Parser.convert(%q{<p <%= method_name %> alt="<% x = 'y' +
|
110
|
+
\"2\" %>" title='<% method_name %>' <%= other_method %></p>})
|
111
|
+
|
112
|
+
tag = tag.css('p').first
|
113
|
+
tag.attributes['data-erb-0'].value.should == "<%= method_name %>"
|
114
|
+
tag.attributes['data-erb-1'].value.should == "<%= other_method %>"
|
115
|
+
tag.attributes['data-erb-alt'].value.should == "<% x = 'y' + \n \\\"2\\\" %>"
|
116
|
+
tag.attributes['data-erb-title'].value.should == "<% method_name %>"
|
80
117
|
end
|
81
118
|
|
82
119
|
it "should convert <%= ... %> including href attribute" do
|
83
|
-
Deface::Parser.convert(%(<a href="<%= x 'y' + "z" %>">A Link</a>))
|
120
|
+
tag = Deface::Parser.convert(%(<a href="<%= x 'y' + "z" %>">A Link</a>))
|
121
|
+
tag = tag.css('a').first
|
122
|
+
tag.attributes['data-erb-href'].value.should eq "<%= x 'y' + \"z\" %>"
|
123
|
+
tag.text.should eq 'A Link'
|
84
124
|
end
|
85
125
|
|
86
126
|
it "should escape contents code tags" do
|
87
|
-
Deface::Parser.convert("<% method_name
|
127
|
+
tag = Deface::Parser.convert("<% method_name :key => 'value' %>")
|
128
|
+
tag = tag.css('code').first
|
129
|
+
tag.attributes.key?('erb-silent').should be_true
|
130
|
+
tag.text.should eq " method_name :key => 'value' "
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should handle round brackets in code tags" do
|
134
|
+
# commented out line below will fail as : adjacent to ( causes Nokogiri parser issue on jruby
|
135
|
+
tag = Deface::Parser.convert("<% method_name(:key => 'value') %>")
|
136
|
+
tag = tag.css('code').first
|
137
|
+
tag.attributes.key?('erb-silent').should be_true
|
138
|
+
tag.text.should eq " method_name(:key => 'value') "
|
139
|
+
|
140
|
+
tag = Deface::Parser.convert("<% method_name( :key => 'value' ) %>")
|
141
|
+
tag = tag.css('code').first
|
142
|
+
tag.attributes.key?('erb-silent').should be_true
|
143
|
+
tag.text.should eq " method_name( :key => 'value' ) "
|
88
144
|
end
|
89
145
|
|
90
146
|
if "".encoding_aware?
|