Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3308. Use an explicit undefined check to test if logical tree … #3309

Merged
merged 2 commits into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions src/lib/dom-tree-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,39 @@
return node.__dom.childNodes;
},

// NOTE: __dom can be created under 2 conditions: (1) an element has a
// logical tree, or (2) an element is in a logical tree. In case (1), the
// element will store firstChild/lastChild, and in case (2), the element
// will store parentNode, nextSibling, previousSibling. This means that
// the mere existence of __dom is not enough to know if the requested
// logical data is available and instead we do an explicit undefined check.
getParentNode: function(node) {
return node.__dom && node.__dom.parentNode || node.parentNode;
return node.__dom && node.__dom.parentNode !== undefined ?
node.__dom.parentNode : node.parentNode;
},

getFirstChild: function(node) {
return node.__dom && node.__dom.firstChild || node.firstChild;
return node.__dom && node.__dom.firstChild !== undefined ?
node.__dom.firstChild : node.firstChild;
},

getLastChild: function(node) {
return node.__dom && node.__dom.lastChild || node.lastChild;
return node.__dom && node.__dom.lastChild !== undefined ?
node.__dom.lastChild : node.lastChild;
},

getNextSibling: function(node) {
return node.__dom && node.__dom.nextSibling || node.nextSibling;
return node.__dom && node.__dom.nextSibling !== undefined ?
node.__dom.nextSibling : node.nextSibling;
},

getPreviousSibling: function(node) {
return node.__dom && node.__dom.previousSibling || node.previousSibling;
return node.__dom && node.__dom.previousSibling !== undefined ?
node.__dom.previousSibling : node.previousSibling;
},

getFirstElementChild: function(node) {
return node.__dom && node.__dom.firstChild ?
return node.__dom && node.__dom.firstChild !== undefined ?
this._getFirstElementChild(node) : node.firstElementChild;
},

Expand All @@ -116,7 +127,7 @@
},

getLastElementChild: function(node) {
return node.__dom && node.__dom.lastChild ?
return node.__dom && node.__dom.lastChild !== undefined ?
this._getLastElementChild(node) : node.lastElementChild;
},

Expand All @@ -129,7 +140,7 @@
},

getNextElementSibling: function(node) {
return node.__dom && node.__dom.nextSibling ?
return node.__dom && node.__dom.nextSibling !== undefined ?
this._getNextElementSibling(node) : node.nextElementSibling;
},

Expand All @@ -142,7 +153,7 @@
},

getPreviousElementSibling: function(node) {
return node.__dom && node.__dom.previousSibling ?
return node.__dom && node.__dom.previousSibling !== undefined ?
this._getPreviousElementSibling(node) : node.previousElementSibling;
},

Expand Down Expand Up @@ -241,8 +252,11 @@
if (n) {
n.__dom.previousSibling = p;
}
// When an element is removed, logical data is no longer tracked.
// Explicitly set `undefined` here to indicate this. This is disginguished
// from `null` which is set if info is null.
node.__dom.parentNode = node.__dom.previousSibling =
node.__dom.nextSibling = null;
node.__dom.nextSibling = undefined;
// remove caching of childNodes
container.__dom.childNodes = null;
}
Expand Down
5 changes: 5 additions & 0 deletions test/unit/polymer-dom-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@
<script>Polymer({is: 'x-echo'});</script>
</dom-module>

<dom-module id="x-simple">
<template><div>simple</div></template>
<script>Polymer({is: 'x-simple'});</script>
</dom-module>


<dom-module id="x-redistribute-a-b">
<template>
Expand Down
30 changes: 30 additions & 0 deletions test/unit/polymer-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,36 @@ suite('Polymer.dom accessors', function() {
assert.equal(Polymer.dom(child).previousElementSibling, before, 'previousElementSibling incorrect');
});

test('Polymer.dom node accessors (empty logical tree)', function() {
var element = document.createElement('x-simple');
assert.equal(Polymer.dom(element).parentNode, null, 'parentNode incorrect');
assert.equal(Polymer.dom(element).firstChild, null, 'firstChild incorrect');
assert.equal(Polymer.dom(element).lastChild, null, 'lastChild incorrect');
assert.equal(Polymer.dom(element).nextSibling, null, 'nextSibling incorrect');
assert.equal(Polymer.dom(element).previousSibling, null, 'previousSibling incorrect');
assert.equal(Polymer.dom(element).firstElementChild, null, 'firstElementChild incorrect');
assert.equal(Polymer.dom(element).lastElementChild, null, 'lastElementChild incorrect');
assert.equal(Polymer.dom(element).nextElementSibling, null, 'nextElementSibling incorrect');
assert.equal(Polymer.dom(element).previousElementSibling, null, 'previousElementSibling incorrect');
});

test('Polymer.dom node accessors (unmanaged logical tree)', function() {
var element = document.createElement('div');
var child1 = document.createElement('div');
var child2 = document.createElement('div');
element.appendChild(child1);
element.appendChild(child2);
assert.equal(Polymer.dom(element).parentNode, null, 'parentNode incorrect');
assert.equal(Polymer.dom(element).firstChild, child1, 'firstChild incorrect');
assert.equal(Polymer.dom(element).lastChild, child2, 'lastChild incorrect');
assert.equal(Polymer.dom(element).nextSibling, null, 'nextSibling incorrect');
assert.equal(Polymer.dom(element).previousSibling, null, 'previousSibling incorrect');
assert.equal(Polymer.dom(element).firstElementChild, child1, 'firstElementChild incorrect');
assert.equal(Polymer.dom(element).lastElementChild, child2, 'lastElementChild incorrect');
assert.equal(Polymer.dom(element).nextElementSibling, null, 'nextElementSibling incorrect');
assert.equal(Polymer.dom(element).previousElementSibling, null, 'previousElementSibling incorrect');
});

test('Polymer.dom textContent', function() {
var testElement = document.createElement('x-project');
Polymer.dom(testElement).textContent = 'Hello World';
Expand Down