From b73a6dece28f0f6bb4dc87a7c5386b1cbaac10f5 Mon Sep 17 00:00:00 2001 From: David Frank Date: Sun, 12 Jul 2015 02:21:05 +0800 Subject: [PATCH] make sure we lookup key attribute recursively --- dist.js | 11 ++++++----- index.js | 11 ++++++----- test/test.js | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/dist.js b/dist.js index 7eeae19..f80ab12 100644 --- a/dist.js +++ b/dist.js @@ -105,7 +105,7 @@ function createVirtualDomNode(el, attr) { return new VNode( el.tagName , createProperties(el) - , createChildren(el) + , createChildren(el, attr) , key , ns ); @@ -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; diff --git a/index.js b/index.js index 19ed193..a95236a 100644 --- a/index.js +++ b/index.js @@ -104,7 +104,7 @@ function createVirtualDomNode(el, attr) { return new VNode( el.tagName , createProperties(el) - , createChildren(el) + , createChildren(el, attr) , key , ns ); @@ -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; diff --git a/test/test.js b/test/test.js index f5ea47a..e18818e 100644 --- a/test/test.js +++ b/test/test.js @@ -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 = '

test

'; + 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'); + }); });