-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use linked-list for element tree traversal. Factor Polymer.DomApi int…
…o shadow/shady modules.
- Loading branch information
Steven Orvell
committed
Nov 19, 2015
1 parent
cfa6d51
commit 306cc81
Showing
12 changed files
with
1,127 additions
and
963 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.