Shady patching: patch element accessors in composed tree; fixes HTMLImports polyfill support.

This commit is contained in:
Steven Orvell 2015-12-14 12:30:56 -08:00
parent c3fbd10da8
commit d135fef14a
3 changed files with 134 additions and 37 deletions

View File

@ -502,61 +502,28 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
firstElementChild: {
get: function() {
if (this.node.__firstChild) {
var n = this.node.__firstChild;
while (n && n.nodeType !== Node.ELEMENT_NODE) {
n = n.__nextSibling;
}
return n;
} else {
return this.node.firstElementChild;
}
return TreeApi.Logical.getFirstElementChild(this.node);
},
configurable: true
},
lastElementChild: {
get: function() {
if (this.node.__lastChild) {
var n = this.node.__lastChild;
while (n && n.nodeType !== Node.ELEMENT_NODE) {
n = n.__previousSibling;
}
return n;
} else {
return this.node.lastElementChild;
}
return TreeApi.Logical.getLastElementChild(this.node);
},
configurable: true
},
nextElementSibling: {
get: function() {
if (this.node.__nextSibling) {
var n = this.node.__nextSibling;
while (n && n.nodeType !== Node.ELEMENT_NODE) {
n = n.__nextSibling;
}
return n;
} else {
return this.node.nextElementSibling;
}
return TreeApi.Logical.getNextElementSibling(this.node);
},
configurable: true
},
previousElementSibling: {
get: function() {
if (this.node.__previousSibling) {
var n = this.node.__previousSibling;
while (n && n.nodeType !== Node.ELEMENT_NODE) {
n = n.__previousSibling;
}
return n;
} else {
return this.node.previousElementSibling;
}
return TreeApi.Logical.getPreviousElementSibling(this.node);
},
configurable: true
},

View File

@ -104,6 +104,54 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
return node.__previousSibling || TreeApi.Composed.getPreviousSibling(node);
},
getFirstElementChild: function(node) {
if (node.__firstChild) {
var n = node.__firstChild;
while (n && n.nodeType !== Node.ELEMENT_NODE) {
n = n.__nextSibling;
}
return n;
} else {
return TreeApi.Composed.getFirstElementChild(node);
}
},
getLastElementChild: function(node) {
if (node.__lastChild) {
var n = node.__lastChild;
while (n && n.nodeType !== Node.ELEMENT_NODE) {
n = n.__previousSibling;
}
return n;
} else {
return TreeApi.Composed.getLastElementChild(node);
}
},
getNextElementSibling: function(node) {
if (node.__nextSibling) {
var n = node.__nextSibling;
while (n && n.nodeType !== Node.ELEMENT_NODE) {
n = n.__nextSibling;
}
return n;
} else {
return TreeApi.Composed.getNextElementSibling(node);
}
},
getPreviousElementSibling: function(node) {
if (node.__previousSibling) {
var n = node.__previousSibling;
while (n && n.nodeType !== Node.ELEMENT_NODE) {
n = n.__previousSibling;
}
return n;
} else {
return TreeApi.Composed.getPreviousElementSibling(node);
}
},
// Capture the list of light children. It's important to do this before we
// start transforming the DOM into "rendered" state.
// Children may be added to this list dynamically. It will be treated as the
@ -222,6 +270,22 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
return node.previousSibling;
},
getFirstElementChild: function(node) {
return node.firstElementChild;
},
getLastElementChild: function(node) {
return node.lastElementChild;
},
getNextElementSibling: function(node) {
return node.nextElementSibling;
},
getPreviousElementSibling: function(node) {
return node.previousElementSibling;
},
// composed tracking needs to reset composed children here in case
// they may have already been set (this shouldn't happen but can
// if dependency ordering is incorrect and as a result upgrade order

View File

@ -27,6 +27,8 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
return;
}
count = 0;
var baseFinishDistribute = Polymer.Base._finishDistribute;
var TreeApi = Polymer.TreeApi;
var DomApi = Polymer.DomApi;
@ -116,6 +118,8 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
var log = false;
var count = 0;
var patchImpl = {
hasPrototypeDescriptors: Boolean(Object.getOwnPropertyDescriptor(
@ -338,6 +342,68 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
}
},
getFirstElementChild: function(node) {
if (node.__patched) {
var c$ = this.getChildNodes(node);
for (var i=0, n; i < c$.length; i++) {
n = c$[i];
if (n.nodeType === Node.ELEMENT_NODE) {
return n;
}
}
} else {
return node.firstElementChild;
}
},
getLastElementChild: function(node) {
if (node.__patched) {
var c$ = this.getChildNodes(node);
for (var i=c$.length, n; i >=0 ; i--) {
n = c$[i];
if (n.nodeType === Node.ELEMENT_NODE) {
return n;
}
}
} else {
return node.lastElementChild;
}
},
getNextElementSibling: function(node) {
if (node.__patched) {
var c$ = this.getChildNodes(node);
var i = c$.indexOf(node);
if (i >= 0) {
for (var n; i < c$.length; i++) {
n = c$[i];
if (n.nodeType === Node.ELEMENT_NODE) {
return n;
}
}
}
} else {
return node.nextElementSibling;
}
},
getPreviousElementSibling: function(node) {
if (node.__patched) {
var c$ = this.getChildNodes(node);
var i = c$.indexOf(node);
if (i >= 0) {
for (var n; i >=0 ; i--) {
n = c$[i];
if (n.nodeType === Node.ELEMENT_NODE) {
return n;
}
}
}
} else {
return node.previousElementSibling;
}
},
// composed tracking needs to reset composed children here in case
// they may have already been set (this shouldn't happen but can
// if dependency ordering is incorrect and as a result upgrade order