Tags can take textual content.

This commit is contained in:
Nathan Sobo 2011-12-27 16:05:01 -06:00
parent e570c5d454
commit b5a06c288e
3 changed files with 22 additions and 0 deletions

View File

@ -20,3 +20,12 @@ fdescribe "Builder", ->
builder.tag 'li'
expect(builder.toHtml()).toBe("<ol><li></li><li></li></ol>")
it "can generate tags with text", ->
builder.tag 'div', "hello"
expect(builder.toHtml()).toBe("<div>hello</div>")
builder.reset()
builder.tag 'div', 22
expect(builder.toHtml()).toBe("<div>22</div>")

View File

@ -1,6 +1,7 @@
_ = require 'underscore'
OpenTag = require 'template/open-tag'
CloseTag = require 'template/close-tag'
Text = require 'template/text'
module.exports =
class Builder
@ -14,12 +15,15 @@ class Builder
options = @extractOptions(args)
@openTag(name)
options.content?()
@text(options.text) if options.text
@closeTag(name)
extractOptions: (args) ->
options = {}
for arg in args
options.content = arg if _.isFunction(arg)
options.text = arg if _.isString(arg)
options.text = arg.toString() if _.isNumber(arg)
options
openTag: (name) ->
@ -28,6 +32,9 @@ class Builder
closeTag: (name) ->
@document.push(new CloseTag(name))
text: (string) ->
@document.push(new Text(string))
reset: ->
@document = []

View File

@ -0,0 +1,6 @@
module.exports =
class Text
constructor: (@string) ->
toHtml: -> @string