Skip to content

Commit

Permalink
s/erb-(loud|silent)/\1/g
Browse files Browse the repository at this point in the history
As we're breaking existing selectors anyways, we might as well remove
the redundant erb-.
  • Loading branch information
jhawthorn committed Sep 4, 2013
1 parent 7463cc5 commit 328b0f2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
10 changes: 5 additions & 5 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Removes any ERB block containing the string `helper_method` in the `posts/new.ht
```ruby
Deface::Override.new(:virtual_path => "posts/new",
:name => "example-4",
:remove => "erb[erb-loud]:contains('helper_method')",
:remove => "erb[loud]:contains('helper_method')",
:original => "<%= helper_method %>")
```

Expand All @@ -171,8 +171,8 @@ Remove an entire ERB if statement (and all it's contents) in the 'admin/products
```ruby
Deface::Override.new(:virtual_path => 'admin/products/index',
:name => "remove_if_statement",
:remove => "erb[erb-silent]:contains('if @product.sold?')",
:closing_selector => "erb[erb-silent]:contains('end')"
:remove => "erb[silent]:contains('if @product.sold?')",
:closing_selector => "erb[silent]:contains('end')"
```

### Scope
Expand Down Expand Up @@ -354,7 +354,7 @@ Deface temporarily converts ERB files into a pseudo HTML markup that can be pars
becomes:

```html
<erb erb-loud> some ruby code </erb>
<erb loud> some ruby code </erb>
```

and
Expand All @@ -366,7 +366,7 @@ and
becomes:

```html
<erb erb-silent> other ruby code </erb>
<erb silent> other ruby code </erb>
```

ERB that is contained inside a HTML tag definition is converted slightly differently to ensure a valid HTML document that Nokogiri can parse:
Expand Down
12 changes: 6 additions & 6 deletions lib/deface/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def self.erb_markup!(source)
end

#replaces all <% %> not inside opening html tags
replacements = [ {"<%=" => "<erb erb-loud>"},
{"<%" => "<erb erb-silent>"},
replacements = [ {"<%=" => "<erb loud>"},
{"<%" => "<erb silent>"},
{"%>" => "</erb>"} ]

replacements.each{ |h| h.each { |replace, with| source.gsub! replace, with } }
Expand All @@ -50,10 +50,10 @@ def self.erb_markup!(source)
# undoes ERB markup generated by Deface::Parser::ERB
#
def self.undo_erb_markup!(source)
replacements = [ {"<erb erb-silent>" => '<%'},
{"<erb erb-silent=\"\">" => '<%'},
{"<erb erb-loud>" => '<%='},
{"<erb erb-loud=\"\">" => '<%='},
replacements = [ {"<erb silent>" => '<%'},
{"<erb silent=\"\">" => '<%'},
{"<erb loud>" => '<%='},
{"<erb loud=\"\">" => '<%='},
{"</erb>" => '%>'}]

replacements.each{ |h| h.each { |replace, with| source.gsub! replace, with } }
Expand Down
36 changes: 18 additions & 18 deletions spec/deface/override_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ module Deface
@original.original_source.should be_an_instance_of Nokogiri::HTML::DocumentFragment

if RUBY_PLATFORM == 'java'
@original.original_source.to_s.should == "<p><erb erb-loud=\"\"> something </erb></p>"
@original.original_source.to_s.should == "<p><erb loud=\"\"> something </erb></p>"
else
@original.original_source.to_s.should == "<p><erb erb-loud> something </erb></p>"
@original.original_source.to_s.should == "<p><erb loud> something </erb></p>"
end
end
end
Expand All @@ -64,11 +64,11 @@ module Deface

it "should return true when input contains similar (ignoring whitespace)" do
if RUBY_PLATFORM == 'java'
@original.validate_original("<p><erb erb-loud=\"\"> something </erb></p>").should be_true
@original.validate_original("<p><erb erb-loud=\"\">something\n</erb> </p>").should be_true
@original.validate_original("<p><erb loud=\"\"> something </erb></p>").should be_true
@original.validate_original("<p><erb loud=\"\">something\n</erb> </p>").should be_true
else
@original.validate_original("<p><erb erb-loud> something </erb></p>").should be_true
@original.validate_original("<p><erb erb-loud>something\n</erb> </p>").should be_true
@original.validate_original("<p><erb loud> something </erb></p>").should be_true
@original.validate_original("<p><erb loud>something\n</erb> </p>").should be_true
end
end

Expand Down Expand Up @@ -189,9 +189,9 @@ module Deface
@override.source

if RUBY_PLATFORM == 'java'
parsed.to_s.gsub(/\n/,'').should == "<div><h1>Manage Posts</h1><erb erb-loud=\"\"> some_method </erb></div>"
parsed.to_s.gsub(/\n/,'').should == "<div><h1>Manage Posts</h1><erb loud=\"\"> some_method </erb></div>"
else
parsed.to_s.gsub(/\n/,'').should == "<div><h1>Manage Posts</h1><erb erb-loud> some_method </erb></div>"
parsed.to_s.gsub(/\n/,'').should == "<div><h1>Manage Posts</h1><erb loud> some_method </erb></div>"
end

@override.source_argument.should == :copy
Expand All @@ -202,7 +202,7 @@ module Deface
end

it "should return unescaped content for source document" do
@override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "h1", :copy => "erb[erb-loud]:contains('some_method')")
@override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "h1", :copy => "erb[loud]:contains('some_method')")
@override.stub(:parsed_document).and_return(parsed)
@override.source.should == "<%= some_method %>"
end
Expand All @@ -224,9 +224,9 @@ module Deface
@override.source

if RUBY_PLATFORM == 'java'
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><erb erb-silent=\"\"> if true </erb><p>True that!</p><erb erb-silent=\"\"> end </erb><p>Hello</p>"
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><erb silent=\"\"> if true </erb><p>True that!</p><erb silent=\"\"> end </erb><p>Hello</p>"
else
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><erb erb-silent> if true </erb><p>True that!</p><erb erb-silent> end </erb><p>Hello</p>"
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><erb silent> if true </erb><p>True that!</p><erb silent> end </erb><p>Hello</p>"
end

@override.source_argument.should == :copy
Expand All @@ -247,9 +247,9 @@ module Deface
it "should remove cut element from original parsed source" do
@override.source
if RUBY_PLATFORM == 'java'
parsed.to_s.gsub(/\n/,'').should == "<div><erb erb-loud=\"\"> some_method </erb></div>"
parsed.to_s.gsub(/\n/,'').should == "<div><erb loud=\"\"> some_method </erb></div>"
else
parsed.to_s.gsub(/\n/,'').should == "<div><erb erb-loud> some_method </erb></div>"
parsed.to_s.gsub(/\n/,'').should == "<div><erb loud> some_method </erb></div>"
end

@override.source_argument.should == :cut
Expand All @@ -260,7 +260,7 @@ module Deface
end

it "should return unescaped content for source document" do
@override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "h1", :cut => "erb[erb-loud]:contains('some_method')")
@override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "h1", :cut => "erb[loud]:contains('some_method')")
@override.stub(:parsed_document).and_return(parsed)
@override.source.should == "<%= some_method %>"
end
Expand All @@ -282,9 +282,9 @@ module Deface
it "should remove cut element from original parsed source" do
@override.source
if RUBY_PLATFORM == 'java'
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><erb erb-loud=\"\"> hello </erb>"
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><erb loud=\"\"> hello </erb>"
else
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><erb erb-loud> hello </erb>"
parsed.to_s.gsub(/\n/,'').should == "<h1>World</h1><erb loud> hello </erb>"
end

@override.source_argument.should == :cut
Expand Down Expand Up @@ -342,12 +342,12 @@ module Deface
@override.source_element.should be_an_instance_of Nokogiri::HTML::DocumentFragment

if RUBY_PLATFORM == 'java'
source = "<erb erb-loud=\"\"> method :opt =&gt; 'x' &amp; 'y' </erb>"
source = "<erb loud=\"\"> method :opt =&gt; 'x' &amp; 'y' </erb>"
@override.source_element.to_s.should == source
#do it twice to ensure it doesn't change as it's destructive
@override.source_element.to_s.should == source
else
source = "<erb erb-loud> method :opt =&gt; 'x' &amp; 'y' </erb>"
source = "<erb loud> method :opt =&gt; 'x' &amp; 'y' </erb>"
@override.source_element.to_s.should == source
#do it twice to ensure it doesn't change as it's destructive
@override.source_element.to_s.should == source
Expand Down
22 changes: 11 additions & 11 deletions spec/deface/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ module Deface
parsed = parsed.to_s.split("\n")

if RUBY_PLATFORM == 'java'
parsed.should == ["<html><head><erb erb-loud=\"\"> method_name </erb></head><body></body></html>"]
parsed.should == ["<html><head><erb loud=\"\"> method_name </erb></head><body></body></html>"]
else
parsed = parsed[1..-1]
parsed.should == "<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n<erb erb-loud> method_name </erb>\n</head>\n<body></body>\n</html>".split("\n")
parsed.should == "<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n<erb loud> method_name </erb>\n</head>\n<body></body>\n</html>".split("\n")
end
end

Expand All @@ -83,13 +83,13 @@ module Deface
it "should convert <% ... %>" do
tag = Deface::Parser.convert("<% method_name %>")
tag = tag.css('erb').first
tag.attributes['erb-silent'].value.should eq ''
tag.attributes['silent'].value.should eq ''
end

it "should convert <%= ... %>" do
tag = Deface::Parser.convert("<%= method_name %>")
tag = tag.css('erb').first
tag.attributes['erb-loud'].value.should eq ''
tag.attributes['loud'].value.should eq ''
end

it "should convert first <% ... %> inside html tag" do
Expand Down Expand Up @@ -146,20 +146,20 @@ module Deface
it "should escape contents erb tags" do
tag = Deface::Parser.convert("<% method_name :key => 'value' %>")
tag = tag.css('erb').first
tag.attributes.key?('erb-silent').should be_true
tag.attributes.key?('silent').should be_true
tag.text.should eq " method_name :key => 'value' "
end

it "should handle round brackets in erb tags" do
# commented out line below will fail as : adjacent to ( causes Nokogiri parser issue on jruby
tag = Deface::Parser.convert("<% method_name(:key => 'value') %>")
tag = tag.css('erb').first
tag.attributes.key?('erb-silent').should be_true
tag.attributes.key?('silent').should be_true
tag.text.should eq " method_name(:key => 'value') "

tag = Deface::Parser.convert("<% method_name( :key => 'value' ) %>")
tag = tag.css('erb').first
tag.attributes.key?('erb-silent').should be_true
tag.attributes.key?('silent').should be_true
tag.text.should eq " method_name( :key => 'value' ) "
end

Expand All @@ -183,12 +183,12 @@ module Deface
end

describe "#undo_erb_markup" do
it "should revert <erb erb-silent>" do
Deface::Parser.undo_erb_markup!("<erb erb-silent> method_name </erb>").should == "<% method_name %>"
it "should revert <erb silent>" do
Deface::Parser.undo_erb_markup!("<erb silent> method_name </erb>").should == "<% method_name %>"
end

it "should revert <erb erb-loud>" do
Deface::Parser.undo_erb_markup!("<erb erb-loud> method_name </erb>").should == "<%= method_name %>"
it "should revert <erb loud>" do
Deface::Parser.undo_erb_markup!("<erb loud> method_name </erb>").should == "<%= method_name %>"
end

it "should revert data-erb-x attrs inside html tag" do
Expand Down

0 comments on commit 328b0f2

Please sign in to comment.