require 'spec_helper' module Deface describe Parser do describe "#convert" do it "should parse html fragment" do Deface::Parser.convert("

Hello

").should be_an_instance_of(Nokogiri::HTML::DocumentFragment) Deface::Parser.convert("

Hello

").to_s.should == "

Hello

" Deface::Parser.convert("Hello").should be_an_instance_of(Nokogiri::HTML::DocumentFragment) Deface::Parser.convert("Hello").to_s.should == "Hello" end it "should parse html document" do parsed = Deface::Parser.convert("Hellotest") parsed.should be_an_instance_of(Nokogiri::HTML::Document) parsed = parsed.to_s.split("\n")[1..-1] parsed.should == "\nHello\ntest\n".split("\n") #ignore doctype added by noko parsed = Deface::Parser.convert("test") parsed.should be_an_instance_of(Nokogiri::HTML::Document) parsed = parsed.to_s.split("\n")[1..-1] parsed.should == "test".split("\n") #ignore doctype added by noko parsed = Deface::Parser.convert("

test

") parsed.should be_an_instance_of(Nokogiri::HTML::Document) parsed = parsed.to_s.split("\n")[1..-1] parsed.should == "

test

".split("\n") #ignore doctype added by noko end it "should convert <% ... %>" do Deface::Parser.convert("<% method_name %>").to_s.should == " method_name " end it "should convert <%= ... %>" do Deface::Parser.convert("<%= method_name %>").to_s.should == " method_name " end it "should convert first <% ... %> inside html tag" do Deface::Parser.convert("

>

").to_s.should == "

" end it "should convert second <% ... %> inside html tag" do Deface::Parser.convert("

<% x = y %>>

").to_s.should == "

" end it "should convert <% ... %> inside double quoted attr value" do Deface::Parser.convert("

\">

").to_s.should == "

" end it "should convert <% ... %> inside single quoted attr value" do Deface::Parser.convert("

").to_s.should == "

" end it "should convert <% ... %> inside non-quoted attr value" do Deface::Parser.convert("

>

").to_s.should == "

" Deface::Parser.convert("

alt=\"test\">

").to_s.should == "

" end it "should convert multiple <% ... %> inside html tag" do Deface::Parser.convert(%q{

alt="<% x = 'y' + \"2\" %>" title='<% method_name %>' <%= other_method %>

}).to_s.should == "

" end it "should convert <%= ... %> including href attribute" do Deface::Parser.convert(%(">A Link)).to_s.should == "A Link" end it "should escape contents code tags" do Deface::Parser.convert("<% method_name(:key => 'value') %>").to_s.should == " method_name(:key => 'value') " end end describe "#undo_erb_markup" do 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 %>" end it "should revert data-erb-x attrs inside html tag" do Deface::Parser.undo_erb_markup!("

").should == "

<% x = y %>>

" end it "should revert data-erb-id attr inside html tag" do Deface::Parser.undo_erb_markup!("

").should == "

1 %>\">

" end it "should revert data-erb-href attr inside html tag" do Deface::Parser.undo_erb_markup!("A Link").should == %(">A Link) end it "should unescape contents of code 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 end end end