Skip to content

Commit

Permalink
Use linked-list for element tree traversal. Factor Polymer.DomApi int…
Browse files Browse the repository at this point in the history
…o shadow/shady modules.
  • Loading branch information
Steven Orvell committed Nov 19, 2015
1 parent cfa6d51 commit 306cc81
Show file tree
Hide file tree
Showing 12 changed files with 1,127 additions and 963 deletions.
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._distributeParent();
}
},

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>
Loading

0 comments on commit 306cc81

Please sign in to comment.