-
Notifications
You must be signed in to change notification settings - Fork 54
/
state-pseudo-class.html
129 lines (115 loc) · 3.79 KB
/
state-pseudo-class.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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
#state-and-part::part(inner) {
opacity: 0;
}
#state-and-part::part(inner):state(innerFoo) {
opacity: 0.5;
}
#state-and-part:state(outerFoo)::part(inner) {
opacity: 0.25;
}
:state( \(escaped\ state ) {}
</style>
<body>
<script>
class TestElement extends HTMLElement {
constructor() {
super();
this._internals = this.attachInternals();
}
get i() {
return this._internals;
}
}
customElements.define('test-element', TestElement);
class ContainerElement extends HTMLElement {
constructor() {
super();
this._internals = this.attachInternals();
this._shadow = this.attachShadow({mode:'open'});
this._shadow.innerHTML = `
<style>
:host {
border-style: solid;
}
:host(:state(dotted)) {
border-style: dotted;
}
</style>
<test-element part="inner"></test-element>`;
}
get i() {
return this._internals;
}
get innerElement() {
return this._shadow.querySelector('test-element');
}
}
customElements.define('container-element', ContainerElement);
test(() => {
assert_throws_dom('SyntaxError', () => { document.querySelector(':state'); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state('); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state()'); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state(=)'); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state(name=value)'); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state( foo bar)'); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state(16px)'); });
}, ':state() parsing failures');
test(() => {
assert_equals(document.styleSheets[0].cssRules[1].cssText,
'#state-and-part::part(inner):state(innerFoo) { opacity: 0.5; }');
assert_equals(document.styleSheets[0].cssRules[3].selectorText,
':state(\\(escaped\\ state)');
}, ':state() serialization');
test(() => {
let element = new TestElement();
let states = element.i.states;
assert_false(element.matches(':state(foo)'));
assert_true(element.matches(':not(:state(foo))'));
states.add('foo');
assert_true(element.matches(':state(foo)'));
assert_true(element.matches(':is(:state(foo))'));
element.classList.add('c1', 'c2');
assert_true(element.matches('.c1:state(foo)'));
assert_true(element.matches(':state(foo).c1'));
assert_true(element.matches('.c2:state(foo).c1'));
}, ':state() in simple cases');
test(() => {
let element = new TestElement();
element.tabIndex = 0;
document.body.appendChild(element);
element.focus();
let states = element.i.states;
states.value = 'foo';
assert_true(element.matches(':focus:state(foo)'));
assert_true(element.matches(':state(foo):focus'));
}, ':state() and other pseudo classes');
test(() => {
let outer = new ContainerElement();
outer.id = 'state-and-part';
document.body.appendChild(outer);
let inner = outer.innerElement;
let innerStates = inner.i.states;
innerStates.add('innerFoo');
assert_equals(getComputedStyle(inner).opacity, '0.5',
'::part() followed by :state()');
innerStates.replace('innerFoo', 'innerfoo');
assert_equals(getComputedStyle(inner).opacity, '0',
':state() matching should be case-sensitive');
innerStates.remove('innerfoo');
outer.i.states.add('outerFoo');
assert_equals(getComputedStyle(inner).opacity, '0.25',
':state() followed by ::part()');
}, ':state() and ::part()');
test(() => {
let outer = new ContainerElement();
document.body.appendChild(outer);
assert_equals(getComputedStyle(outer).borderStyle, 'solid');
outer.i.states.toggle('dotted');
assert_equals(getComputedStyle(outer).borderStyle, 'dotted');
}, ':state() and :host()');
</script>
</body>