-
Notifications
You must be signed in to change notification settings - Fork 0
/
break-on-element.js
82 lines (68 loc) · 2.06 KB
/
break-on-element.js
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
export class BreakOnElement extends HTMLElement {
#observer;
static {
this.styleSheet = new CSSStyleSheet();
this.styleSheet.replaceSync(`:host { display: contents }`);
}
get elementToObserve() {
const elementToObserve = this.querySelector(this.hasAttribute("selector") ? this.getAttribute("selector") : ":scope > :only-child");
if (this.hasAttribute("text") && !this.hasAttribute("subtree")) {
let textNode = null;
for (const node of elementToObserve.childNodes) {
if (node instanceof Text) {
textNode = node;
break;
}
}
return textNode;
}
return elementToObserve;
}
connectedCallback() {
if (!this.shadowRoot) {
this.attachShadow({ mode: "open" });
this.shadowRoot.adoptedStyleSheets = [this.constructor.styleSheet];
this.shadowRoot.innerHTML = "<slot></slot>";
}
const elementToObserve = this.elementToObserve;
if (elementToObserve) {
const options = {};
if (this.hasAttribute("subtree")) {
options.subtree = true;
}
if (this.hasAttribute("attributes")) {
options.attributes = true;
options.attributeOldValue = true;
const attributesValue = this.getAttribute("attributes");
const isBooleanAttribute = attributesValue === "" || attributesValue === "attributes";
if (!isBooleanAttribute) {
options.attributeFilter = attributesValue.split(" ");
}
}
if (this.hasAttribute("children")) {
options.childList = true;
}
if (this.hasAttribute("text")) {
options.characterData = true;
options.characterDataOldValue = true;
}
if (options.attributes || options.childList || options.characterData) {
this.#observer = new MutationObserver(mutationRecords => {
debugger;
});
this.#observer.observe(elementToObserve, options);
} else {
console.log("Not observing: insufficient options supplied", this);
}
}
}
disconnectedCallback() {
this.#observer?.disconnect();
}
static define(tagName = "break-on") {
if (!window.customElements.get(tagName)) {
window[this.name] = this;
window.customElements.define(tagName, this);
}
}
}