-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathstyling-cross-scope-var.html
629 lines (544 loc) · 17.2 KB
/
styling-cross-scope-var.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
<!doctype html>
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
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
-->
<html>
<head>
<meta charset="utf-8">
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<link rel="import" href="../../polymer.html">
</head>
<body>
<x-scope></x-scope>
<dom-module id="x-grand-child-scope">
<style>
:host {
display: block;
padding: 8px;
}
#scope {
border: var(--scope-var);
}
#child {
border: var(--child-scope-var);
}
#me {
border: var(--grand-child-scope-var);
}
</style>
<template>
<div id="me">x-grand-child-scope</div>
<div id="scope">From x-scope</div>
<div id="child">From x-child-scope</div>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({is: 'x-grand-child-scope'});
});
</script>
</dom-module>
<dom-module id="x-host-property">
<style>
:host {
display: block;
padding: 8px;
border: var(--scope-var);
}
</style>
<template>
Host property
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({is: 'x-host-property'});
});
</script>
</dom-module>
<dom-module id="x-child-scope">
<style>
:host {
display: block;
padding: 8px;
}
:root {
--gc4-scope: 5px solid green;
}
#me {
border: var(--child-scope-var);
}
#gc2 {
--grand-child-scope-var: 4px solid seagreen;
}
#gc4 {
--grand-child-scope-var:
var(--gc4-scope);
}
</style>
<template>
<div id="me">x-child-scope</div>
<x-grand-child-scope id="gc1"></x-grand-child-scope>
<x-grand-child-scope id="gc2"></x-grand-child-scope>
<x-grand-child-scope id="gc3"></x-grand-child-scope>
<x-grand-child-scope id="gc4"></x-grand-child-scope>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({is: 'x-child-scope'});
});
</script>
</dom-module>
<dom-module id="x-overrides">
<style>
:host {
border: 1px dashed gray;
margin: 8px;
padding: 8px;
display: block;
--grand-child-scope-var: var(--rename);
}
</style>
<template>
overrides:
<x-grand-child-scope id="gc1"></x-grand-child-scope>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({is: 'x-overrides'});
});
</script>
</dom-module>
<dom-module id="x-overrides2">
<style>
:host {
border: 1px dashed gray;
margin: 8px;
padding: 8px;
display: block;
}
:root {
--grand-child-scope-var: var(--rename);
}
</style>
<template>
overrides:
<x-grand-child-scope id="gc1"></x-grand-child-scope>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({is: 'x-overrides2'});
});
</script>
</dom-module>
<dom-module id="x-late">
<style>
:host {
border: var(--late);
display: block;
}
</style>
<template>
late
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({is: 'x-late'});
});
</script>
</dom-module>
<dom-module id="x-overrides3">
<style>
:host {
border: 1px dashed gray;
margin: 8px;
padding: 8px;
display: block;
}
:root {
--fillin: 16px;
}
</style>
<template>
overrides:
<x-late id="late"></x-late>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({is: 'x-overrides3'});
});
</script>
</dom-module>
<dom-module id="x-has-def">
<style>
:host {
border: var(--border, --defaultBorder);
margin: 8px;
padding: 8px;
display: block;
}
</style>
<template>
Element with default variable.
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({is: 'x-has-def'});
});
</script>
</dom-module>
<dom-module id="x-has-if">
<style>
.iffy {
border: var(--scope-var);
}
</style>
<template>
<template is="dom-if" if="{{gogo}}">
<div class="iffy">iffy</div>
</template>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-has-if',
properties: {
gogo: {value: true}
}
});
});
</script>
</dom-module>
<dom-module id="x-button">
<style>
:host {
display: block;
border: var(--button-border);
}
</style>
<template>
Button!
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-button',
extends: 'button'
});
});
</script>
</dom-module>
<dom-module id="x-dynamic">
<style>
:host {
display: block;
margin: 20px;
border: var(--dynamic);
}
</style>
<template>
Dynamic
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-dynamic'
});
});
</script>
</dom-module>
<dom-module id="x-scope">
<style>
:host {
x--invalid: 15px solid gray;
display: block;
padding: 8px;
--scope-var: 1px solid black;
--fallback: 7px solid orange;
--default1: var(--undefined, 6px solid yellow);
--default2: var(--undefined, --fallback);
--default3: var(--undefined, rgb(128, 200, 250));
--defaultBorder: 22px solid green;
--a: 10px;
--b: 5px;
--primary-color: rgb(128, 128, 128);
--late: var(--fillin);
--button-border: 16px solid tomato;
--after: 17px solid brown;
--end-term: 19px solid blue}
:root{--ws-term: 18px solid orange}
#me {
border: var(--scope-var);
}
x-child-scope {
--child-scope-var: 2px solid orange;
--grand-child-scope-var: 3px solid steelblue;
}
x-child-scope.special {
--child-scope-var: 12px solid orange;
}
#applyDefault1 {
border: var(--undefined, 6px solid yellow);
}
#applyDefault2 {
border: var(--undefined, --fallback);
}
#default1 {
border: var(--default1);
}
#default2 {
border: var(--default2);
}
#default3 {
padding: 8px;
background-color: var(--default3);
}
#defaultElement2 {
--defaultBorder: 23px solid goldenrod;
}
#overrides1a, #overrides1b, #overrides2 {
--rename: 8px solid navy;
}
#overrides1b, #overrides2 {
--grand-child-scope-var: 9px solid orange;
}
#overridesConcrete {
border: var(--scope-var);
border: 4px solid steelblue;
}
#calc {
border: solid red;
border-width: calc(var(--a) + var(--b));
}
#shadow {
box-shadow: 10px 10px 10px var(--primary-color);
-webkit-box-shadow: 10px 10px 10px var(--primary-color);
}
x-host-property {
border: 10px solid purple;
}
#invalid {
border: var(--invalid);
}
#after::after {
content: 'after';
border: var(--after);
}
#wsTerm {
border: var(--ws-term)
}
#endTerm {border: var(--end-term)}
</style>
<template>
<div id="me">x-scope</div>
<x-child-scope id="child"></x-child-scope>
<x-child-scope id="child2"></x-child-scope>
<x-overrides id="overrides1a"></x-overrides>
<x-overrides id="overrides1b"></x-overrides>
<x-overrides2 id="overrides2"></x-overrides2>
<x-overrides3 id="overrides3"></x-overrides3>
<div id="overridesConcrete">override concrete</div>
<button id="button" is="x-button"></button>
<div id="default1">default</div>
<div id="default2">var default</div>
<div id="default3">tricky property rgb(...) default</div>
<x-has-def id="defaultElement1"></x-has-def>
<x-has-def id="defaultElement2"></x-has-def>
<div id="applyDefault1">default</div>
<div id="applyDefault2">var default</div>
<div id="calc">Calc</div>
<div id="shadow">Shadow</div>
<div id="invalid">invalid</div>
<x-host-property id="hostProp"></x-host-property>
<x-has-if id="iffy"></x-has-if>
<div id="after"></div>
<x-dynamic id="dynamic"></x-dynamic>
<div id="wsTerm">new line var</div>
<div id="endTerm">end var</div>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-scope'
});
});
</script>
</dom-module>
<script>
suite('scoped-styling-var', function() {
function assertComputed(element, value, pseudo) {
var computed = getComputedStyle(element, pseudo);
assert.equal(computed['border-top-width'], value, 'computed style incorrect');
}
function assertStylePropertyValue(properties, name, includeValue) {
assert.property(properties, name);
assert.include(properties[name], includeValue);
}
var styled = document.querySelector('x-scope');
test('simple variables calculated correctly between scopes', function() {
assertStylePropertyValue(styled._styleProperties, '--scope-var', '1px');
//
var child = styled.$.child;
assertStylePropertyValue(child._styleProperties, '--scope-var', '1px');
assertStylePropertyValue(child._styleProperties, '--child-scope-var', '2px');
assertStylePropertyValue(child._styleProperties, '--grand-child-scope-var', '3px');
//
var gc1 = child.$.gc1;
assertStylePropertyValue(gc1._styleProperties, '--scope-var', '1px');
assertStylePropertyValue(gc1._styleProperties, '--child-scope-var', '2px');
assertStylePropertyValue(gc1._styleProperties, '--grand-child-scope-var', '3px');
//
var gc2 = child.$.gc2;
assertStylePropertyValue(gc2._styleProperties, '--scope-var', '1px');
assertStylePropertyValue(gc2._styleProperties, '--child-scope-var', '2px');
assertStylePropertyValue(gc2._styleProperties, '--grand-child-scope-var', '4px');
//
var gc3 = child.$.gc3;
assertStylePropertyValue(gc3._styleProperties, '--scope-var', '1px');
assertStylePropertyValue(gc3._styleProperties, '--child-scope-var', '2px');
assertStylePropertyValue(gc3._styleProperties, '--grand-child-scope-var', '3px');
});
test('invalid variables not parsed', function() {
assert.notProperty(styled._styleProperties, 'x--invalid');
assertComputed(styled.$.invalid, '0px');
});
test('simple variables applied correctly between scopes', function() {
assertComputed(styled.$.me, '1px');
assertComputed(styled.$.child.$.me, '2px');
assertComputed(styled.$.child.$.gc1.$.me, '3px');
assertComputed(styled.$.child.$.gc1.$.scope, '1px');
assertComputed(styled.$.child.$.gc1.$.child, '2px');
assertComputed(styled.$.child.$.gc2.$.me, '4px');
assertComputed(styled.$.child.$.gc2.$.scope, '1px');
assertComputed(styled.$.child.$.gc2.$.child, '2px');
});
test('variable can be set to another variable', function() {
var gc4 = styled.$.child.$.gc4;
assertStylePropertyValue(gc4._styleProperties, '--grand-child-scope-var', '5px');
assertComputed(gc4.$.me, '5px');
});
test('variable default values can be assigned to other variables', function() {
assertStylePropertyValue(styled._styleProperties, '--default1', '6px');
assertStylePropertyValue(styled._styleProperties, '--default2', '7px');
assertComputed(styled.$.default1, '6px');
assertComputed(styled.$.default2, '7px');
assertComputed(styled.$.applyDefault1, '6px');
assertComputed(styled.$.applyDefault2, '7px');
});
test('variable literal defaults can contain one (...)', function() {
var b = getComputedStyle(styled.$.default3).backgroundColor;
assert.match(b, /rgb\(128/, 'literal fallback containin (...) not set');
});
test('variable values can be used with calc', function() {
assertComputed(styled.$.calc, '15px');
});
test('variable values can be used with box-shadow', function() {
var b = getComputedStyle(styled.$.shadow).boxShadow;
assert.match(b, /rgb\(128/, 'box shadow not set correctly');
});
test('host properties can be overridden with outer scope styles', function() {
assertComputed(styled.$.hostProp, '10px');
});
test('updateStyles changes property values and using style cache', function() {
styled.$.child.classList.add('special');
var l = document.querySelectorAll('style').length;
styled.updateStyles();
if (styled.shadyRoot) {
assert.equal(document.querySelectorAll('style').length, l+4);
}
assertComputed(styled.$.child.$.me, '12px');
styled.$.child.classList.remove('special');
styled.updateStyles();
if (styled.shadyRoot) {
assert.equal(document.querySelectorAll('style').length, l);
}
assertComputed(styled.$.child.$.me, '2px');
});
test('updateStyles changes own properties based on customStyle changes', function() {
styled.$.child.customStyle['--child-scope-var'] = '30px solid orange';
styled.$.child.updateStyles();
assertComputed(styled.$.child.$.me, '30px');
styled.$.child.customStyle = {};
styled.$.child.updateStyles();
assertComputed(styled.$.child.$.me, '2px');
});
test('updateStyles with properties argument changes styles', function() {
styled.$.child.updateStyles({'--child-scope-var': '26px solid seagreen'});
assertComputed(styled.$.child.$.me, '26px');
styled.$.child.customStyle = {};
styled.$.child.updateStyles();
assertComputed(styled.$.child.$.me, '2px');
});
test('styles update based on root customStyle changes', function() {
assertComputed(styled.$.dynamic, '0px');
Polymer.StyleDefaults.customStyle['--dynamic'] = '4px solid navy';
Polymer.updateStyles();
assertComputed(styled.$.dynamic, '4px');
Polymer.updateStyles({'--dynamic': '6px solid gray'});
assertComputed(styled.$.dynamic, '6px');
});
test('null customStyles unapply', function() {
styled.$.dynamic.updateStyles({'--dynamic': '8px solid black'});
assertComputed(styled.$.dynamic, '8px');
styled.$.dynamic.updateStyles({'--dynamic': null});
assertComputed(styled.$.dynamic, '6px');
styled.$.dynamic.updateStyles({'--dynamic': '8px solid black'});
assertComputed(styled.$.dynamic, '8px');
styled.$.dynamic.updateStyles({'--dynamic': null});
assertComputed(styled.$.dynamic, '6px');
});
test('style properties with dom-if', function() {
var e = styled.$.iffy;
var c = Polymer.dom(e.root).querySelector('.iffy');
assert.ok(c, 'dom-if did not stamp');
assertComputed(c, '1px');
});
test('variable precedence and overrides', function() {
// renamed property applied
var o1a = styled.$.overrides1a;
assertStylePropertyValue(o1a._styleProperties, '--rename', '8px');
assertStylePropertyValue(o1a._styleProperties, '--grand-child-scope-var', '8px');
assertComputed(o1a.$.gc1.$.me, '8px');
// :host property overridden by outer scope
var o1b = styled.$.overrides1b;
assertStylePropertyValue(o1b._styleProperties, '--rename', '8px');
assertStylePropertyValue(o1b._styleProperties, '--grand-child-scope-var', '9px');
assertComputed(o1b.$.gc1.$.me, '9px');
// own scope property overrides outer scope
var o2 = styled.$.overrides2;
assertStylePropertyValue(o2._styleProperties, '--rename', '8px');
assertStylePropertyValue(o2._styleProperties, '--grand-child-scope-var', '8px');
assertComputed(o2.$.gc1.$.me, '8px');
// late bound property does *not* resolve using inherited value
var o3 = styled.$.overrides3;
assert.equal(o3._styleProperties['--late'], '', 'property should not be late bound');
assertStylePropertyValue(o3._styleProperties, '--fillin', '16px');
assertComputed(o3.$.late, '0px');
});
test('type extension elements consume properties in :host rules', function() {
assertComputed(styled.$.button, '16px');
});
test('pseudo-elements can consume custom properties', function() {
assertComputed(styled.$.after, '17px', '::after');
});
test('elements using only variable defaults are styled properly', function() {
assertComputed(styled.$.defaultElement1, '22px');
assertComputed(styled.$.defaultElement2, '23px');
});
test('vars with trailing new line or } apply', function() {
assertComputed(styled.$.wsTerm, '18px');
assertComputed(styled.$.endTerm, '19px');
});
// TODO(sorvell): fix for #1761 was reverted; include test once this issue is addressed
// test('var values can be overridden by subsequent concrete properties', function() {
// assertComputed(styled.$.overridesConcrete, '4px');
// });
});
</script>
</body>
</html>