Skip to content

Commit

Permalink
Add Polymer.DomApi.wrap
Browse files Browse the repository at this point in the history
- Make sure nodes are wrapped under ShadowDOMPolyfill
- Forwards `window.wrap` when defined
- Remove weird forked `DomApi` definition
- Remove calls to `wrap` from gestures
- Add tests
  • Loading branch information
dfreedm committed Nov 11, 2015
1 parent ce32459 commit 6cf974a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
21 changes: 8 additions & 13 deletions src/lib/dom-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,17 @@
var nativeCloneNode = Element.prototype.cloneNode;
var nativeImportNode = Document.prototype.importNode;

// ensure nodes are wrapped if SD polyfill is present
var needsToWrap = Settings.hasShadow && !Settings.nativeShadow;
var wrap = window.wrap ? window.wrap : function(node) { return node; };

var DomApi = function(node) {
this.node = node;
this.node = needsToWrap ? wrap(node) : node;
if (this.patch) {
this.patch();
}
};

// ensure nodes are wrapped if SD polyfill is present
if (window.wrap && Settings.useShadow && !Settings.useNativeShadow) {
DomApi = function(node) {
this.node = wrap(node);
if (this.patch) {
this.patch();
}
};
}

DomApi.prototype = {

flush: function() {
Expand All @@ -66,7 +60,7 @@

var n = node;
// wrap document for SD polyfill
var wrappedDocument = wrap(document);
var wrappedDocument = Polymer.DomApi.wrap(document);

// walk from node to `this` or `document`
while (n && n !== wrappedDocument && n !== this.node) {
Expand Down Expand Up @@ -977,7 +971,8 @@
hasInsertionPoint: hasInsertionPoint,
ctor: DomApi,
factory: factory,
hasDomApi: hasDomApi
hasDomApi: hasDomApi,
wrap: wrap
};

})();
Expand Down
6 changes: 3 additions & 3 deletions src/standard/gestures.html
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
handleNative: function(ev) {
var handled;
var type = ev.type;
var node = wrap(ev.currentTarget);
var node = Polymer.DomApi.wrap(ev.currentTarget);
var gobj = node[GESTURE_KEY];
if (!gobj) {
return;
Expand Down Expand Up @@ -296,7 +296,7 @@
// automate the event listeners for the native events
add: function(node, evType, handler) {
// SD polyfill: handle case where `node` is unwrapped, like `document`
node = wrap(node);
node = Polymer.DomApi.wrap(node);
var recognizer = this.gestures[evType];
var deps = recognizer.deps;
var name = recognizer.name;
Expand Down Expand Up @@ -329,7 +329,7 @@
// automate event listener removal for native events
remove: function(node, evType, handler) {
// SD polyfill: handle case where `node` is unwrapped, like `document`
node = wrap(node);
node = Polymer.DomApi.wrap(node);
var recognizer = this.gestures[evType];
var deps = recognizer.deps;
var name = recognizer.name;
Expand Down
8 changes: 8 additions & 0 deletions test/unit/polymer-dom-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,11 @@
});
</script>
</dom-module>

<dom-module id="x-wrapped">
<script>
Polymer({
is: 'x-wrapped'
});
</script>
</dom-module>
2 changes: 2 additions & 0 deletions test/unit/polymer-dom-shadow.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

<x-deep-contains></x-deep-contains>

<x-wrapped></x-wrapped>

<script src="polymer-dom.js"></script>

</body>
Expand Down
2 changes: 2 additions & 0 deletions test/unit/polymer-dom.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

<x-deep-contains></x-deep-contains>

<x-wrapped></x-wrapped>

<script src="polymer-dom.js"></script>

</body>
Expand Down
9 changes: 9 additions & 0 deletions test/unit/polymer-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,4 +936,13 @@ suite('Polymer.dom non-distributed elements', function() {

document.body.removeChild(separate);
});

test('Polymer.DomApi.wrap', function() {
var wrap = window.wrap || function(node) { return node; };

var node = document.querySelector('x-wrapped');
assert.equal(wrap(document), Polymer.DomApi.wrap(document), 'document should be wrapped');
assert.equal(wrap(node), Polymer.DomApi.wrap(node), 'node should be wrapped');
assert.equal(wrap(node), Polymer.dom(node).node, 'Polymer.dom should always wrap the input node');
});
});

0 comments on commit 6cf974a

Please sign in to comment.