Skip to content

Commit 3c97938

Browse files
author
Elliott Marquez
authored
Merge pull request #223 from GoogleWebComponents/shadow-dom-support
clone global stylesheet elements into root for shadowdom support
2 parents 510692a + 84116c5 commit 3c97938

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

google-chart.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
-->
5959
<dom-module id="google-chart">
6060
<template>
61+
<div id="styles"></div>
6162
<style include="google-chart-styles"></style>
6263
<google-chart-loader id="loader" type="[[type]]"></google-chart-loader>
6364
<div id="chartdiv"></div>
@@ -310,6 +311,12 @@
310311
_typeChanged: function() {
311312
this.$.loader.create(this.type, this.$.chartdiv)
312313
.then(function(chart) {
314+
315+
// only add link stylesheet elements if there are none already
316+
if (!this.$.styles.children.length) {
317+
this._localizeGlobalStylesheets();
318+
}
319+
313320
var loader = this.$.loader;
314321
Object.keys(this.events.concat(['select', 'ready'])
315322
.reduce(function(set, eventName) {
@@ -469,6 +476,33 @@
469476
.then(function(dataView) {
470477
this._dataView = dataView;
471478
}.bind(this));
479+
},
480+
481+
/**
482+
* Queries global document head for google charts link#load-css-* and clones
483+
* them into the local root's div#styles element for shadow dom support.
484+
*/
485+
_localizeGlobalStylesheets: function() {
486+
// get all gchart stylesheets
487+
var stylesheets = Polymer.dom(document.head)
488+
.querySelectorAll('link[rel="stylesheet"][type="text/css"]');
489+
490+
var stylesheetsArray = Array.from(stylesheets);
491+
492+
for (var i = 0; i < stylesheetsArray.length; i++) {
493+
var sheetLinkEl = stylesheetsArray[i];
494+
var isGchartStylesheet = sheetLinkEl.id.indexOf('load-css-') == 0;
495+
496+
if (isGchartStylesheet) {
497+
// clone necessary stylesheet attributes
498+
var clonedLinkEl = document.createElement('link');
499+
clonedLinkEl.setAttribute('rel', 'stylesheet');
500+
clonedLinkEl.setAttribute('type', 'text/css');
501+
clonedLinkEl.setAttribute('href', sheetLinkEl.getAttribute('href'));
502+
503+
Polymer.dom(this.$.styles).appendChild(clonedLinkEl);
504+
}
505+
}
472506
}
473507
});
474508
})();

0 commit comments

Comments
 (0)