diff --git a/README.markdown b/README.markdown index c7e526d..9305086 100644 --- a/README.markdown +++ b/README.markdown @@ -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 => "code[erb-loud]:contains('helper_method')", + :remove => "erb[erb-loud]:contains('helper_method')", :original => "<%= helper_method %>") ``` @@ -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 => "code[erb-silent]:contains('if @product.sold?')", - :closing_selector => "code[erb-silent]:contains('end')" + :remove => "erb[erb-silent]:contains('if @product.sold?')", + :closing_selector => "erb[erb-silent]:contains('end')" ``` ### Scope @@ -354,7 +354,7 @@ Deface temporarily converts ERB files into a pseudo HTML markup that can be pars becomes: ```html - some ruby code + some ruby code ``` and @@ -366,7 +366,7 @@ and becomes: ```html - other ruby code + other ruby code ``` ERB that is contained inside a HTML tag definition is converted slightly differently to ensure a valid HTML document that Nokogiri can parse: diff --git a/lib/deface/actions/surround_action.rb b/lib/deface/actions/surround_action.rb index 3627750..1553a49 100644 --- a/lib/deface/actions/surround_action.rb +++ b/lib/deface/actions/surround_action.rb @@ -6,10 +6,10 @@ def source_element end def original_placeholders - @original_placeholders ||= source_element.css("code:contains('render_original')") + @original_placeholders ||= source_element.css("erb:contains('render_original')") raise(DefaceError, "The surround action couldn't find <%= render_original %> in your template") unless @original_placeholders.first @original_placeholders end end end -end \ No newline at end of file +end diff --git a/lib/deface/parser.rb b/lib/deface/parser.rb index 4fb0374..fc4f572 100644 --- a/lib/deface/parser.rb +++ b/lib/deface/parser.rb @@ -34,13 +34,13 @@ def self.erb_markup!(source) end #replaces all <% %> not inside opening html tags - replacements = [ {"<%=" => ""}, - {"<%" => ""}, - {"%>" => ""} ] + replacements = [ {"<%=" => ""}, + {"<%" => ""}, + {"%>" => ""} ] replacements.each{ |h| h.each { |replace, with| source.gsub! replace, with } } - source.scan(/()((?:(?!<\/code>)[\s\S])*)(<\/code>)/).each do |match| + source.scan(/()((?:(?!<\/erb>)[\s\S])*)(<\/erb>)/).each do |match| source.sub!("#{match[0]}#{match[1]}#{match[2]}") { |m| m = "#{match[0]}#{CGI.escapeHTML(match[1])}#{match[2]}" } end @@ -50,11 +50,11 @@ def self.erb_markup!(source) # undoes ERB markup generated by Deface::Parser::ERB # def self.undo_erb_markup!(source) - replacements = [ {"" => '<%'}, - {"" => '<%'}, - {"" => '<%='}, - {"" => '<%='}, - {"" => '%>'}] + replacements = [ {"" => '<%'}, + {"" => '<%'}, + {"" => '<%='}, + {"" => '<%='}, + {"" => '%>'}] replacements.each{ |h| h.each { |replace, with| source.gsub! replace, with } } diff --git a/spec/deface/applicator_spec.rb b/spec/deface/applicator_spec.rb index 73cc3bd..0290939 100644 --- a/spec/deface/applicator_spec.rb +++ b/spec/deface/applicator_spec.rb @@ -31,7 +31,7 @@ module Deface describe "with a single :copy using :start and :end" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_before => "h1", - :copy => {:start => "code:contains('if true')", :end => "code:contains('end')"}) } + :copy => {:start => "erb:contains('if true')", :end => "erb:contains('end')"}) } let(:source) { "

World

<% if true %>

True that!

<% end %>

Hello

" } @@ -52,7 +52,7 @@ module Deface describe "with a single :cut using :start and :end" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", - :cut => {:start => "code:contains('if true')", :end => "code:contains('end')"}) } + :cut => {:start => "erb:contains('if true')", :end => "erb:contains('end')"}) } let(:source) { "

World

<% if true %>

True that!

<% end %>

Hello

" } diff --git a/spec/deface/override_spec.rb b/spec/deface/override_spec.rb index 2502939..9d803a3 100644 --- a/spec/deface/override_spec.rb +++ b/spec/deface/override_spec.rb @@ -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 == "

something

" + @original.original_source.to_s.should == "

something

" else - @original.original_source.to_s.should == "

something

" + @original.original_source.to_s.should == "

something

" end end end @@ -64,11 +64,11 @@ module Deface it "should return true when input contains similar (ignoring whitespace)" do if RUBY_PLATFORM == 'java' - @original.validate_original("

something

").should be_true - @original.validate_original("

something\n

").should be_true + @original.validate_original("

something

").should be_true + @original.validate_original("

something\n

").should be_true else - @original.validate_original("

something

").should be_true - @original.validate_original("

something\n

").should be_true + @original.validate_original("

something

").should be_true + @original.validate_original("

something\n

").should be_true end end @@ -117,11 +117,11 @@ module Deface before(:each) do @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", - :haml => %q{%strong{:class => "code", :id => "message"}= 'Hello, World!'}) + :haml => %q{%strong{:class => "erb", :id => "message"}= 'Hello, World!'}) end it "should return erb converted from haml as source" do - @override.source.should == "<%= 'Hello, World!' %>\n\n" + @override.source.should == "<%= 'Hello, World!' %>\n\n" @override.source_argument.should == :haml end @@ -131,11 +131,11 @@ module Deface before(:each) do @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", - :slim => %q{strong class="code" id="message"= 'Hello, World!'}) + :slim => %q{strong class="erb" id="message"= 'Hello, World!'}) end it "should return erb converted from slim as source" do - @override.source.should == "<%= ::Temple::Utils.escape_html_safe(('Hello, World!')) %><%\n%>" + @override.source.should == "<%= ::Temple::Utils.escape_html_safe(('Hello, World!')) %><%\n%>" @override.source_argument.should == :slim end @@ -189,9 +189,9 @@ module Deface @override.source if RUBY_PLATFORM == 'java' - parsed.to_s.gsub(/\n/,'').should == "

Manage Posts

some_method
" + parsed.to_s.gsub(/\n/,'').should == "

Manage Posts

some_method
" else - parsed.to_s.gsub(/\n/,'').should == "

Manage Posts

some_method
" + parsed.to_s.gsub(/\n/,'').should == "

Manage Posts

some_method
" end @override.source_argument.should == :copy @@ -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 => "code[erb-loud]:contains('some_method')") + @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "h1", :copy => "erb[erb-loud]:contains('some_method')") @override.stub(:parsed_document).and_return(parsed) @override.source.should == "<%= some_method %>" end @@ -215,7 +215,7 @@ module Deface before(:each) do @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "h1", - :copy => {:start => "code:contains('if true')", :end => "code:contains('end')"}) + :copy => {:start => "erb:contains('if true')", :end => "erb:contains('end')"}) @override.stub(:parsed_document).and_return(parsed) end @@ -224,9 +224,9 @@ module Deface @override.source if RUBY_PLATFORM == 'java' - parsed.to_s.gsub(/\n/,'').should == "

World

if true

True that!

end

Hello

" + parsed.to_s.gsub(/\n/,'').should == "

World

if true

True that!

end

Hello

" else - parsed.to_s.gsub(/\n/,'').should == "

World

if true

True that!

end

Hello

" + parsed.to_s.gsub(/\n/,'').should == "

World

if true

True that!

end

Hello

" end @override.source_argument.should == :copy @@ -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 == "
some_method
" + parsed.to_s.gsub(/\n/,'').should == "
some_method
" else - parsed.to_s.gsub(/\n/,'').should == "
some_method
" + parsed.to_s.gsub(/\n/,'').should == "
some_method
" end @override.source_argument.should == :cut @@ -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 => "code[erb-loud]:contains('some_method')") + @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "h1", :cut => "erb[erb-loud]:contains('some_method')") @override.stub(:parsed_document).and_return(parsed) @override.source.should == "<%= some_method %>" end @@ -274,7 +274,7 @@ module Deface before(:each) do @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "h1", - :cut => {:start => "code:contains('if true')", :end => "code:contains('end')"}) + :cut => {:start => "erb:contains('if true')", :end => "erb:contains('end')"}) @override.stub(:parsed_document).and_return(parsed) end @@ -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 == "

World

hello " + parsed.to_s.gsub(/\n/,'').should == "

World

hello " else - parsed.to_s.gsub(/\n/,'').should == "

World

hello " + parsed.to_s.gsub(/\n/,'').should == "

World

hello " end @override.source_argument.should == :cut @@ -342,12 +342,12 @@ module Deface @override.source_element.should be_an_instance_of Nokogiri::HTML::DocumentFragment if RUBY_PLATFORM == 'java' - source = " method :opt => 'x' & 'y' " + source = " method :opt => 'x' & 'y' " @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 = " method :opt => 'x' & 'y' " + source = " method :opt => 'x' & 'y' " @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 @@ -497,7 +497,7 @@ module Deface Deface::Override.all.clear @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "

Argh!

") - @second = Deface::Override.new(:virtual_path => "posts/index", :name => "second", :insert_after => "p", :text => "
this is code?
") + @second = Deface::Override.new(:virtual_path => "posts/index", :name => "second", :insert_after => "p", :text => "
this is erb?
") @digest = Deface::Override.digest(:virtual_path => "posts/index") end @@ -513,7 +513,7 @@ module Deface @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "

Argh!

") Deface::Override.digest(:virtual_path => "posts/index").should == @digest - @second = Deface::Override.new(:virtual_path => "posts/index", :name => "2nd", :insert_after => "p", :text => "
this is code?
") + @second = Deface::Override.new(:virtual_path => "posts/index", :name => "2nd", :insert_after => "p", :text => "
this is erb?
") Deface::Override.digest(:virtual_path => "posts/index").should_not == @digest end diff --git a/spec/deface/parser_spec.rb b/spec/deface/parser_spec.rb index 41beb0e..16e5764 100644 --- a/spec/deface/parser_spec.rb +++ b/spec/deface/parser_spec.rb @@ -68,13 +68,13 @@ module Deface it "should convert <% ... %>" do tag = Deface::Parser.convert("<% method_name %>") - tag = tag.css('code').first + tag = tag.css('erb').first tag.attributes['erb-silent'].value.should eq '' end it "should convert <%= ... %>" do tag = Deface::Parser.convert("<%= method_name %>") - tag = tag.css('code').first + tag = tag.css('erb').first tag.attributes['erb-loud'].value.should eq '' end @@ -129,22 +129,22 @@ module Deface tag.text.should eq 'A Link' end - it "should escape contents code tags" do + it "should escape contents erb tags" do tag = Deface::Parser.convert("<% method_name :key => 'value' %>") - tag = tag.css('code').first + tag = tag.css('erb').first tag.attributes.key?('erb-silent').should be_true tag.text.should eq " method_name :key => 'value' " end - it "should handle round brackets in code tags" do + 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('code').first + tag = tag.css('erb').first tag.attributes.key?('erb-silent').should be_true tag.text.should eq " method_name(:key => 'value') " tag = Deface::Parser.convert("<% method_name( :key => 'value' ) %>") - tag = tag.css('code').first + tag = tag.css('erb').first tag.attributes.key?('erb-silent').should be_true tag.text.should eq " method_name( :key => 'value' ) " end @@ -169,12 +169,12 @@ module Deface end describe "#undo_erb_markup" do - it "should revert " do - Deface::Parser.undo_erb_markup!(" method_name ").should == "<% method_name %>" + it "should revert " do + Deface::Parser.undo_erb_markup!(" method_name ").should == "<% method_name %>" end - it "should revert " do - Deface::Parser.undo_erb_markup!(" method_name ").should == "<%= method_name %>" + it "should revert " do + Deface::Parser.undo_erb_markup!(" method_name ").should == "<%= method_name %>" end it "should revert data-erb-x attrs inside html tag" do @@ -195,7 +195,7 @@ module Deface end end - it "should unescape contents of code tags" do + it "should unescape contents of erb tags" do Deface::Parser.undo_erb_markup!("<% method(:key => 'value' %>").should == "<% method(:key => 'value' %>" Deface::Parser.undo_erb_markup!("<% method(:key => 'value'\n %>").should == "<% method(:key => 'value'\n %>" end