@@ -294,17 +294,22 @@ function removeResizeListener(node) {
294
294
}
295
295
}
296
296
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 ) {
298
303
// 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 ;
302
307
css = '/* Chart.js */\n' + css ;
308
+ var style = document . createElement ( 'style' ) ;
303
309
style . setAttribute ( 'type' , 'text/css' ) ;
304
- document . getElementsByTagName ( 'head' ) [ 0 ] . appendChild ( style ) ;
310
+ style . appendChild ( document . createTextNode ( css ) ) ;
311
+ rootNode . appendChild ( style ) ;
305
312
}
306
-
307
- style . appendChild ( document . createTextNode ( css ) ) ;
308
313
}
309
314
310
315
module . exports = {
@@ -325,18 +330,18 @@ module.exports = {
325
330
_enabled : typeof window !== 'undefined' && typeof document !== 'undefined' ,
326
331
327
332
/**
333
+ * Initializes resources that depend on platform options.
334
+ * @param {HTMLCanvasElement } canvas - The Canvas element.
328
335
* @private
329
336
*/
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 ) {
338
338
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 ) ;
340
345
}
341
346
} ,
342
347
@@ -358,10 +363,6 @@ module.exports = {
358
363
// https://github.com/chartjs/Chart.js/issues/2807
359
364
var context = item && item . getContext && item . getContext ( '2d' ) ;
360
365
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
-
365
366
// `instanceof HTMLCanvasElement/CanvasRenderingContext2D` fails when the item is
366
367
// inside an iframe or when running in a protected environment. We could guess the
367
368
// types from their toString() value but let's keep things flexible and assume it's
@@ -370,6 +371,9 @@ module.exports = {
370
371
// https://github.com/chartjs/Chart.js/issues/4102
371
372
// https://github.com/chartjs/Chart.js/issues/4152
372
373
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 ) ;
373
377
initCanvas ( item , config ) ;
374
378
return context ;
375
379
}
0 commit comments