-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
198 lines (194 loc) · 5.91 KB
/
demo.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
<!doctype html>
<html lang="en-ca">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {
font-family: system-ui;
}
</style>
<script type="module">
import { BreakOnElement } from "./break-on-element.js";
BreakOnElement.define();
customElements.define("modify-parent", class extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: "open" })
this.shadowRoot.innerHTML = `
<button type="button"><slot></slot></button>
`;
this.shadowRoot
.querySelector(":host > button")
.addEventListener("click", () => {
this.parentElement.setAttribute(
this.getAttribute("attribute"),
this.getAttribute("value"),
);
});
}
});
customElements.define("add-sibling", class extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: "open" });
this.shadowRoot.innerHTML = `
<button type="button"><slot>Add Sibling</slot></button>
`;
this.shadowRoot
.querySelector(":host > button")
.addEventListener("click", () => {
const siblingElement = this.ownerDocument.createElement("span");
siblingElement.textContent = "Sibling";
siblingElement.classList.add("sibling");
this.parentElement.appendChild(siblingElement);
});
}
});
customElements.define("remove-sibling", class extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: "open" });
this.shadowRoot.innerHTML = `
<button type="button"><slot>Remove Sibling</slot></button>
`;
this.shadowRoot
.querySelector(":host > button")
.addEventListener("click", () => {
const lastSibling = this.parentElement.querySelector(":scope > .sibling:last-of-type");
lastSibling?.remove();
});
}
});
customElements.define("add-text", class extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: "open" });
this.shadowRoot.innerHTML = `
<button type="button"><slot>Add Text</slot></button>
`;
this.shadowRoot
.querySelector(":host > button")
.addEventListener("click", () => {
let firstTextNode;
for (const node of this.parentElement.childNodes) {
if (node instanceof Text) {
firstTextNode = node;
break;
}
}
firstTextNode.appendData(" Hello, World!");
});
}
});
customElements.define("remove-text", class extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: "open" });
this.shadowRoot.innerHTML = `
<button type="button"><slot>Remove Text</slot></button>
`;
this.shadowRoot
.querySelector(":host > button")
.addEventListener("click", () => {
let firstTextNode;
for (const node of this.parentElement.childNodes) {
if (node instanceof Text) {
firstTextNode = node;
break;
}
}
firstTextNode.deleteData(firstTextNode.length - 14, 14);
});
}
});
</script>
</head>
<body>
<h1>Break On Element Demonstration</h1>
<p>This custom element is a declarative debugger for element modifications.</p>
<p>You will need to open up your browser’s developer tools for it to work initially.</p>
<hr>
<break-on>
<p>This doesn’t provide sufficient options for it to be observed.</p>
</break-on>
<break-on attributes>
<p>
Breaking on attribute modifications:
<modify-parent attribute="id" value="something">Add ID</modify-parent>
<modify-parent attribute="class" value="something">Add Class</modify-parent>
</p>
</break-on>
<break-on attributes="class">
<p>
Breaking on class attribute modifications:
<modify-parent attribute="id" value="something">Add ID</modify-parent>
<modify-parent attribute="class" value="something">Add Class</modify-parent>
</p>
</break-on>
<break-on attributes="class id">
<p>
Breaking on class and ID attribute modifications:
<modify-parent attribute="id" value="something">Add ID</modify-parent>
<modify-parent attribute="class" value="something">Add Class</modify-parent>
<modify-parent attribute="style" value="--custom-prop: hello;">Add Custom Property</modify-parent>
</p>
</break-on>
<break-on attributes subtree>
<p>
Breaking on attribute modifications including in the subtree.
<modify-parent attribute="id" value="something">Add ID</modify-parent>
<modify-parent attribute="class" value="something">Add Class</modify-parent>
<span>
<modify-parent attribute="style" value="--custom-prop: hello;">Add Custom Property to Subtree element</modify-parent>
</span>
</p>
</break-on>
<break-on children>
<p>
Break on children added or removed.
<add-sibling></add-sibling>
<remove-sibling></remove-sibling>
</p>
</break-on>
<break-on children subtree>
<p>
Break on children added or removed in a subtree.
<add-sibling></add-sibling>
<remove-sibling></remove-sibling>
<span>
<add-sibling></add-sibling>
<remove-sibling></remove-sibling>
</span>
</p>
</break-on>
<break-on selector=":scope > div > p" attributes="class">
<div>
<p>Setting the breakpoint on a nested element <modify-parent attribute="class" value="hello">Add Class</modify-parent></p>
</div>
</break-on>
<break-on text>
<div>
Breaks on text changes to the first top-level text node.
<add-text></add-text>
<remove-text></remove-text>
<ul>
<li>
Sub tree text node.
<add-text></add-text>
<remove-text></remove-text>
</li>
</ul>
</div>
</break-on>
<break-on text subtree>
<div>
Breaks on text changes to all text nodes in the subtree.
<add-text></add-text>
<remove-text></remove-text>
<ul>
<li>
Sub tree text node.
<add-text></add-text>
<remove-text></remove-text>
</li>
</ul>
</div>
</break-on>
</body>
</html>