Builder correctly generates void (self-closing) tags.

This commit is contained in:
Nathan Sobo
2011-12-27 16:43:28 -06:00
parent 21fb88141e
commit bd79d9cd5d
2 changed files with 29 additions and 10 deletions

View File

@@ -35,3 +35,7 @@ fdescribe "Builder", ->
expect(fragment.attr('id')).toBe('foo')
expect(fragment.attr('class')).toBe('bar')
it "can generate self-closing tags", ->
builder.tag 'br', id: 'foo'
expect(builder.toHtml()).toBe('<br id="foo">')

View File

@@ -6,6 +6,18 @@ Text = require 'template/text'
module.exports =
class Builder
@elements:
normal: 'a abbr address article aside audio b bdi bdo blockquote body button
canvas caption cite code colgroup datalist dd del details dfn div dl dt em
fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header hgroup
html i iframe ins kbd label legend li map mark menu meter nav noscript object
ol optgroup option output p pre progress q rp rt ruby s samp script section
select small span strong style sub summary sup table tbody td textarea tfoot
th thead time title tr u ul video'.split /\s+/
void: 'area base br col command embed hr img input keygen link meta param
source track wbr'.split /\s+/
constructor: ->
@reset()
@@ -17,10 +29,19 @@ class Builder
tag: (name, args...) ->
options = @extractOptions(args)
@openTag(name, options.attributes)
options.content?()
@text(options.text) if options.text
@closeTag(name)
@document.push(new OpenTag(name, options.attributes))
if @elementIsVoid(name)
if (options.text? or options.content?)
throw new Error("Self-closing tag #{tag} cannot have text or content")
else
options.content?()
@text(options.text) if options.text
@document.push(new CloseTag(name))
elementIsVoid: (name) ->
_.contains(this.constructor.elements.void, name)
extractOptions: (args) ->
options = {}
@@ -31,12 +52,6 @@ class Builder
options.attributes = arg if _.isObject(arg)
options
openTag: (name, attributes) ->
@document.push(new OpenTag(name, attributes))
closeTag: (name) ->
@document.push(new CloseTag(name))
text: (string) ->
@document.push(new Text(string))