mirror of
https://github.com/atom/atom.git
synced 2026-01-23 22:08:08 -05:00
Add Builder.render and instance methods for tags
Builder.render takes a function that calls tag methods and returns a view fragment.
This commit is contained in:
@@ -6,6 +6,19 @@ describe "Builder", ->
|
||||
|
||||
beforeEach -> builder = new Builder
|
||||
|
||||
describe "@render", ->
|
||||
fit "runs the given function in a fresh builder instance and returns the resulting view fragment", ->
|
||||
fragment =
|
||||
Builder.render ->
|
||||
@div =>
|
||||
@ol class: 'cool-list', =>
|
||||
@li()
|
||||
@li()
|
||||
|
||||
expect(fragment).toMatchSelector('div')
|
||||
expect(fragment.find('ol.cool-list')).toExist()
|
||||
expect(fragment.find('li').length).toBe 2
|
||||
|
||||
describe ".tag(name, args...)", ->
|
||||
it "can generate simple tags", ->
|
||||
builder.tag 'div'
|
||||
|
||||
@@ -6,6 +6,11 @@ Text = require 'template/text'
|
||||
|
||||
module.exports =
|
||||
class Builder
|
||||
@render: (fn) ->
|
||||
builder = new this
|
||||
fn.call(builder)
|
||||
builder.toFragment()
|
||||
|
||||
@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
|
||||
@@ -18,6 +23,13 @@ class Builder
|
||||
void: 'area base br col command embed hr img input keygen link meta param
|
||||
source track wbr'.split /\s+/
|
||||
|
||||
|
||||
@allElements: ->
|
||||
@elements.normal.concat(@elements.void)
|
||||
|
||||
_.each @allElements(), (tagName) =>
|
||||
@prototype[tagName] = (args...) -> @tag(tagName, args...)
|
||||
|
||||
constructor: ->
|
||||
@reset()
|
||||
|
||||
@@ -37,7 +49,7 @@ class Builder
|
||||
@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")
|
||||
throw new Error("Self-closing tag #{name} cannot have text or content")
|
||||
else
|
||||
options.content?()
|
||||
@text(options.text) if options.text
|
||||
|
||||
Reference in New Issue
Block a user