Skip to content

Commit 074e0a3

Browse files
committed
Fixed shadow dom styles.
1 parent 9ee0b90 commit 074e0a3

File tree

6 files changed

+80
-23
lines changed

6 files changed

+80
-23
lines changed

build/index.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/main.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,17 @@ setTimeout(async () => {
6161
const j = await a.json();
6262
console.log(j);
6363
}, 5000);
64+
65+
setTimeout(() => {
66+
const sheet1 = new CSSStyleSheet();
67+
sheet1.replaceSync("* { color: red; }");
68+
69+
/*const div = document.createElement("div");
70+
const shadowRoot = div.attachShadow({ mode: "open" });
71+
shadowRoot.adoptedStyleSheets = [sheet1];
72+
shadowRoot.innerHTML = `
73+
<span class="foo">Hello!<br />HelloHelloHelloHelloHelloHelloHelloHelloHello HelloHelloHelloHelloHelloHelloHelloHelloHello HelloHelloHelloHelloHelloHelloHelloHelloHello Hello</span>
74+
`;
75+
document.body.appendChild(div);*/
76+
document.adoptedStyleSheets = [sheet1];
77+
}, 2000);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gleap",
3-
"version": "6.8.14",
3+
"version": "6.8.15",
44
"main": "build/index.js",
55
"scripts": {
66
"start": "webpack serve",

published/6.8.14/index.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

published/latest/index.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ScreenCapture.js

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,32 +207,37 @@ const replaceStyleNodes = (clone, styleSheet, cssTextContent, styleId) => {
207207
cloneTargetNode.remove();
208208
}
209209
}
210-
} catch (exp) {}
210+
} catch (exp) { }
211211
}
212212
};
213213

214+
const getTextContentFromStyleSheet = (styleSheet) => {
215+
var cssRules = null;
216+
try {
217+
if (styleSheet.cssRules) {
218+
cssRules = styleSheet.cssRules;
219+
} else if (styleSheet.rules) {
220+
cssRules = styleSheet.rules;
221+
}
222+
} catch (exp) { }
223+
224+
var cssTextContent = "";
225+
if (cssRules) {
226+
for (var cssRuleItem in cssRules) {
227+
if (cssRules[cssRuleItem].cssText) {
228+
cssTextContent += cssRules[cssRuleItem].cssText;
229+
}
230+
}
231+
}
232+
233+
return cssTextContent;
234+
}
235+
214236
const downloadAllCSSUrlResources = (clone, remote) => {
215237
var promises = [];
216238
for (var i = 0; i < document.styleSheets.length; i++) {
217239
const styleSheet = document.styleSheets[i];
218-
219-
var cssRules = null;
220-
try {
221-
if (styleSheet.cssRules) {
222-
cssRules = styleSheet.cssRules;
223-
} else if (styleSheet.rules) {
224-
cssRules = styleSheet.rules;
225-
}
226-
} catch (exp) {}
227-
228-
var cssTextContent = "";
229-
if (cssRules) {
230-
for (var cssRuleItem in cssRules) {
231-
if (cssRules[cssRuleItem].cssText) {
232-
cssTextContent += cssRules[cssRuleItem].cssText;
233-
}
234-
}
235-
}
240+
const cssTextContent = getTextContentFromStyleSheet(styleSheet);
236241

237242
if (styleSheet && styleSheet.ownerNode) {
238243
if (cssTextContent != "" && !remote) {
@@ -311,8 +316,34 @@ const prepareRemoteData = (clone, remote) => {
311316
});
312317
};
313318

319+
const handleAdoptedStyleSheets = (doc, clone, shadowNodeId) => {
320+
if (typeof doc.adoptedStyleSheets !== "undefined") {
321+
for (let i = 0; i < doc.adoptedStyleSheets.length; i++) {
322+
const styleSheet = doc.adoptedStyleSheets[i];
323+
const cssTextContent = getTextContentFromStyleSheet(styleSheet);
324+
325+
var shadowStyleNode = window.document.createElement("style");
326+
shadowStyleNode.type = "text/css";
327+
if (shadowStyleNode.styleSheet) {
328+
shadowStyleNode.styleSheet.cssText = cssTextContent;
329+
} else {
330+
shadowStyleNode.appendChild(
331+
window.document.createTextNode(cssTextContent)
332+
);
333+
}
334+
335+
if (shadowNodeId) {
336+
shadowStyleNode.setAttribute("bb-shadow-child", shadowNodeId);
337+
}
338+
339+
clone.insertBefore(shadowStyleNode, clone.firstElementChild);
340+
}
341+
}
342+
}
343+
314344
const deepClone = (host) => {
315345
let shadowNodeId = 1;
346+
316347
const cloneNode = (node, parent, shadowRoot) => {
317348
const walkTree = (nextn, nextp, innerShadowRoot) => {
318349
while (nextn) {
@@ -378,11 +409,15 @@ const deepClone = (host) => {
378409
}
379410

380411
parent.appendChild(clone);
412+
381413
if (node.shadowRoot) {
382414
walkTree(node.shadowRoot.firstChild, clone, shadowNodeId);
415+
handleAdoptedStyleSheets(node.shadowRoot, clone, shadowNodeId);
416+
383417
if (typeof clone.setAttribute !== "undefined") {
384418
clone.setAttribute("bb-shadow-parent", shadowNodeId);
385419
}
420+
386421
++shadowNodeId;
387422
}
388423

@@ -391,6 +426,14 @@ const deepClone = (host) => {
391426

392427
const fragment = document.createDocumentFragment();
393428
cloneNode(host, fragment);
429+
430+
// Work on adopted stylesheets.
431+
var clonedHead = fragment.querySelector("head");
432+
if (!clonedHead) {
433+
clonedHead = fragment;
434+
}
435+
handleAdoptedStyleSheets(window.document, clonedHead);
436+
394437
return fragment;
395438
};
396439

0 commit comments

Comments
 (0)