You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refactor style element cloning for shadow DOM integration
- Introduced helper functions to clone style and link elements, improving code readability and maintainability.
- Added functionality to extract CSS rules from style elements using the CSSOM when textContent is unavailable.
- Enhanced the cloning process to ensure accurate representation of styles in the shadow DOM.
* Extracts CSS rules from a style element by accessing its associated stylesheet.
104
+
*
105
+
* This function is necessary because sometimes `element.textContent` is empty or unreliable
106
+
* for dynamically created style elements, but the actual CSS rules are accessible via the
107
+
* CSSOM (CSS Object Model) through `document.styleSheets`.
108
+
*
109
+
* @param el - The HTML element to extract CSS from (should be a style element)
110
+
* @returns The concatenated CSS text from all rules in the stylesheet, or empty string if the element is not a style element or if the stylesheet is not found.
111
+
*/
112
+
functiongetStyleElementCSS(el: HTMLElement){
113
+
if(!(elinstanceofHTMLStyleElement)){
114
+
return"";
115
+
}
116
+
117
+
// Find the CSSStyleSheet object associated with this style element
118
+
constsheet=Array.from(document.styleSheets).find(
119
+
(s)=>s.ownerNode===el
120
+
);
121
+
122
+
if(!sheet)return"";
123
+
124
+
try{
125
+
// Extract and concatenate all CSS rules from the stylesheet
126
+
returnArray.from(sheet.cssRules)
127
+
.map((rule)=>rule.cssText)
128
+
.join("\n");
129
+
}catch(e){
130
+
// CORS restrictions prevent reading cross-origin stylesheets
131
+
console.warn("Unable to read CSS rules (maybe cross-origin):",e);
0 commit comments