Skip to content

Commit f9be6d4

Browse files
authored
fix: copy styles from adoptedStyleSheets for chart exporting (#9143)
1 parent 0132e40 commit f9be6d4

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

packages/charts/src/vaadin-chart-mixin.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,10 +1207,18 @@ export const ChartMixin = (superClass) =>
12071207
if (!self.tempBodyStyle) {
12081208
let effectiveCss = '';
12091209

1210+
// PolymerElement uses `<style>` tags for adding styles
12101211
[...self.shadowRoot.querySelectorAll('style')].forEach((style) => {
12111212
effectiveCss += style.textContent;
12121213
});
12131214

1215+
// LitElement uses `adoptedStyleSheets` for adding styles
1216+
if (self.shadowRoot.adoptedStyleSheets) {
1217+
self.shadowRoot.adoptedStyleSheets.forEach((sheet) => {
1218+
effectiveCss += [...sheet.cssRules].map((rule) => rule.cssText).join('\n');
1219+
});
1220+
}
1221+
12141222
// Strip off host selectors that target individual instances
12151223
effectiveCss = effectiveCss.replace(/:host\(.+?\)/gu, (match) => {
12161224
const selector = match.substr(6, match.length - 7);

packages/charts/test/exporting.test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ describe('vaadin-chart exporting', () => {
3434

3535
it('should temporarily copy shadow styles to the body before export', async () => {
3636
let styleCopiedToBody = false;
37+
let styleContent;
3738

3839
// Track style movement into the document body
3940
const observer = new MutationObserver((mutations) => {
40-
styleCopiedToBody ||= mutations.some(
41-
(mutation) =>
42-
Array.from(mutation.addedNodes)
43-
.map((node) => node.tagName.toLowerCase())
44-
.indexOf('style') >= 0,
45-
);
41+
mutations.forEach((mutation) => {
42+
const styleTag = [...mutation.addedNodes].find((node) => node instanceof HTMLStyleElement);
43+
if (styleTag) {
44+
styleCopiedToBody = true;
45+
styleContent = styleTag.textContent;
46+
}
47+
});
4648
});
4749

4850
observer.observe(document.body, { childList: true });
@@ -57,6 +59,7 @@ describe('vaadin-chart exporting', () => {
5759
expect(fireEventSpy.firstCall.args[1]).to.be.equal('beforeExport');
5860
await nextRender(chart);
5961
expect(styleCopiedToBody).to.be.true;
62+
expect(styleContent).to.include('.highcharts-color-0');
6063
});
6164

6265
it('should remove shadow styles from body after export', async () => {

0 commit comments

Comments
 (0)