-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathshadow.js
More file actions
166 lines (144 loc) · 3.96 KB
/
Copy pathshadow.js
File metadata and controls
166 lines (144 loc) · 3.96 KB
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
import StyleObserver from "../index.js";
import gentleRegisterProperty from "./util/gentle-register-property.js";
import tests from "./tests.js";
import bugs from "../src/util/bugs/index.js";
const inlineStyleTest = {
name: "Don't use inline styles",
skip: !document.adoptedStyleSheets || bugs.ADOPTED_STYLE_SHEET,
async run ({property, meta, initial, value}) {
// Make the property unique
property += "-" + this.data.hostId;
let host = document.getElementById(this.data.hostId);
host.style.setProperty(property, initial);
return new Promise((resolve, reject) => {
let observer = new StyleObserver(records => {
resolve("Observer fired");
});
observer.observe(host, property);
requestAnimationFrame(() => {
host.style.setProperty(property, value);
});
// Timeout after 500ms
setTimeout(() => reject(), 500);
})
.catch(_ => "Observer did not fire")
.then(result => {
let inlineValue = host.style.getPropertyValue("--style-observer-transition");
let adoptedValue = getComputedStyle(host).getPropertyValue("--style-observer-transition");
return { result, inlineValue, adoptedValue };
});
},
map (value) {
let { result, inlineValue, adoptedValue } = value;
inlineValue = inlineValue.length === 0 || inlineValue === "Empty";
adoptedValue = adoptedValue.length > 0;
return { result, inlineValue, adoptedValue };
},
arg: {
property: "--test-inline-style",
initial: "foo",
value: "bar",
},
expect: {
result: "Observer fired",
inlineValue: "Empty",
adoptedValue: "Not empty",
},
};
export default {
name: "Shadow DOM",
beforeEach () {
let host = document.getElementById(this.data.hostId);
let id = this.arg.property + "-" + CSS.escape(this.arg.value);
let target = Object.assign(document.createElement("div"), { id });
host.shadowRoot.append(target);
},
run ({property, meta, initial, value}) {
let host = document.getElementById(this.data.hostId);
let id = property + "-" + CSS.escape(value);
let target = host.shadowRoot.getElementById(id);
if (property.startsWith("--")) {
// Make custom properties unique across all tests
property += "-" + this.data.hostId;
}
if (meta) {
gentleRegisterProperty(property, meta);
}
if (initial !== undefined) {
target.style.setProperty(property, initial);
}
return new Promise((resolve, reject) => {
let observer = new StyleObserver(records => {
resolve(records);
});
observer.observe(target, property);
target.style.setProperty(property, value);
// Timeout after 500ms
setTimeout(() => reject(), 500);
})
.catch(_ => {
return [{ value: "Timed out" }];
})
.then(records => records[0].value);
},
afterEach () {
let host = document.getElementById(this.data.hostId);
let id = this.arg.property + "-" + CSS.escape(this.arg.value);
let target = host.shadowRoot.getElementById(id);
target.remove();
},
tests: [
{
name: "Imperative",
beforeAll () {
let host = Object.assign(document.createElement("div"), { id: "imperative-host" });
host.attachShadow({ mode: "open" });
host.shadowRoot.innerHTML = "";
document.body.append(host);
},
afterAll () {
document.getElementById("imperative-host").remove();
},
data: {
hostId: "imperative-host",
},
tests: [
...tests,
inlineStyleTest,
],
},
{
name: "Declarative",
skip: document.getElementById("declarative-host").shadowRoot === null,
data: {
hostId: "declarative-host",
},
tests: [
...tests,
inlineStyleTest,
],
},
{
name: "Registered custom element",
async beforeAll () {
class RegisteredComponent extends HTMLElement {
constructor () {
super();
this.attachShadow({ mode: "open" });
}
connectedCallback () {
this.setAttribute("id", "registered-host");
}
}
customElements.define("registered-component", RegisteredComponent);
},
data: {
hostId: "registered-host",
},
tests: [
...tests,
inlineStyleTest,
],
},
],
};