Skip to content

Commit

Permalink
Merge pull request #3112 from Polymer/shady-linked
Browse files Browse the repository at this point in the history
ShadyDOM: use a linked-list for tree accessors
  • Loading branch information
kevinpschaaf committed Dec 16, 2015
2 parents dd34fc6 + 8c95014 commit 273ab0f
Show file tree
Hide file tree
Showing 22 changed files with 1,730 additions and 1,090 deletions.
4 changes: 4 additions & 0 deletions polymer.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@
},

_initFeatures: function() {
// setup gestures
this._setupGestures();
// manage configuration
this._setupConfigure();
// setup style properties
this._setupStyleProperties();
// setup debouncers
this._setupDebouncers();
// setup shady
this._setupShady();
this._registerHost();
if (this._template) {
// manage local dom
Expand Down
18 changes: 11 additions & 7 deletions src/lib/annotations/annotations.html
Original file line number Diff line number Diff line change
Expand Up @@ -366,18 +366,22 @@

// instance-time

_localSubTree: function(node, host) {
return (node === host) ? node.childNodes :
(node._lightChildren || node.childNodes);
},

findAnnotatedNode: function(root, annote) {
// recursively ascend tree until we hit root
var parent = annote.parent &&
Polymer.Annotations.findAnnotatedNode(root, annote.parent);
// unwind the stack, returning the indexed node at each level
return !parent ? root :
Polymer.Annotations._localSubTree(parent, root)[annote.index];
if (parent) {
// note: marginally faster than indexing via childNodes
// (http://jsperf.com/childnodes-lookup)
for (var n=parent.firstChild, i=0; n; n=n.nextSibling) {
if (annote.index === i++) {
return n;
}
}
} else {
return root;
}
}

};
Expand Down
1 change: 1 addition & 0 deletions src/lib/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

createdCallback: function() {
Polymer.telemetry.instanceCount++;
this.isAttached = false;
this.root = this;
this._doBehavior('created'); // abstract
this._initFeatures(); // abstract
Expand Down
16 changes: 13 additions & 3 deletions src/lib/dom-api-classlist.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

var DomApi = Polymer.DomApi.ctor;

var useShadow = Polymer.Settings.useShadow;

/**
* DomApi.classList allows maniuplation of `classList` compatible with
* Polymer.dom. The general usage is
Expand All @@ -36,20 +38,28 @@
}

DomApi.ClassList.prototype = {

add: function() {
this.node.classList.add.apply(this.node.classList, arguments);
this.domApi._distributeParent();
this._distributeParent();
},

remove: function() {
this.node.classList.remove.apply(this.node.classList, arguments);
this.domApi._distributeParent();
this._distributeParent();
},

toggle: function() {
this.node.classList.toggle.apply(this.node.classList, arguments);
this.domApi._distributeParent();
this._distributeParent();
},

_distributeParent: function() {
if (!useShadow) {
this.domApi._maybeDistributeParent();
}
},

contains: function() {
return this.node.classList.contains.apply(this.node.classList,
arguments);
Expand Down
134 changes: 134 additions & 0 deletions src/lib/dom-api-shadow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<script>
(function() {
'use strict';

var Settings = Polymer.Settings;
var TreeApi = Polymer.TreeApi;
var DomApi = Polymer.DomApi;

// *************** Configure DomApi for Shadow DOM!! ***************
if (!Settings.useShadow) {
return;
}

Polymer.Base.extend(DomApi.prototype, {

querySelectorAll: function(selector) {
return TreeApi.arrayCopy(this.node.querySelectorAll(selector));
},

getOwnerRoot: function() {
var n = this.node;
while (n) {
if (n.nodeType === Node.DOCUMENT_FRAGMENT_NODE && n.host) {
return n;
}
n = n.parentNode;
}
},

importNode: function(externalNode, deep) {
var doc = this.node instanceof Document ? this.node :
this.node.ownerDocument;
return doc.importNode(externalNode, deep);
},

getDestinationInsertionPoints: function() {
var n$ = this.node.getDestinationInsertionPoints &&
this.node.getDestinationInsertionPoints();
return n$ ? TreeApi.arrayCopy(n$) : [];
},

getDistributedNodes: function() {
var n$ = this.node.getDistributedNodes &&
this.node.getDistributedNodes();
return n$ ? TreeApi.arrayCopy(n$) : [];
}

});

Object.defineProperties(DomApi.prototype, {

childNodes: {
get: function() {
return TreeApi.arrayCopyChildNodes(this.node);
},
configurable: true
},

children: {
get: function() {
return TreeApi.arrayCopyChildren(this.node);
},
configurable: true
},

// textContent / innerHTML
textContent: {
get: function() {
return this.node.textContent;
},
set: function(value) {
return this.node.textContent = value;
},
configurable: true
},

innerHTML: {
get: function() {
return this.node.innerHTML;
},
set: function(value) {
return this.node.innerHTML = value;
},
configurable: true
}

});

var forwardMethods = function(m$) {
for (var i=0; i < m$.length; i++) {
forwardMethod(m$[i]);
}
};

var forwardMethod = function(method) {
DomApi.prototype[method] = function() {
return this.node[method].apply(this.node, arguments);
}
};

forwardMethods(['cloneNode', 'appendChild', 'insertBefore',
'removeChild', 'replaceChild', 'setAttribute', 'removeAttribute',
'querySelector']);

var forwardProperties = function(f$) {
for (var i=0; i < f$.length; i++) {
forwardProperty(f$[i]);
}
};

var forwardProperty = function(name) {
Object.defineProperty(DomApi.prototype, name, {
get: function() {
return this.node[name];
},
configurable: true
});
};

forwardProperties(['parentNode', 'firstChild', 'lastChild',
'nextSibling', 'previousSibling', 'firstElementChild',
'lastElementChild', 'nextElementSibling', 'previousElementSibling']);

})();
</script>

0 comments on commit 273ab0f

Please sign in to comment.