Skip to content

Commit 306cc81

Browse files
author
Steven Orvell
committed
Use linked-list for element tree traversal. Factor Polymer.DomApi into shadow/shady modules.
1 parent cfa6d51 commit 306cc81

12 files changed

Lines changed: 1127 additions & 963 deletions

‎src/lib/dom-api-classlist.html‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
var DomApi = Polymer.DomApi.ctor;
1616

17+
var useShadow = Polymer.Settings.useShadow;
18+
1719
/**
1820
* DomApi.classList allows maniuplation of `classList` compatible with
1921
* Polymer.dom. The general usage is
@@ -36,20 +38,28 @@
3638
}
3739

3840
DomApi.ClassList.prototype = {
41+
3942
add: function() {
4043
this.node.classList.add.apply(this.node.classList, arguments);
41-
this.domApi._distributeParent();
44+
this._distributeParent();
4245
},
4346

4447
remove: function() {
4548
this.node.classList.remove.apply(this.node.classList, arguments);
46-
this.domApi._distributeParent();
49+
this._distributeParent();
4750
},
4851

4952
toggle: function() {
5053
this.node.classList.toggle.apply(this.node.classList, arguments);
51-
this.domApi._distributeParent();
54+
this._distributeParent();
55+
},
56+
57+
_distributeParent: function() {
58+
if (!useShadow) {
59+
this.domApi._distributeParent();
60+
}
5261
},
62+
5363
contains: function() {
5464
return this.node.classList.contains.apply(this.node.classList,
5565
arguments);

‎src/lib/dom-api-shadow.html‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<!--
2+
@license
3+
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7+
Code distributed by Google as part of the polymer project is also
8+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9+
-->
10+
<script>
11+
(function() {
12+
'use strict';
13+
14+
var Settings = Polymer.Settings;
15+
var TreeApi = Polymer.TreeApi;
16+
var DomApi = Polymer.DomApi;
17+
18+
// *************** Configure DomApi for Shadow DOM!! ***************
19+
if (!Settings.useShadow) {
20+
return;
21+
}
22+
23+
Polymer.Base.extend(DomApi.prototype, {
24+
25+
querySelectorAll: function(selector) {
26+
return TreeApi.arrayCopy(this.node.querySelectorAll(selector));
27+
},
28+
29+
getOwnerRoot: function() {
30+
var n = this.node;
31+
while (n) {
32+
if (n.nodeType === Node.DOCUMENT_FRAGMENT_NODE && n.host) {
33+
return n;
34+
}
35+
n = n.parentNode;
36+
}
37+
},
38+
39+
importNode: function(externalNode, deep) {
40+
var doc = this.node instanceof Document ? this.node :
41+
this.node.ownerDocument;
42+
return doc.importNode(externalNode, deep);
43+
},
44+
45+
getDestinationInsertionPoints: function() {
46+
var n$ = this.node.getDestinationInsertionPoints &&
47+
this.node.getDestinationInsertionPoints();
48+
return n$ ? TreeApi.arrayCopy(n$) : [];
49+
},
50+
51+
getDistributedNodes: function() {
52+
var n$ = this.node.getDistributedNodes &&
53+
this.node.getDistributedNodes();
54+
return n$ ? TreeApi.arrayCopy(n$) : [];
55+
}
56+
57+
});
58+
59+
Object.defineProperties(DomApi.prototype, {
60+
61+
childNodes: {
62+
get: function() {
63+
return TreeApi.arrayCopyChildNodes(this.node);
64+
},
65+
configurable: true
66+
},
67+
68+
children: {
69+
get: function() {
70+
return TreeApi.arrayCopyChildren(this.node);
71+
},
72+
configurable: true
73+
},
74+
75+
// textContent / innerHTML
76+
textContent: {
77+
get: function() {
78+
return this.node.textContent;
79+
},
80+
set: function(value) {
81+
return this.node.textContent = value;
82+
},
83+
configurable: true
84+
},
85+
86+
innerHTML: {
87+
get: function() {
88+
return this.node.innerHTML;
89+
},
90+
set: function(value) {
91+
return this.node.innerHTML = value;
92+
},
93+
configurable: true
94+
}
95+
96+
});
97+
98+
var forwardMethods = function(m$) {
99+
for (var i=0; i < m$.length; i++) {
100+
forwardMethod(m$[i]);
101+
}
102+
};
103+
104+
var forwardMethod = function(method) {
105+
DomApi.prototype[method] = function() {
106+
return this.node[method].apply(this.node, arguments);
107+
}
108+
};
109+
110+
forwardMethods(['cloneNode', 'appendChild', 'insertBefore',
111+
'removeChild', 'replaceChild', 'setAttribute', 'removeAttribute',
112+
'querySelector']);
113+
114+
var forwardProperties = function(f$) {
115+
for (var i=0; i < f$.length; i++) {
116+
forwardProperty(f$[i]);
117+
}
118+
};
119+
120+
var forwardProperty = function(name) {
121+
Object.defineProperty(DomApi.prototype, name, {
122+
get: function() {
123+
return this.node[name];
124+
},
125+
configurable: true
126+
});
127+
};
128+
129+
forwardProperties(['parentNode', 'firstChild', 'lastChild',
130+
'nextSibling', 'previousSibling', 'firstElementChild',
131+
'lastElementChild', 'nextElementSibling', 'previousElementSibling']);
132+
133+
})();
134+
</script>

0 commit comments

Comments
 (0)