Skip to content

Commit

Permalink
Changes based on review.
Browse files Browse the repository at this point in the history
Re-use code in Polymer.dom when in noPatch mode.
  • Loading branch information
Steven Orvell committed Dec 8, 2018
1 parent 42b13d0 commit 8954c25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 81 deletions.
97 changes: 17 additions & 80 deletions lib/legacy/polymer.dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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
*/
import '../utils/boot.js';

import { wrap } from '../utils/wrap.js';
import '../utils/settings.js';
import { FlattenedNodesObserver } from '../utils/flattened-nodes-observer.js';
export { flush, enqueueDebouncer as addDebouncer } from '../utils/flush.js';
Expand Down Expand Up @@ -93,15 +93,15 @@ export let DomApi = class {
* @override
*/
deepContains(node) {
if (this.node.contains(node)) {
if (wrap(this.node).contains(node)) {
return true;
}
let n = node;
let doc = node.ownerDocument;
// walk from node to `this` or `document`
while (n && n !== doc && n !== this.node) {
// use logical parentnode, or native ShadowRoot host
n = n.parentNode || n.host;
n = wrap(n).parentNode || wrap(n).host;
}
return n === this.node;
}
Expand All @@ -116,7 +116,7 @@ export let DomApi = class {
* @override
*/
getOwnerRoot() {
return this.node.getRootNode();
return wrap(this.node).getRootNode();
}

/**
Expand All @@ -128,7 +128,7 @@ export let DomApi = class {
*/
getDistributedNodes() {
return (this.node.localName === 'slot') ?
this.node.assignedNodes({flatten: true}) :
wrap(this.node).assignedNodes({flatten: true}) :
[];
}

Expand All @@ -140,10 +140,10 @@ export let DomApi = class {
*/
getDestinationInsertionPoints() {
let ip$ = [];
let n = this.node.assignedSlot;
let n = wrap(this.node).assignedSlot;
while (n) {
ip$.push(n);
n = n.assignedSlot;
n = wrap(n).assignedSlot;
}
return ip$;
}
Expand All @@ -159,7 +159,7 @@ export let DomApi = class {
importNode(node, deep) {
let doc = this.node instanceof Document ? this.node :
this.node.ownerDocument;
return doc.importNode(node, deep);
return wrap(doc).importNode(node, deep);
}

/**
Expand Down Expand Up @@ -380,77 +380,14 @@ DomApi.prototype.innerHTML;

if (window.ShadyDOM && window.ShadyDOM.inUse && window.ShadyDOM.noPatch) {

class Wrapper extends ShadyDOM.Wrapper {

observeNodes(callback) {
return new FlattenedNodesObserver(
/** @type {!HTMLElement} */(this.node), callback);
}
class Wrapper extends ShadyDOM.Wrapper {}

unobserveNodes(observerHandle) {
observerHandle.disconnect();
// copy bespoke API onto wrapper
Object.getOwnPropertyNames(DomApi.prototype).forEach((prop) => {
if (prop != 'activeElement') {
Wrapper.prototype[prop] = DomApi.prototype[prop];
}

notifyObserver() {}

deepContains(node) {
if (this.contains(node)) {
return true;
}
let n = node;
let doc = node.ownerDocument;
// walk from node to `this` or `document`
while (n && n !== doc && n !== this.node) {
// use logical parentnode, or native ShadowRoot host
n = this.parentNode || n.host;
}
return n === this.node;
}

getOwnerRoot() {
return this.getRootNode();
}

getDistributedNodes() {
return (this.node.localName === 'slot') ?
this.assignedNodes({flatten: true}) :
[];
}

getDestinationInsertionPoints() {
let ip$ = [];
let n = this.assignedSlot;
while (n) {
ip$.push(n);
n = ShadyDOM.wrap(n).assignedSlot;
}
return ip$;
}

importNode(node, deep) {
let doc = this.node instanceof Document ? this.node :
this.node.ownerDocument;
return ShadyDOM.wrap(doc).importNode(node, deep);
}

getEffectiveChildNodes() {
return FlattenedNodesObserver.getFlattenedNodes(
/** @type {!HTMLElement} */ (this));
}

queryDistributedElements(selector) {
let c$ = this.getEffectiveChildNodes();
let list = [];
for (let i=0, l=c$.length, c; (i<l) && (c=c$[i]); i++) {
if ((c.nodeType === Node.ELEMENT_NODE) &&
matchesSelector(c, selector)) {
list.push(c);
}
}
return list;
}

}
});

DomApi = Wrapper;

Expand Down Expand Up @@ -510,14 +447,14 @@ export const dom = function(obj) {
if (obj instanceof DomApi || obj instanceof EventApi) {
return obj;
}
if (!obj['__domApi']) {
let helper;
let helper = obj['__domApi'];
if (!helper) {
if (obj instanceof Event) {
helper = new EventApi(obj);
} else {
helper = new DomApi(obj);
}
obj['__domApi'] = helper;
}
return obj['__domApi'];
return helper;
};
2 changes: 1 addition & 1 deletion lib/utils/templatize.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class TemplateInstanceBase extends base {
this.root = this._stampTemplate(this.__dataHost);
// Save list of stamped children
let children = this.children = [];
// TODO(sorvell): probably ok not to use `wrap` here.
// Polymer 1.x did not use `Polymer.dom` here so not bothering.
for (let n = this.root.firstChild; n; n=n.nextSibling) {
children.push(n);
n.__templatizeInstance = this;
Expand Down

0 comments on commit 8954c25

Please sign in to comment.