@iyulab/data-components 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,82 +0,0 @@
1
- let isInitialized = false;
2
- const protectedCustomStylesheets = /* @__PURE__ */ new Set();
3
- function registerProtectedStylesheet(stylesheet) {
4
- protectedCustomStylesheets.add(stylesheet);
5
- }
6
- function isProtectedCustomStylesheet(sheet) {
7
- return protectedCustomStylesheets.has(sheet);
8
- }
9
- function isLitStyleSheet(sheet) {
10
- try {
11
- if (!sheet.cssRules || sheet.cssRules.length === 0) return false;
12
- const firstRule = sheet.cssRules[0];
13
- return firstRule.cssText.startsWith(":host");
14
- } catch {
15
- return false;
16
- }
17
- }
18
- function isEmptyOrHMRReset(sheets) {
19
- if (!sheets || sheets.length === 0) return true;
20
- return sheets.every((sheet) => {
21
- try {
22
- return !sheet.cssRules || sheet.cssRules.length === 0;
23
- } catch {
24
- return true;
25
- }
26
- });
27
- }
28
- function initShadowDomProtection() {
29
- if (isInitialized) return;
30
- if (typeof window === "undefined") return;
31
- const originalDescriptor = Object.getOwnPropertyDescriptor(
32
- ShadowRoot.prototype,
33
- "adoptedStyleSheets"
34
- );
35
- if (!originalDescriptor) {
36
- console.warn("[data-components] adoptedStyleSheets descriptor not found");
37
- return;
38
- }
39
- const protectedLitStylesMap = /* @__PURE__ */ new WeakMap();
40
- Object.defineProperty(ShadowRoot.prototype, "adoptedStyleSheets", {
41
- get() {
42
- return originalDescriptor.get?.call(this) ?? [];
43
- },
44
- set(sheets) {
45
- const savedLitStyles = protectedLitStylesMap.get(this) || [];
46
- const newLitStyles = sheets?.filter(isLitStyleSheet) || [];
47
- const newNonLitStyles = sheets?.filter((s) => !isLitStyleSheet(s)) || [];
48
- if (newLitStyles.length > 0) {
49
- protectedLitStylesMap.set(this, [...newLitStyles]);
50
- }
51
- const protectedSheets = Array.from(protectedCustomStylesheets);
52
- if (isEmptyOrHMRReset(sheets) && (savedLitStyles.length > 0 || protectedSheets.length > 0)) {
53
- const preserved = [...savedLitStyles, ...protectedSheets.filter((s) => !savedLitStyles.includes(s))];
54
- originalDescriptor.set?.call(this, preserved);
55
- return;
56
- }
57
- if (savedLitStyles.length > 0 && newLitStyles.length === 0 && newNonLitStyles.length > 0) {
58
- const nonProtectedNewStyles = newNonLitStyles.filter((s) => !isProtectedCustomStylesheet(s));
59
- const mergedSheets = [
60
- ...savedLitStyles,
61
- ...protectedSheets.filter((s) => !savedLitStyles.includes(s)),
62
- ...nonProtectedNewStyles.filter((s) => !protectedSheets.includes(s))
63
- ];
64
- originalDescriptor.set?.call(this, mergedSheets);
65
- return;
66
- }
67
- const finalSheets = sheets ? [...sheets] : [];
68
- protectedSheets.forEach((ps) => {
69
- if (!finalSheets.includes(ps)) {
70
- finalSheets.push(ps);
71
- }
72
- });
73
- originalDescriptor.set?.call(this, finalSheets);
74
- },
75
- configurable: true,
76
- enumerable: true
77
- });
78
- isInitialized = true;
79
- }
80
- initShadowDomProtection();
81
-
82
- export { initShadowDomProtection, registerProtectedStylesheet };