Skip to content

Commit e3b3baa

Browse files
author
Steven Orvell
committed
Update to match 2.x branch
1 parent c3bd4d6 commit e3b3baa

File tree

9 files changed

+65
-58
lines changed

9 files changed

+65
-58
lines changed

lib/elements/dom-if.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Debouncer } from '../utils/debounce.js';
1414
import { enqueueDebouncer, flush } from '../utils/flush.js';
1515
import { microTask } from '../utils/async.js';
1616
import { root } from '../utils/path.js';
17+
import { wrap } from '../utils/wrap.js';
1718

1819
/**
1920
* The `<dom-if>` element will stamp a light-dom `<template>` child when
@@ -120,9 +121,9 @@ export class DomIf extends PolymerElement {
120121
*/
121122
disconnectedCallback() {
122123
super.disconnectedCallback();
123-
if (!this.parentNode ||
124-
(this.parentNode.nodeType == Node.DOCUMENT_FRAGMENT_NODE &&
125-
!this.parentNode.host)) {
124+
const parent = Polymer.wrap(this).parentNode;
125+
if (!parent || (parent.nodeType == Node.DOCUMENT_FRAGMENT_NODE &&
126+
!Polymer.wrap(parent).host)) {
126127
this.__teardownInstance();
127128
}
128129
}
@@ -174,15 +175,15 @@ export class DomIf extends PolymerElement {
174175
}
175176

176177
__ensureInstance() {
177-
let parentNode = this.parentNode;
178+
let parentNode = wrap(this).parentNode;
178179
// Guard against element being detached while render was queued
179180
if (parentNode) {
180181
if (!this.__ctor) {
181-
let template = /** @type {HTMLTemplateElement} */(this.querySelector('template'));
182+
let template = /** @type {HTMLTemplateElement} */(wrap(this).querySelector('template'));
182183
if (!template) {
183184
// Wait until childList changes and template should be there by then
184185
let observer = new MutationObserver(() => {
185-
if (this.querySelector('template')) {
186+
if (wrap(this).querySelector('template')) {
186187
observer.disconnect();
187188
this.__render();
188189
} else {
@@ -219,16 +220,16 @@ export class DomIf extends PolymerElement {
219220
}
220221
if (!this.__instance) {
221222
this.__instance = new this.__ctor();
222-
parentNode.insertBefore(this.__instance.root, this);
223+
wrap(parentNode).insertBefore(this.__instance.root, this);
223224
} else {
224225
this.__syncHostProperties();
225226
let c$ = this.__instance.children;
226227
if (c$ && c$.length) {
227228
// Detect case where dom-if was re-attached in new position
228-
let lastChild = this.previousSibling;
229+
let lastChild = wrap(this).previousSibling;
229230
if (lastChild !== c$[c$.length-1]) {
230231
for (let i=0, n; (i<c$.length) && (n=c$[i]); i++) {
231-
parentNode.insertBefore(n, this);
232+
wrap(parentNode).insertBefore(n, this);
232233
}
233234
}
234235
}
@@ -253,12 +254,12 @@ export class DomIf extends PolymerElement {
253254
let c$ = this.__instance.children;
254255
if (c$ && c$.length) {
255256
// use first child parent, for case when dom-if may have been detached
256-
let parent = c$[0].parentNode;
257+
let parent = wrap(c$[0]).parentNode;
257258
// Instance children may be disconnected from parents when dom-if
258259
// detaches if a tree was innerHTML'ed
259260
if (parent) {
260261
for (let i=0, n; (i<c$.length) && (n=c$[i]); i++) {
261-
parent.removeChild(n);
262+
wrap(parent).removeChild(n);
262263
}
263264
}
264265
}

lib/elements/dom-repeat.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { enqueueDebouncer, flush } from '../utils/flush.js';
1515
import { OptionalMutableData } from '../mixins/mutable-data.js';
1616
import { matches, translate } from '../utils/path.js';
1717
import { timeOut, microTask } from '../utils/async.js';
18+
import { wrap } from '../utils/wrap.js';
1819

1920
/**
2021
* @constructor
@@ -323,7 +324,7 @@ export class DomRepeat extends domRepeatBase {
323324
// only perform attachment if the element was previously detached.
324325
if (this.__isDetached) {
325326
this.__isDetached = false;
326-
let parent = this.parentNode;
327+
let parent = wrap(this).parentNode;
327328
for (let i=0; i<this.__instances.length; i++) {
328329
this.__attachInstance(i, parent);
329330
}
@@ -585,14 +586,14 @@ export class DomRepeat extends domRepeatBase {
585586
let inst = this.__instances[idx];
586587
for (let i=0; i<inst.children.length; i++) {
587588
let el = inst.children[i];
588-
inst.root.appendChild(el);
589+
wrap(inst.root).appendChild(el);
589590
}
590591
return inst;
591592
}
592593

593594
__attachInstance(idx, parent) {
594595
let inst = this.__instances[idx];
595-
parent.insertBefore(inst.root, this);
596+
wrap(parent).insertBefore(inst.root, this);
596597
}
597598

598599
__detachAndRemoveInstance(idx) {
@@ -625,7 +626,7 @@ export class DomRepeat extends domRepeatBase {
625626
}
626627
let beforeRow = this.__instances[instIdx + 1];
627628
let beforeNode = beforeRow ? beforeRow.children[0] : this;
628-
this.parentNode.insertBefore(inst.root, beforeNode);
629+
wrap(wrap(this).parentNode).insertBefore(inst.root, beforeNode);
629630
this.__instances[instIdx] = inst;
630631
return inst;
631632
}

lib/legacy/legacy-element-mixin.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ import { setTouchAction } from '../utils/gestures.js';
2020
import { Debouncer } from '../utils/debounce.js';
2121
import { timeOut, microTask } from '../utils/async.js';
2222
import { get } from '../utils/path.js';
23+
import { wrap } from '../utils/wrap.js';
2324

2425
let styleInterface = window.ShadyCSS;
2526

26-
let wrap = (n) => n;
27-
2827
/**
2928
* Element class mixin that provides Polymer's "legacy" API intended to be
3029
* backward-compatible to the greatest extent possible with the API
@@ -988,22 +987,6 @@ export const LegacyElementMixin = dedupingMixin((base) => {
988987

989988
}
990989

991-
if (window.ShadyDOM && window.ShadyDOM.inUse && window.ShadyDOM.noPatch) {
992-
wrap = ShadyDOM.wrap;
993-
994-
LegacyElement.prototype._attachDom = function(dom) {
995-
const w = this.__wrapper = ShadyDOM.wrap(this);
996-
if (dom) {
997-
if (!w.shadowRoot) {
998-
w.attachShadow({mode: 'open'});
999-
}
1000-
w.shadowRoot.appendChild(dom);
1001-
return w.shadowRoot;
1002-
}
1003-
return null;
1004-
};
1005-
}
1006-
1007990
LegacyElement.prototype.is = '';
1008991

1009992
return LegacyElement;

lib/legacy/polymer.dom.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,12 @@ DomApi.prototype.innerHTML;
379379

380380

381381
if (window.ShadyDOM && window.ShadyDOM.inUse && window.ShadyDOM.noPatch) {
382+
382383
class Wrapper extends ShadyDOM.Wrapper {
384+
383385
observeNodes(callback) {
384386
return new FlattenedNodesObserver(
385-
/** @type {!HTMLElement} */(this.node), callback, dom);
387+
/** @type {!HTMLElement} */(this.node), callback);
386388
}
387389

388390
unobserveNodes(observerHandle) {
@@ -433,7 +435,7 @@ if (window.ShadyDOM && window.ShadyDOM.inUse && window.ShadyDOM.noPatch) {
433435

434436
getEffectiveChildNodes() {
435437
return FlattenedNodesObserver.getFlattenedNodes(
436-
/** @type {!HTMLElement} */ (this), dom);
438+
/** @type {!HTMLElement} */ (this));
437439
}
438440

439441
queryDistributedElements(selector) {
@@ -448,10 +450,6 @@ if (window.ShadyDOM && window.ShadyDOM.inUse && window.ShadyDOM.noPatch) {
448450
return list;
449451
}
450452

451-
get activeElement() {
452-
let node = this.node;
453-
return node._activeElement !== undefined ? node._activeElement : node.activeElement;
454-
}
455453
}
456454

457455
DomApi = Wrapper;
@@ -508,10 +506,10 @@ if (window.ShadyDOM && window.ShadyDOM.inUse && window.ShadyDOM.noPatch) {
508506
* @return {!DomApi|!EventApi} Wrapper providing either node API or event API
509507
*/
510508
export const dom = function(obj) {
509+
obj = obj || document;
511510
if (obj instanceof DomApi || obj instanceof EventApi) {
512511
return obj;
513512
}
514-
obj = obj || document;
515513
if (!obj['__domApi']) {
516514
let helper;
517515
if (obj instanceof Event) {

lib/mixins/element-mixin.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { pathFromUrl, resolveCss, resolveUrl } from '../utils/resolve-url.js';
1616
import { DomModule } from '../elements/dom-module.js';
1717
import { PropertyEffects } from './property-effects.js';
1818
import { PropertiesMixin } from './properties-mixin.js';
19+
import { wrap } from '../utils/wrap.js';
1920

2021
/**
2122
* Current Polymer version in Semver notation.
@@ -651,13 +652,14 @@ export const ElementMixin = dedupingMixin(base => {
651652
* @return {ShadowRoot} node to which the dom has been attached.
652653
*/
653654
_attachDom(dom) {
654-
if (this.attachShadow) {
655+
const n = wrap(this);
656+
if (n.attachShadow) {
655657
if (dom) {
656-
if (!this.shadowRoot) {
657-
this.attachShadow({mode: 'open'});
658+
if (!n.shadowRoot) {
659+
n.attachShadow({mode: 'open'});
658660
}
659-
this.shadowRoot.appendChild(dom);
660-
return this.shadowRoot;
661+
n.shadowRoot.appendChild(dom);
662+
return n.shadowRoot;
661663
}
662664
return null;
663665
} else {

lib/utils/flattened-nodes-observer.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import './boot.js';
1111

1212
import { calculateSplices } from './array-splice.js';
1313
import { microTask } from './async.js';
14+
import { wrap } from './wrap.js';
1415

1516
/**
1617
* Returns true if `node` is a slot element
@@ -81,16 +82,16 @@ export let FlattenedNodesObserver = class {
8182
* @nocollapse See https://github.com/google/closure-compiler/issues/2763
8283
*/
8384
// eslint-disable-next-line
84-
static getFlattenedNodes(node, wrapper = n => n) {
85-
const wrapped = wrapper(node);
85+
static getFlattenedNodes(node) {
86+
const wrapped = wrap(node);
8687
if (isSlot(node)) {
8788
node = /** @type {!HTMLSlotElement} */(node); // eslint-disable-line no-self-assign
8889
return wrapped.assignedNodes({flatten: true});
8990
} else {
9091
return Array.from(wrapped.childNodes).map((node) => {
9192
if (isSlot(node)) {
9293
node = /** @type {!HTMLSlotElement} */(node); // eslint-disable-line no-self-assign
93-
return wrapper(node).assignedNodes({flatten: true});
94+
return wrap(node).assignedNodes({flatten: true});
9495
} else {
9596
return [node];
9697
}
@@ -104,12 +105,11 @@ export let FlattenedNodesObserver = class {
104105
* or removals from the target's list of flattened nodes.
105106
*/
106107
// eslint-disable-next-line
107-
constructor(target, callback, wrapper = n=> n) {
108+
constructor(target, callback) {
108109
/**
109110
* @type {MutationObserver}
110111
* @private
111112
*/
112-
this._wrap = wrapper;
113113
this._shadyChildrenObserver = null;
114114
/**
115115
* @type {MutationObserver}
@@ -147,9 +147,9 @@ export let FlattenedNodesObserver = class {
147147
connect() {
148148
if (isSlot(this._target)) {
149149
this._listenSlots([this._target]);
150-
} else if (this._wrap(this._target).children) {
150+
} else if (wrap(this._target).children) {
151151
this._listenSlots(
152-
/** @type {!NodeList<!Node>} */ (this._wrap(this._target).children));
152+
/** @type {!NodeList<!Node>} */ (wrap(this._target).children));
153153
if (window.ShadyDOM) {
154154
this._shadyChildrenObserver =
155155
ShadyDOM.observeChildren(this._target, (mutations) => {
@@ -178,9 +178,9 @@ export let FlattenedNodesObserver = class {
178178
disconnect() {
179179
if (isSlot(this._target)) {
180180
this._unlistenSlots([this._target]);
181-
} else if (this._wrap(this._target).children) {
181+
} else if (wrap(this._target).children) {
182182
this._unlistenSlots(
183-
/** @type {!NodeList<!Node>} */ (this._wrap(this._target).children));
183+
/** @type {!NodeList<!Node>} */ (wrap(this._target).children));
184184
if (window.ShadyDOM && this._shadyChildrenObserver) {
185185
ShadyDOM.unobserveChildren(this._shadyChildrenObserver);
186186
this._shadyChildrenObserver = null;

lib/utils/templatize.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ import './boot.js';
4949

5050
import { PropertyEffects } from '../mixins/property-effects.js';
5151
import { MutableData } from '../mixins/mutable-data.js';
52-
import { strictTemplatePolicy } from '../utils/settings.js';
52+
import { strictTemplatePolicy } from '../settings.js';
53+
import { wrap } from '../wrap.js';
5354

5455
// Base class for HTMLTemplateElement extension that has property effects
5556
// machinery for propagating host properties to children. This is an ES5
@@ -115,6 +116,7 @@ class TemplateInstanceBase extends base {
115116
this.root = this._stampTemplate(this.__dataHost);
116117
// Save list of stamped children
117118
let children = this.children = [];
119+
// TODO(sorvell): probably ok not to use `wrap` here.
118120
for (let n = this.root.firstChild; n; n=n.nextSibling) {
119121
children.push(n);
120122
n.__templatizeInstance = this;
@@ -219,11 +221,11 @@ class TemplateInstanceBase extends base {
219221
} else if (n.localName === 'slot') {
220222
if (hide) {
221223
n.__polymerReplaced__ = document.createComment('hidden-slot');
222-
n.parentNode.replaceChild(n.__polymerReplaced__, n);
224+
wrap(wrap(n).parentNode).replaceChild(n.__polymerReplaced__, n);
223225
} else {
224226
const replace = n.__polymerReplaced__;
225227
if (replace) {
226-
replace.parentNode.replaceChild(n, replace);
228+
wrap(wrap(replace).parentNode).replaceChild(n, replace);
227229
}
228230
}
229231
}
@@ -580,7 +582,7 @@ export function modelForElement(template, node) {
580582
} else {
581583
// Still in a template scope, keep going up until
582584
// a __templatizeInstance is found
583-
node = node.parentNode;
585+
node = wrap(node).parentNode;
584586
}
585587
}
586588
return null;

lib/utils/wrap.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
@license
3+
Copyright (c) 2017 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+
11+
// Node wrapper to ensure ShadowDOM safe operation regardless of polyfill
12+
// presence or mode. Note that with the introduction of `ShadyDOM.noPatch`,
13+
// a node wrapper must be used to access ShadowDOM API.
14+
// This is similar to using `Polymer.dom` but relies exclusively
15+
// on the presence of the ShadyDOM polyfill rather than requiring the loading
16+
// of legacy (Polymer.dom) API.
17+
export const wrap = (window.ShadyDOM && window.ShadyDOM.noPatch) ?
18+
window.ShadyDOM.wrap : (n) => n;
19+

test/runner.html

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
'unit/dom-bind.html',
7171
'unit/array-selector.html',
7272
'unit/polymer-dom.html',
73+
'unit/polymer-dom-nopatch.html',
7374
'unit/polymer-dom-observeNodes.html',
7475
'unit/flattened-nodes-observer.html',
7576
// TODO: substitute for equivalent es6 import tests

0 commit comments

Comments
 (0)