mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Add template/Builder. It generates basic tags.
This commit is contained in:
16
spec/stdlib/template/builder-spec.coffee
Normal file
16
spec/stdlib/template/builder-spec.coffee
Normal file
@@ -0,0 +1,16 @@
|
||||
Builder = require 'template/builder'
|
||||
|
||||
fdescribe "Builder", ->
|
||||
builder = null
|
||||
|
||||
beforeEach -> builder = new Builder
|
||||
|
||||
describe ".tag(name, args...)", ->
|
||||
it "can generate simple tags", ->
|
||||
builder.tag 'div'
|
||||
expect(builder.toHtml()).toBe("<div></div>")
|
||||
|
||||
builder.reset()
|
||||
builder.tag 'ol'
|
||||
expect(builder.toHtml()).toBe("<ol></ol>")
|
||||
|
||||
25
src/stdlib/template/builder.coffee
Normal file
25
src/stdlib/template/builder.coffee
Normal file
@@ -0,0 +1,25 @@
|
||||
_ = require 'underscore'
|
||||
OpenTag = require 'template/open-tag'
|
||||
CloseTag = require 'template/close-tag'
|
||||
|
||||
module.exports =
|
||||
class Builder
|
||||
constructor: ->
|
||||
@reset()
|
||||
|
||||
toHtml: ->
|
||||
_.map(@document, (x) -> x.toHtml()).join('')
|
||||
|
||||
tag: (name) ->
|
||||
@openTag(name)
|
||||
@closeTag(name)
|
||||
|
||||
openTag: (name) ->
|
||||
@document.push(new OpenTag(name))
|
||||
|
||||
closeTag: (name) ->
|
||||
@document.push(new CloseTag(name))
|
||||
|
||||
reset: ->
|
||||
@document = []
|
||||
|
||||
7
src/stdlib/template/close-tag.coffee
Normal file
7
src/stdlib/template/close-tag.coffee
Normal file
@@ -0,0 +1,7 @@
|
||||
module.exports =
|
||||
class CloseTag
|
||||
constructor: (@name) ->
|
||||
|
||||
toHtml: ->
|
||||
"</#{@name}>"
|
||||
|
||||
7
src/stdlib/template/open-tag.coffee
Normal file
7
src/stdlib/template/open-tag.coffee
Normal file
@@ -0,0 +1,7 @@
|
||||
module.exports =
|
||||
class OpenTag
|
||||
constructor: (@name) ->
|
||||
|
||||
toHtml: ->
|
||||
"<#{@name}>"
|
||||
|
||||
Reference in New Issue
Block a user