# encoding: UTF-8 require 'spec_helper' module Deface describe Parser do describe "#convert" do it "should parse html fragment" do Deface::Parser.convert("
test
") parsed.should be_an_instance_of(Nokogiri::HTML::Document) parsed = parsed.to_s.split("\n") if RUBY_PLATFORM == 'java' parsed.should eq ["test
"] else parsed = parsed[1..-1] parsed.should eq ["test
"] end end # Regression test for #84, #100 it "should parse html document with erb in the head" do parsed = Deface::Parser.convert("<%= method_name %>") parsed.should be_an_instance_of(Nokogiri::HTML::Document) parsed = parsed.to_s.split("\n") if RUBY_PLATFORM == 'java' parsed.should == [">
").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 if RUBY_PLATFORM == 'java' it "should convert <% ... %> contains double quoted value" do Deface::Parser.convert(">
").to_s.should == "" end 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 tag = Deface::Parser.convert(">
") tag = tag.css('p').first tag.attributes['data-erb-id'].value.should eq '<% method_name %>' tag = Deface::Parser.convert("alt=\"test\">
") tag = tag.css('p').first tag.attributes['data-erb-id'].value.should eq '<% method_name %>' tag.attributes['alt'].value.should eq 'test' end it "should convert multiple <% ... %> inside html tag" do tag = Deface::Parser.convert(%q{alt="<% x = 'y' + \"2\" %>" title='<% method_name %>' <%= other_method %>
}) tag = tag.css('p').first tag.attributes['data-erb-0'].value.should == "<%= method_name %>" tag.attributes['data-erb-1'].value.should == "<%= other_method %>" tag.attributes['data-erb-alt'].value.should == "<% x = 'y' + \n \\\"2\\\" %>" tag.attributes['data-erb-title'].value.should == "<% method_name %>" end it "should convert <%= ... %> including href attribute" do tag = Deface::Parser.convert(%(">A Link)) tag = tag.css('a').first tag.attributes['data-erb-href'].value.should eq "<%= x 'y' + \"z\" %>" tag.text.should eq 'A Link' end it "should escape contents erb tags" do tag = Deface::Parser.convert("<% method_name :key => 'value' %>") tag = tag.css('erb').first 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?('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?('silent').should be_true tag.text.should eq " method_name( :key => 'value' ) " end it "should respect valid encoding tag" do source = %q{<%# encoding: ISO-8859-1 %>Can you say ümlaut?} Deface::Parser.convert(source) source.encoding.name.should == 'ISO-8859-1' end it "should force default encoding" do source = %q{Can you say ümlaut?} source.force_encoding('ISO-8859-1') Deface::Parser.convert(source) source.encoding.should == Encoding.default_external end it "should force default encoding" do source = %q{<%# encoding: US-ASCII %>Can you say ümlaut?} lambda { Deface::Parser.convert(source) }.should raise_error(ActionView::WrongEncodingError) end end describe "#undo_erb_markup" do it "should revert<% 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 if RUBY_PLATFORM == 'java' it "should revert data-erb-x containing double quoted value" do Deface::Parser.undo_erb_markup!("").should == %(>
) end end 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 end end end