Skip to content

Commit

Permalink
Recursively render until rendered unrenderable
Browse files Browse the repository at this point in the history
This allows us to simplify user-defined render() methods. One only
needs to return something structurally smaller. render() will
recurse on that structure until it (hopefully) terminates by
returning a DOM element.
  • Loading branch information
garious committed Aug 3, 2015
1 parent b61fcdd commit 1367d1d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 4 additions & 6 deletions dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ function addAttribute(e, subscriber, k, v) {
function setChildren(subscriber, e, xs) {
e.innerHTML = '';
for (var i = 0; i < xs.length; i++) {
var x = xs[i];
x = typeof x === 'string' ? global.document.createTextNode(x) : x;
if (typeof x.render === 'function') {
x = x.render();
}
var x = render(xs[i]);
e.appendChild(x);
}
}
Expand Down Expand Up @@ -219,8 +215,10 @@ function element(as) {
function render(e) {
if (typeof e === 'string') {
return global.document.createTextNode(e);
} else if (typeof e.render === 'function') {
return render(e.render());
}
return e.render();
return e;
}

module.exports = {
Expand Down
9 changes: 9 additions & 0 deletions dom_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ var eq = assert.deepEqual;
eq(dom.render('hello').data, 'hello');
})();

//
// Test recursive render.
//
(function testRenderRender() {
var e = dom.element({name: 'p', contents: 'hello'});
var component = {render: function() {return e;}}
eq(dom.render(component).tagName, 'P');
})();

//
// Test setting focus
//
Expand Down

0 comments on commit 1367d1d

Please sign in to comment.