Skip to content

Commit c6e4053

Browse files
committed
merge content.html changes from 0.8-preview into 0.8-simplex
1 parent a8bef54 commit c6e4053

File tree

3 files changed

+77
-51
lines changed

3 files changed

+77
-51
lines changed

src/features/base/content.html

+48-31
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626

2727
_bootContent: function() {
2828
this._useContent = this._useContent ||
29-
(this._template && this._template.content.querySelector('content'));
29+
Boolean(this._template &&
30+
this._template.content.querySelector('content'));
3031
},
3132

3233
poolContent: function() {
@@ -40,7 +41,7 @@
4041
root.host = this;
4142
// initialize the `root` pointers: `root` is guarenteed to always be
4243
// available, and be either `this` or `this.contentRoot`. By contrast,
43-
// `contentRoot` is only set if _useContent is true.
44+
// `contentRoot` is only set if _useContent is true.
4445
this.contentRoot = root;
4546
this.root = root;
4647
// TODO(jmesserly): ad-hoc signal for `ShadowDOM-lite-enhanced` root
@@ -53,12 +54,15 @@
5354
if (!this.contentRoot) {
5455
throw Error('poolContent() must be called before distributeContent()');
5556
}
57+
// NOTE: `contentRoot` is populated only for the first
58+
// distribution. After that dom should be in the composed tree and
59+
// distribution info should not be reset.
5660
// reset distributions
57-
this._resetLightTree(this.contentRoot);
61+
this._resetDistribution(this.contentRoot);
5862
// compute which nodes should be distributed where
5963
// TODO(jmesserly): this is simplified because we assume a single
6064
// ShadowRoot per host and no `<shadow>`.
61-
this._poolDistribution(this.contentRoot, this._poolPopulation());
65+
this._distributePool(this.contentRoot, this._collectPool());
6266
// update the real DOM to be the composed tree
6367
this._composeTree(this);
6468
},
@@ -93,11 +97,29 @@
9397
// still prefixed. Alternatively we could just polyfill it somewhere.
9498
// Note that the arguments are reversed from what you might expect.
9599
elementMatches: function(selector, node) {
96-
if (node === undefined) node = this;
100+
if (node === undefined) {
101+
node = this;
102+
}
97103
return matchesSelector.call(node, selector);
98104
},
99105

100-
_poolPopulation: function() {
106+
// Many of the following methods are all conceptually static, but they are
107+
// included here as "protected" methods to allow overriding.
108+
109+
_resetDistribution: function(node) {
110+
var children = getLightChildren(node);
111+
for (var i = 0; i < children.length; i++) {
112+
var child = children[i];
113+
if (isInsertionPoint(child)) {
114+
child._distributedNodes = [];
115+
} else if (child._destinationInsertionPoints) {
116+
child._destinationInsertionPoints = undefined;
117+
}
118+
this._resetDistribution(child);
119+
}
120+
},
121+
122+
_collectPool: function() {
101123
// Gather the pool of nodes that should be distributed. We will combine
102124
// these with the "content root" to arrive at the composed tree.
103125
var pool = [];
@@ -113,31 +135,17 @@
113135
return pool;
114136
},
115137

116-
// Many of the following methods are all conceptually static, but they are
117-
// included here as "protected" methods to allow overriding.
118-
119-
_resetLightTree: function(node) {
120-
var children = getLightChildren(node);
121-
for (var i = 0; i < children.length; i++) {
122-
var child = children[i];
123-
if (isInsertionPoint(child)) {
124-
child._distributedNodes = [];
125-
} else if (child._destinationInsertionPoints) {
126-
child._destinationInsertionPoints = undefined;
127-
}
128-
this._resetLightTree(child);
129-
}
130-
},
131-
132-
_poolDistribution: function(node, pool) {
133-
if (node.localName == 'content') {
138+
_distributePool: function(node, pool) {
139+
if (isInsertionPoint(node)) {
134140
// distribute nodes from the pool that this selector matches
135141
var content = node;
136142
var anyDistributed = false;
137143
for (var i = 0; i < pool.length; i++) {
138144
var node = pool[i];
139145
// skip nodes that were already used
140-
if (!node) continue;
146+
if (!node) {
147+
continue;
148+
}
141149
// distribute this node if it matches
142150
if (this._matchesContentSelect(node, content)) {
143151
distributeNodeInto(node, content);
@@ -159,7 +167,7 @@
159167
// recursively distribute.
160168
var children = getLightChildren(node);
161169
for (var i = 0; i < children.length; i++) {
162-
this._poolDistribution(children[i], pool);
170+
this._distributePool(children[i], pool);
163171
}
164172
},
165173

@@ -220,12 +228,18 @@
220228
_matchesContentSelect: function(node, contentElement) {
221229
var select = contentElement.getAttribute('select');
222230
// no selector matches all nodes (including text)
223-
if (!select) return true;
231+
if (!select) {
232+
return true;
233+
}
224234
select = select.trim();
225235
// same thing if it had only whitespace
226-
if (!select) return true;
236+
if (!select) {
237+
return true;
238+
}
227239
// selectors can only match Elements
228-
if (!(node instanceof Element)) return false;
240+
if (!(node instanceof Element)) {
241+
return false;
242+
}
229243
// only valid selectors can match:
230244
// TypeSelector
231245
// *
@@ -234,15 +248,18 @@
234248
// AttributeSelector
235249
// negation
236250
var validSelectors = /^(:not\()?[*.#[a-zA-Z_|]/;
237-
if (!validSelectors.test(select)) return false;
251+
if (!validSelectors.test(select)) {
252+
return false;
253+
}
254+
// TODO(sorvell): This try..catch seems unfortunate and will nerf
255+
// performance, can we remove?
238256
try {
239257
return this.elementMatches(select, node);
240258
} catch (ex) {
241259
// Invalid selector.
242260
return false;
243261
}
244262
}
245-
246263
});
247264

248265
function distributeNodeInto(child, insertionPoint) {

test/unit/ready-elements.html

+27-18
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,58 @@
66
}
77

88
var readyMixin = {
9+
moniker: function() {
10+
return this.tag + (this.id ? '#' + this.id : '')
11+
},
912
ready: function() {
10-
readyList.push(this.name);
13+
readyList.push(this.moniker());
1114
}
12-
}
15+
};
1316
</script>
1417

1518
<template>
1619
x-zot
1720
</template>
1821
<script>
19-
Polymer(extend({
20-
name: 'x-zot',
21-
22-
}, readyMixin));
22+
Polymer({
23+
tag: 'x-zot',
24+
mixins: [readyMixin]
25+
});
2326
</script>
2427

2528
<template>
2629
<x-zot></x-zot>
2730
</template>
2831
<script>
29-
Polymer(extend({
30-
name: 'x-bar',
31-
32-
}, readyMixin));
32+
Polymer({
33+
tag: 'x-bar',
34+
mixins: [readyMixin]
35+
});
3336
</script>
3437

3538
<template>
3639
<x-bar></x-bar>
3740
<x-bar></x-bar>
3841
</template>
3942
<script>
40-
Polymer(extend({
41-
name: 'x-foo',
42-
43-
}, readyMixin));
43+
Polymer({
44+
tag: 'x-foo',
45+
mixins: [readyMixin]
46+
});
4447
</script>
4548

4649
<template>
50+
<x-zot id="a">
51+
<x-zot id="b"></x-zot>
52+
<x-zot id="c">
53+
<x-zot id="d"></x-zot>
54+
</x-zot>
55+
</x-zot>
4756
<x-foo></x-foo>
4857
</template>
4958
<script>
50-
Polymer(extend({
51-
name: 'x-ready',
52-
53-
}, readyMixin));
59+
Polymer({
60+
tag: 'x-ready',
61+
mixins: [readyMixin]
62+
});
5463
</script>

test/unit/ready.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
suite('ready', function() {
2626

2727
test('element create in dom calls ready', function() {
28-
assert.deepEqual(readyList, ['x-ready', 'x-foo', 'x-bar', 'x-zot', 'x-bar', 'x-zot']);
28+
assert.deepEqual(readyList, ['x-ready', 'x-zot#a', 'x-zot#b', 'x-zot#c', 'x-zot#d', 'x-foo', 'x-bar', 'x-zot', 'x-bar', 'x-zot']);
2929
});
3030

3131
test('element create + attach calls ready', function() {
3232
clearReadyList();
3333
document.body.appendChild(document.createElement('x-ready'));
3434
CustomElements.takeRecords(document);
35-
assert.deepEqual(readyList, ['x-ready', 'x-foo', 'x-bar', 'x-zot', 'x-bar', 'x-zot']);
35+
assert.deepEqual(readyList, ['x-ready', 'x-zot#a', 'x-zot#b', 'x-zot#c', 'x-zot#d', 'x-foo', 'x-bar', 'x-zot', 'x-bar', 'x-zot']);
3636
});
3737

3838
});

0 commit comments

Comments
 (0)