Skip to content

Commit

Permalink
make sure we lookup key attribute recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
David Frank committed Jul 11, 2015
1 parent b103292 commit b73a6de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
11 changes: 6 additions & 5 deletions dist.js
Expand Up @@ -105,7 +105,7 @@ function createVirtualDomNode(el, attr) {
return new VNode(
el.tagName
, createProperties(el)
, createChildren(el)
, createChildren(el, attr)
, key
, ns
);
Expand All @@ -114,13 +114,14 @@ function createVirtualDomNode(el, attr) {
/**
* Recursively create vdom
*
* @param Object el Parent element
* @return Array Child vnode or vtext
* @param Object el Parent element
* @param String attr Attribute name that contains vdom key
* @return Array Child vnode or vtext
*/
function createChildren(el) {
function createChildren(el, attr) {
var children = [];
for (var i = 0; i < el.childNodes.length; i++) {
children.push(createNode(el.childNodes[i]));
children.push(createNode(el.childNodes[i], attr));
};

return children;
Expand Down
11 changes: 6 additions & 5 deletions index.js
Expand Up @@ -104,7 +104,7 @@ function createVirtualDomNode(el, attr) {
return new VNode(
el.tagName
, createProperties(el)
, createChildren(el)
, createChildren(el, attr)
, key
, ns
);
Expand All @@ -113,13 +113,14 @@ function createVirtualDomNode(el, attr) {
/**
* Recursively create vdom
*
* @param Object el Parent element
* @return Array Child vnode or vtext
* @param Object el Parent element
* @param String attr Attribute name that contains vdom key
* @return Array Child vnode or vtext
*/
function createChildren(el) {
function createChildren(el, attr) {
var children = [];
for (var i = 0; i < el.childNodes.length; i++) {
children.push(createNode(el.childNodes[i]));
children.push(createNode(el.childNodes[i], attr));
};

return children;
Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Expand Up @@ -515,4 +515,19 @@ describe('vdom-parser', function () {
expect(output.properties.id).to.be.undefined;
expect(output.properties.attributes['data-id']).to.equal('example');
});

it('should support optional key lookup, recursively', function () {
input = '<div id="abc"><p id="edf">test</p></div>';
output = parser(input, 'id');

expect(output.type).to.equal('VirtualNode');
expect(output.tagName).to.equal('DIV');
expect(output.key).to.equal('abc');

var children = output.children;
expect(children).to.have.length(1);
expect(children[0].type).to.equal('VirtualNode');
expect(children[0].tagName).to.equal('P');
expect(children[0].key).to.equal('edf');
});
});

0 comments on commit b73a6de

Please sign in to comment.