From b5a06c288efa35b5a6862ce6fa7484eecb30f90c Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 27 Dec 2011 16:05:01 -0600 Subject: [PATCH] Tags can take textual content. --- spec/stdlib/template/builder-spec.coffee | 9 +++++++++ src/stdlib/template/builder.coffee | 7 +++++++ src/stdlib/template/text.coffee | 6 ++++++ 3 files changed, 22 insertions(+) create mode 100644 src/stdlib/template/text.coffee diff --git a/spec/stdlib/template/builder-spec.coffee b/spec/stdlib/template/builder-spec.coffee index 28f1da96c..173a3bb60 100644 --- a/spec/stdlib/template/builder-spec.coffee +++ b/spec/stdlib/template/builder-spec.coffee @@ -20,3 +20,12 @@ fdescribe "Builder", -> builder.tag 'li' expect(builder.toHtml()).toBe("
") + + it "can generate tags with text", -> + builder.tag 'div', "hello" + expect(builder.toHtml()).toBe("
hello
") + + builder.reset() + builder.tag 'div', 22 + expect(builder.toHtml()).toBe("
22
") + diff --git a/src/stdlib/template/builder.coffee b/src/stdlib/template/builder.coffee index 41b6807cf..1677f4386 100644 --- a/src/stdlib/template/builder.coffee +++ b/src/stdlib/template/builder.coffee @@ -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 = [] diff --git a/src/stdlib/template/text.coffee b/src/stdlib/template/text.coffee new file mode 100644 index 000000000..5f20e1d2e --- /dev/null +++ b/src/stdlib/template/text.coffee @@ -0,0 +1,6 @@ +module.exports = +class Text + constructor: (@string) -> + + toHtml: -> @string +