Skip to content

Commit dc4f2b7

Browse files
authored
Merge pull request #89 from PolymerLabs/shady-prep
Fix tests under Shady Dom.
2 parents dd302e3 + 521f91d commit dc4f2b7

11 files changed

+163
-199
lines changed

src/shady/flush.html

-104
This file was deleted.

src/shady/patch.html

+12-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@
3535
return;
3636
}
3737
if (!node.__patched && patchImpl.canPatchNode(node)) {
38-
Tree.Logical.saveChildNodes(node);
39-
if (!Tree.Composed.hasParentNode(node)) {
40-
Tree.Composed.saveParentNode(node);
41-
}
42-
Tree.Composed.saveChildNodes(node);
38+
Tree.saveChildNodes(node);
4339
patchImpl.patch(node);
4440
}
4541
};
@@ -134,6 +130,17 @@
134130
ShadyDom.patchImpl = patchImpl;
135131

136132
var createRootAndEnsurePatched = function(node, useV0) {
133+
// TODO(sorvell): need to ensure ancestors are patched but this introduces
134+
// a timing problem with gathering composed children.
135+
// (1) currently the child list is crawled and patched when patching occurs
136+
// (this needs to change)
137+
// (2) we can only patch when an element has received its parsed children
138+
// because we cannot detect them when inserted by parser.
139+
// var ancestor = node;
140+
// while (ancestor) {
141+
// ShadyDom.patch(ancestor);
142+
// ancestor = ancestor.parentNode || ancestor.host;
143+
// }
137144
ShadyDom.patch(node);
138145
var root = new ShadyDom.ShadyRoot(node, useV0);
139146
ShadyDom.patch(root);

src/shady/shady.html

+31-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
Code distributed by Google as part of the polymer project is also
88
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
99
-->
10-
<link rel="import" href="../utils/debounce.html">
1110
<link rel="import" href="utils.html">
1211
<link rel="import" href="array-splice.html">
1312
<link rel="import" href="tree.html">
@@ -16,7 +15,6 @@
1615
<!-- <link rel="import" href="lib/dom-api-distributed-nodes-observer.html"> -->
1716
<link rel="import" href="distributor.html">
1817
<link rel="import" href="patch.html">
19-
<link rel="import" href="flush.html">
2018
<script>
2119

2220
(function() {
@@ -73,9 +71,9 @@
7371
//console.log('update from', this.host, 'root', distributionRoot.host, distributionRoot._clean);
7472
if (distributionRoot._clean) {
7573
distributionRoot._clean = false;
76-
//console.log('triggered render on', distributionRoot.host);
77-
distributionRoot.debounceRender()
78-
ShadyDom.addDebouncer(distributionRoot.debounceRender());
74+
ShadyDom.enqueue(function() {
75+
distributionRoot.render();
76+
});
7977
}
8078
},
8179

@@ -101,19 +99,7 @@
10199
}
102100
},
103101

104-
// async render this root!
105-
debounceRender: function() {
106-
this._renderDebouncer = ShadyDom.debounce(this._renderDebouncer,
107-
this.render, null, this);
108-
return this._renderDebouncer;
109-
},
110-
111102
render: function() {
112-
//console.log('render', this.host);
113-
if (this._renderDebouncer) {
114-
this._renderDebouncer.cancel();
115-
this._renderDebouncer = null;
116-
}
117103
if (!this._clean) {
118104
if (!this._skipUpdateInsertionPoints) {
119105
this.updateInsertionPoints();
@@ -314,6 +300,34 @@
314300

315301
ShadyDom.ShadyRoot = ShadyRoot;
316302

303+
// render enqueuer
304+
ShadyDom.enqueue = function(callback) {
305+
if (!this.flush._scheduled) {
306+
this.flush._scheduled = true;
307+
Promise.resolve().then(() => { this.flush(); });
308+
}
309+
this.flush._list.push(callback);
310+
}
311+
312+
var customElements = window.CustomElements;
313+
314+
// render flusher
315+
ShadyDom.flush = function() {
316+
var list = this.flush._list;
317+
while (list.length) {
318+
list.shift()();
319+
}
320+
if (customElements) {
321+
customElements.takeRecords();
322+
}
323+
// continue flushing after elements are upgraded...
324+
if (list.length) {
325+
this.flush();
326+
}
327+
}
328+
329+
ShadyDom.flush._list = [];
330+
317331
})();
318332

319333
</script>

src/shady/tree.html

+45-16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
copy[i] = a$[i];
4848
}
4949
return copy;
50+
},
51+
52+
saveChildNodes: function(node) {
53+
this.Logical.saveChildNodes(node);
54+
if (!this.Composed.hasParentNode(node)) {
55+
this.Composed.saveParentNode(node);
56+
}
57+
this.Composed.saveChildNodes(node);
5058
}
5159

5260
};
@@ -274,6 +282,30 @@
274282

275283
}
276284

285+
// for testing...
286+
var descriptors = {};
287+
function getNativeProperty(element, property) {
288+
if (!descriptors[property]) {
289+
descriptors[property] = Object.getOwnPropertyDescriptor(
290+
HTMLElement.prototype, property) ||
291+
Object.getOwnPropertyDescriptor(
292+
Element.prototype, property) ||
293+
Object.getOwnPropertyDescriptor(
294+
Node.prototype, property);
295+
}
296+
return descriptors[property].get.call(element);
297+
}
298+
299+
// for testing...
300+
function assertNative(element, property, tracked) {
301+
var native = getNativeProperty(element, property);
302+
if (native != tracked && element.__patched) {
303+
console.warn('tracked', tracked, 'native', native);
304+
}
305+
return tracked;
306+
}
307+
308+
277309
// TODO(sorvell): composed tree manipulation is made available
278310
// (1) to maninpulate the composed tree, and (2) to track changes
279311
// to the tree for optional patching pluggability.
@@ -494,25 +526,12 @@
494526
insertBefore: function(parentNode, newChild, refChild) {
495527
this.saveChildNodes(parentNode);
496528
// remove from current location.
497-
if (this.hasParentNode(newChild)) {
498-
var oldParent = this.getParentNode(newChild);
499-
if (oldParent) {
500-
this._removeChild(oldParent, newChild);
501-
}
502-
}
503529
this._addChild(parentNode, newChild, refChild);
504530
return nativeInsertBefore.call(parentNode, newChild, refChild || null);
505531
},
506532

507533
appendChild: function(parentNode, newChild) {
508534
this.saveChildNodes(parentNode);
509-
// remove from current location.
510-
if (this.hasParentNode(newChild)) {
511-
var oldParent = this.getParentNode(newChild);
512-
if (oldParent) {
513-
this._removeChild(oldParent, newChild);
514-
}
515-
}
516535
this._addChild(parentNode, newChild);
517536
return nativeAppendChild.call(parentNode, newChild);
518537
},
@@ -527,10 +546,18 @@
527546
},
528547

529548
_addChild: function(parentNode, newChild, refChild) {
530-
if (newChild.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
549+
var isFrag = (newChild.nodeType === Node.DOCUMENT_FRAGMENT_NODE);
550+
var oldParent = this.getParentNode(newChild);
551+
if (oldParent) {
552+
this._removeChild(oldParent, newChild);
553+
}
554+
if (isFrag) {
531555
var c$ = this.getChildNodes(newChild);
532-
for (var j=0; j < c$.length; j++) {
533-
this.recordInsertBefore(c$[j], parentNode, refChild);
556+
for (var i=0; i < c$.length; i++) {
557+
var c = c$[i];
558+
// unlink document fragment children
559+
this._removeChild(newChild, c);
560+
this.recordInsertBefore(c, parentNode, refChild);
534561
}
535562
} else {
536563
this.recordInsertBefore(newChild, parentNode, refChild);
@@ -545,5 +572,7 @@
545572

546573
ShadyDom.Tree = Tree;
547574

575+
ShadyDom.getNativeProperty = getNativeProperty;
576+
548577
})();
549578
</script>

src/shady/utils.html

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
ShadyDom.force = (location.search.indexOf('forceShadyDom=true') >= 0);
2020
}
2121

22-
// BREAKME(sorvell): dependency on polymer.
23-
ShadyDom.inUse = ShadyDom.force ||
24-
(Polymer.shadowDomV0 && !Element.prototype.createShadowRoot) ||
25-
(!Polymer.shadowDomV0 && !Element.prototype.attachShadow);
22+
ShadyDom.inUse = ShadyDom.force || !Element.prototype.attachShadow;
2623

2724
var p = Element.prototype;
2825
var matches = p.matches || p.matchesSelector ||

src/templatizer/dom-if.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@
7575
detached: function() {
7676
if (!this.parentNode ||
7777
(this.parentNode.nodeType == Node.DOCUMENT_FRAGMENT_NODE &&
78-
(!Polymer.Settings.hasShadow ||
79-
!(this.parentNode instanceof ShadowRoot)))) {
78+
!this.parentNode.host)) {
8079
this._teardownInstance();
8180
}
8281
},

src/templatizer/dom-repeat.html

+2
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@
743743
}
744744
var beforeRow = this._instances[idx + 1];
745745
var beforeNode = beforeRow && !beforeRow.isPlaceholder ? beforeRow.children[0] : this;
746+
// TODO(sorvell): remove when fragment patching is auto-magic.
747+
ShadyDom.patch(inst.root);
746748
this.parentNode.insertBefore(inst.root, beforeNode);
747749
this._instances[idx] = inst;
748750
return inst;

test/unit/behaviors.html

+9-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@
221221
'propChanged2(prop)'
222222
]);
223223
this.registeredCount++;
224-
this.registeredThis = this;
224+
this.registeredProps = [this.prop1, this.prop2, this.prop3, this.prop4];
225+
this.registeredBehaviors = this.behaviors;
225226
},
227+
prop1: true,
226228
ready: function() {
227229
this.ensureAttributes({
228230
attr: true
@@ -237,12 +239,14 @@
237239
};
238240

239241
Polymer.registerBehavior2 ={
242+
prop2: true,
240243
registered: function() {
241244
this.registeredCount++;
242245
}
243246
};
244247

245248
Polymer.registerBehavior3 ={
249+
prop3: true,
246250
registered: function() {
247251
this.registeredCount++;
248252
}
@@ -254,6 +258,7 @@
254258
Polymer.registerBehavior2,
255259
Polymer.registerBehavior3
256260
],
261+
prop4: true,
257262

258263
registered: function() {
259264
this.registeredCount++;
@@ -348,7 +353,9 @@
348353
test('called once for each behavior with access to element prototype', function() {
349354
var el = fixture('registered');
350355
assert.equal(el.registeredCount, 4);
351-
assert.equal(el.registeredThis, Object.getPrototypeOf(el));
356+
assert.equal(el.registeredBehaviors.length, 3);
357+
assert.equal(el.registeredBehaviors, el.behaviors);
358+
assert.deepEqual(el.registeredProps, [true, true, true, true]);
352359
});
353360

354361
});

0 commit comments

Comments
 (0)