# encoding: utf-8 require_relative 'common' describe 'Sanitize' do describe 'initializer' do it 'should not modify a transformers array in the given config' do transformers = [ lambda {} ] Sanitize.new({ :transformers => transformers }) transformers.length.must_equal(1) end end describe 'instance methods' do before do @s = Sanitize.new end describe '#document' do before do @s = Sanitize.new(:elements => ['html']) end it 'should sanitize an HTML document' do @s.document('Lorem ipsum dolor sit
amet ') .must_equal "Lorem ipsum dolor sit amet alert(\"hello world\");\n" end it 'should not modify the input string' do input = 'foo' @s.document(input) input.must_equal('foo') end it 'should not choke on frozen documents' do @s.document('foo'.freeze).must_equal "foo\n" end end describe '#fragment' do it 'should sanitize an HTML fragment' do @s.fragment('Lorem ipsum dolor sit
amet ') .must_equal 'Lorem ipsum dolor sit amet alert("hello world");' end it 'should not modify the input string' do input = 'foo' @s.fragment(input) input.must_equal 'foo' end it 'should not choke on fragments containing or ' do @s.fragment('foo').must_equal 'foo' @s.fragment('foo').must_equal 'foo' @s.fragment('foo').must_equal 'foo' @s.fragment('foo').must_equal 'foo' end it 'should not choke on frozen fragments' do @s.fragment('foo'.freeze).must_equal 'foo' end end describe '#node!' do it 'should sanitize a Nokogiri::XML::Node' do doc = Nokogiri::HTML5.parse('Lorem ipsum dolor sit
amet ') frag = doc.fragment doc.xpath('/html/body/node()').each {|node| frag << node } @s.node!(frag) frag.to_html.must_equal 'Lorem ipsum dolor sit amet alert("hello world");' end describe "when the given node is a document and isn't whitelisted" do it 'should raise a Sanitize::Error' do doc = Nokogiri::HTML5.parse('foo') proc { @s.node!(doc) }.must_raise Sanitize::Error end end end end describe 'class methods' do describe '.document' do it 'should call #document' do Sanitize.stub_instance(:document, proc {|html| html + ' called' }) do Sanitize.document('foo') .must_equal 'foo called' end end end describe '.fragment' do it 'should call #fragment' do Sanitize.stub_instance(:fragment, proc {|html| html + ' called' }) do Sanitize.fragment('foo').must_equal 'foo called' end end end describe '.node!' do it 'should call #node!' do Sanitize.stub_instance(:node!, proc {|input| input + ' called' }) do Sanitize.node!('not really a node').must_equal 'not really a node called' end end end end end