describe Html::Element do
# Helper to shortcut a lot of typing...
def build(*args, &block)
Html::Element.build(*args, &block)
end
it 'should build standard tags' do
Html::Element.new('b').render.should == ''
Html::Element.build('script').should include('')
end
it 'should build singleton tags' do
build('img').should == ''
build('input').should == ''
end
it 'should build attributes' do
build('img', :src => '/foo.jpg').should == ''
build('span', :id => 'alpha', :class => 'bold').should == ''
end
it 'should support basic text' do
build('span', 'heya').should == 'heya'
end
it 'should html escape basic text' do
build('span', '"boo!"').should == '"boo!"'
end
it 'should html escape attr values' do
build('a', :alt => 'We had "fun"').should == ''
end
it 'should support indenting' do
Html::Element.new('div', 'indent!').render.should == "\n
\n indent!\n
\n"
end
it 'should support block indenting by skipping initial newline' do
Html::Element.new('div', 'indent!').render(0,true).should == "
\n indent!\n
\n"
end
it 'should add attributes when called as setters' do
span = Html::Element.new(:span)
span.class = 'dynamic'
span.class += ' frenetic'
span.id = 'header'
span.attrs.keys.should include(:class, :id)
span.render.should == ''
end
it 'should add attributes using []=' do
div = Html::Element.new(:div)
div['foo'] = 2
div.attrs.keys.should include(:foo)
div.render.should == "\n\n"
end
it 'should allow reading attributes using []' do
div = Html::Element.new(:div, :bar => 'hi')
div[:bar].should == 'hi'
end
it 'should yield a block for customization on creation' do
Html::Element.new(:span) {|span|
span.id = 'fun'
}.render.should == ''
end
it 'should contain HTML' do
Html::Element.new(:span).html.should be_a(Html)
end
it 'should allow setting the inner html' do
span = Html::Element.new(:span)
span.html = 'some text'
span.render.should == 'some text'
span.html = Html.new.strong('more text!')
span.render.should == 'more text!'
end
end