Skip to content

Commit 0b62f28

Browse files
Simon-Tangetimberg
authored andcommitted
Inject styles into Shadow DOM when inside Shadow DOM (#5763) (#6556)
1 parent 4d7fefc commit 0b62f28

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/platforms/platform.dom.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,22 @@ function removeResizeListener(node) {
294294
}
295295
}
296296

297-
function injectCSS(platform, css) {
297+
/**
298+
* Injects CSS styles inline if the styles are not already present.
299+
* @param {HTMLDocument|ShadowRoot} rootNode - the node to contain the <style>.
300+
* @param {string} css - the CSS to be injected.
301+
*/
302+
function injectCSS(rootNode, css) {
298303
// https://stackoverflow.com/q/3922139
299-
var style = platform._style || document.createElement('style');
300-
if (!platform._style) {
301-
platform._style = style;
304+
var expando = rootNode[EXPANDO_KEY] || (rootNode[EXPANDO_KEY] = {});
305+
if (!expando.containsStyles) {
306+
expando.containsStyles = true;
302307
css = '/* Chart.js */\n' + css;
308+
var style = document.createElement('style');
303309
style.setAttribute('type', 'text/css');
304-
document.getElementsByTagName('head')[0].appendChild(style);
310+
style.appendChild(document.createTextNode(css));
311+
rootNode.appendChild(style);
305312
}
306-
307-
style.appendChild(document.createTextNode(css));
308313
}
309314

310315
module.exports = {
@@ -325,18 +330,18 @@ module.exports = {
325330
_enabled: typeof window !== 'undefined' && typeof document !== 'undefined',
326331

327332
/**
333+
* Initializes resources that depend on platform options.
334+
* @param {HTMLCanvasElement} canvas - The Canvas element.
328335
* @private
329336
*/
330-
_ensureLoaded: function() {
331-
if (this._loaded) {
332-
return;
333-
}
334-
335-
this._loaded = true;
336-
337-
// https://github.com/chartjs/Chart.js/issues/5208
337+
_ensureLoaded: function(canvas) {
338338
if (!this.disableCSSInjection) {
339-
injectCSS(this, stylesheet);
339+
// If the canvas is in a shadow DOM, then the styles must also be inserted
340+
// into the same shadow DOM.
341+
// https://github.com/chartjs/Chart.js/issues/5763
342+
var root = canvas.getRootNode();
343+
var targetNode = root.host ? root : document.head;
344+
injectCSS(targetNode, stylesheet);
340345
}
341346
},
342347

@@ -358,10 +363,6 @@ module.exports = {
358363
// https://github.com/chartjs/Chart.js/issues/2807
359364
var context = item && item.getContext && item.getContext('2d');
360365

361-
// Load platform resources on first chart creation, to make possible to change
362-
// platform options after importing the library (e.g. `disableCSSInjection`).
363-
this._ensureLoaded();
364-
365366
// `instanceof HTMLCanvasElement/CanvasRenderingContext2D` fails when the item is
366367
// inside an iframe or when running in a protected environment. We could guess the
367368
// types from their toString() value but let's keep things flexible and assume it's
@@ -370,6 +371,9 @@ module.exports = {
370371
// https://github.com/chartjs/Chart.js/issues/4102
371372
// https://github.com/chartjs/Chart.js/issues/4152
372373
if (context && context.canvas === item) {
374+
// Load platform resources on first chart creation, to make it possible to
375+
// import the library before setting platform options.
376+
this._ensureLoaded(item);
373377
initCanvas(item, config);
374378
return context;
375379
}

0 commit comments

Comments
 (0)