require 'spec_helper' class Dummy extend Deface::Applicator::ClassMethods extend Deface::Search::ClassMethods attr_reader :parsed_document def self.all Rails.application.config.deface.overrides.all end end def attributes_to_sorted_array(src) Nokogiri::HTML::DocumentFragment.parse(src).children.first.attributes end module Deface describe Applicator do include_context "mock Rails.application" before { Dummy.all.clear } describe "with a single remove override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove => "p", :text => "

Argh!

") } let(:source) { "

test

<%= raw(text) %>" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<%= raw(text) %>" end end describe "with a single remove override with closing_selector defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove => "h1", :closing_selector => "h2") } let(:source) { "

I should be safe

Before!

start

some junk

more junk

end

After!" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "

I should be safe

Before!After!" end end describe "with a single replace override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "p", :text => "

Argh!

") } let(:source) { "

test

" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "

Argh!

" end end describe "with a single replace override with closing_selector defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :closing_selector => "h2", :text => "Argh!") } let(:source) { "

start

some junk

more junk

end

" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "Argh!" end end describe "with a single replace_contents override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace_contents => "p", :text => "

Argh!

") } let(:source) { "

HelloI am not a pirate

" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "

Argh!

" end end describe "with a single replace_contents override with closing_selector defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace_contents => "h1", :closing_selector => "h2", :text => "Argh!") } let(:source) { "

start

some junk

more junk

end

" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "

start

Argh!

end

" end end describe "with a single insert_after override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "img.button", :text => "<% help %>") } let(:source) { "
" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "
<% help %>
" end end describe "with a single insert_before override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "ul li:last", :text => "<%= help %>") } let(:source) { "" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "" end end describe "with a single insert_top override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_top => "ul", :text => "
  • me first
  • ") } let(:source) { "" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "" end end describe "with a single insert_top override defined when targetted elemenet has no children" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_top => "ul", :text => "
  • first
  • second
  • third
  • ") } let(:source) { "" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "" end end describe "with a single insert_bottom override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_bottom => "ul", :text => "
  • I'm always last
  • ") } let(:source) { "" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "" end end describe "with a single insert_bottom override defined when targetted elemenet has no children" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_bottom => "ul", :text => "
  • I'm always last
  • ") } let(:source) { "" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "" end end describe "with a single set_attributes override (containing only text) defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :set_attributes => 'img', :attributes => {:class => 'pretty', :alt => 'something interesting'}) } let(:source) { "" } it "should return modified source" do attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"})) attrs["class"].value.should == "pretty" attrs["alt"].value.should == "something interesting" attrs["src"].value.should == "path/to/button.png" end end describe "with a single set_attributes override (containing erb) defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :set_attributes => 'img', :attributes => {:class => 'pretty', 'data-erb-alt' => '<%= something_interesting %>'}) } let(:source) { "" } it "should return modified source" do attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"})) attrs["class"].value.should == "pretty" attrs["alt"].value.should == "<%= something_interesting %>" attrs["src"].value.should == "path/to/button.png" end end describe "with a single set_attributes override (containing erb) defined targetting an existing pseudo attribute" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :set_attributes => 'img', :attributes => {:class => '<%= get_some_other_class %>', :alt => 'something interesting'}) } let(:source) { "\" src=\"path/to/button.png\">" } it "should return modified source" do attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"})) attrs["class"].value.should == "<%= get_some_other_class %>" attrs["alt"].value.should == "something interesting" attrs["src"].value.should == "path/to/button.png" end end describe "with a single add_to_attributes override (containing only text) defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :add_to_attributes => 'img', :attributes => {:class => 'pretty', :alt => 'something interesting'}) } let(:source) { "" } it "should return modified source" do attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"})) attrs["class"].value.should == "button pretty" attrs["alt"].value.should == "something interesting" end end describe "with a single add_to_attributes override (containing erb) defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :add_to_attributes => 'img', :attributes => {:class => '<%= add_class %>'}) } let(:source) { "" } it "should return modified source" do attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"})) attrs["class"].value.should == "button <%= add_class %>" attrs["src"].value.should == "path/to/button.png" end end describe "with a single add_to_attributes override (containing erb) defined using pseudo attribute name" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :add_to_attributes => 'img', :attributes => {'data-erb-class' => '<%= add_class %>'}) } let(:source) { "" } it "should return modified source" do attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"})) attrs["class"].value.should == "button <%= add_class %>" attrs["src"].value.should == "path/to/button.png" end end describe "with a single add_to_attributes override (containing erb) defined targetting an existing pseudo attribute" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :add_to_attributes => 'img', :attributes => {:class => '<%= get_some_other_class %>'}) } let(:source) { "\" src=\"path/to/button.png\">" } it "should return modified source" do attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"})) attrs["class"].value.should == "<%= get_class %> <%= get_some_other_class %>" attrs["src"].value.should == "path/to/button.png" end end describe "with a single remove_from_attributes override (containing only text) defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img', :attributes => {:class => 'pretty'}) } let(:source) { "" } it "should return modified source" do attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"})) attrs["class"].value.should == "button" end end describe "with a single remove_from_attributes override (containing erb) defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img', :attributes => {:class => '<%= add_class %>'}) } let(:source) { "\" src=\"path/to/button.png\">" } it "should return modified source" do attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"})) attrs["class"].value.should == "button" attrs["src"].value.should == "path/to/button.png" end end describe "with a single remove_from_attributes override (containing erb) defined using pseudo attribute name" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img', :attributes => {'data-erb-class' => '<%= add_class %>'}) } let(:source) { "\" src=\"path/to/button.png\">" } it "should return modified source" do attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"})) attrs["class"].value.should == "button" attrs["src"].value.should == "path/to/button.png" end end describe "with a single remove_from_attributes override (containing only text) defined where value is not present in attribute" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img', :attributes => {:class => 'pretty'}) } let(:source) { "" } it "should return unmodified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "" end end describe "with a single remove_from_attributes override (containing only text) defined where value is not present in attribute" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img', :attributes => {:class => 'pretty'}) } let(:source) { "" } it "should return unmodified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "" end end describe "with a single remove_from_attributes override (containing erb) defined targetting an existing pseudo attribute" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove_from_attributes => 'img', :attributes => {:class => '<%= get_some_other_class %>'}) } let(:source) { " <%= get_some_other_class %>\" src=\"path/to/button.png\">" } it "should return modified source" do attrs = attributes_to_sorted_array(Dummy.apply(source, {:virtual_path => "posts/index"})) attrs["class"].value.should == "<%= get_class %>" attrs["src"].value.should == "path/to/button.png" end end describe "with a single set_attributes override (containing a pseudo attribute with erb) defined targetting an existing pseudo attribute" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :set_attributes => 'img', :attributes => {'class' => '<%= hello_world %>'}) } let(:source) { "
    \" src=\"path/to/button.png\">
    " } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "
    \">
    " end end describe "with a single html surround override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :surround => "p", :text => "

    It's behind you!

    <%= render_original %>
    ") } let(:source) { "

    test

    " } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "

    It's behind you!

    test

    " end end describe "with a single erb surround override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :surround => "p", :text => "<% some_method('test') do %><%= render_original %><% end %>") } let(:source) { "

    test

    " } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", '').should == "<% some_method('test') do %>

    test

    <% end %>
    " end end describe "with a single surround override defined using :closing_selector" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :surround => "h1", :closing_selector => "p", :text => "<% some_method('test') do %><%= render_original %><% end %>") } let(:source) { "

    Start

    middle

    This is the end.

    " } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", '').should == "<% some_method('test') do %>

    Start

    middle

    This is the end.

    <% end %>
    " end end describe "with a single html surround_contents override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :surround_contents => "div", :text => "<%= render_original %>") } let(:source) { "

    yay!

    test

    " } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "

    yay!

    test

    " end end describe "with a single erb surround_contents override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :surround_contents => "p", :text => "<% if 1==1 %><%= render_original %><% end %>") } let(:source) { "

    test

    " } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "

    <% if 1==1 %>test<% end %>

    " end end describe "with a single erb surround_contents override defined using :closing_selector" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :surround_contents => "h1", :closing_selector => "p", :text => "<% if 1==1 %><%= render_original %><% end %>") } let(:source) { "

    Start

    middle

    child

    This is the end.

    " } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", '').should == "

    Start

    <% if 1==1 %>

    middle

    child

    <% end %>

    This is the end.

    " end end describe "with a single disabled override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove => "p", :text => "

    Argh!

    ", :disabled => true) } let(:source) { "

    test

    <%= raw(text) %>" } it "should return unmodified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "

    test

    <%= raw(text) %>" end end describe "with a single :copy override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "p", :copy => "h1") } let(:source) { "

    World

    Hello

    " } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "

    World

    Hello

    World

    " end end 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')"}) } let(:source) { "

    World

    <% if true %>

    True that!

    <% end %>

    Hello

    " } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<% if true %>

    True that!

    <% end %>

    World

    <% if true %>

    True that!

    <% end %>

    Hello

    " end end describe "with a single :cut override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "p", :cut => "h1") } let(:source) { "

    World

    Hello

    " } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "

    Hello

    World

    " end end 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')"}) } let(:source) { "

    World

    <% if true %>

    True that!

    <% end %>

    Hello

    " } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).should == "<% if true %>

    True that!

    <% end %>

    Hello

    " end end describe "with mulitple sequenced overrides defined" do before do Deface::Override.new(:virtual_path => "posts/index", :name => "third", :insert_after => "li:contains('second')", :text => "
  • third
  • ", :sequence => {:after => "second"}) Deface::Override.new(:virtual_path => "posts/index", :name => "second", :insert_after => "li", :text => "
  • second
  • ", :sequence => {:after => "first"}) Deface::Override.new(:virtual_path => "posts/index", :name => "first", :replace => "li", :text => "
  • first
  • ") end let(:source) { "" } it "should return modified source" do Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "").should == "" end end describe "with incompatible actions and :closing_selector" do let(:source) { "" } it "should return modified source" do [:insert_before, :insert_after, :insert_top, :insert_bottom, :set_attributes, :remove_from_attributes, :add_to_attributes].each do |action| Deface::Override.all.clear Deface::Override.new(:virtual_path => "posts/index", :name => "first", action => "li", :closing_selector => "p", :text => "
  • first
  • ") expect { Dummy.apply(source, {:virtual_path => "posts/index"}) }.to raise_error(Deface::NotSupportedError) end end end end end