From 1c297ab253c81c8e31d8895b5d428487f8114654 Mon Sep 17 00:00:00 2001 From: David Winegar Date: Tue, 21 Jan 2020 14:37:51 -0800 Subject: [PATCH] Make disableCSSInjection a global again --- samples/advanced/content-security-policy.js | 4 ++-- src/core/core.controller.js | 10 +++++----- src/index.js | 4 +++- .../platform.js => platform/platform.base.js} | 4 +--- src/{platforms => platform}/platform.basic.js | 6 +++--- src/{platforms => platform}/platform.dom.css | 0 src/{platforms => platform}/platform.dom.js | 12 ++++++------ src/platform/platform.js | 3 +++ src/{platforms => platform}/platforms.js | 6 +++--- 9 files changed, 26 insertions(+), 23 deletions(-) rename src/{platforms/platform.js => platform/platform.base.js} (92%) rename src/{platforms => platform}/platform.basic.js (82%) rename src/{platforms => platform}/platform.dom.css (100%) rename src/{platforms => platform}/platform.dom.js (98%) create mode 100644 src/platform/platform.js rename src/{platforms => platform}/platforms.js (57%) diff --git a/samples/advanced/content-security-policy.js b/samples/advanced/content-security-policy.js index 4c4daec2c3f..c77d43193b8 100644 --- a/samples/advanced/content-security-policy.js +++ b/samples/advanced/content-security-policy.js @@ -1,6 +1,8 @@ var utils = Samples.utils; utils.srand(110); +// CSP: disable automatic style injection +Chart.platform.disableCSSInjection = true; function generateData() { var DATA_COUNT = 16; @@ -46,8 +48,6 @@ window.addEventListener('load', function() { } } }, - // CSP: disable automatic style injection - disableCSSInjection: true } }); }); diff --git a/src/core/core.controller.js b/src/core/core.controller.js index c8889513e25..2da5705ebf4 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -6,7 +6,7 @@ import defaults from './core.defaults'; import helpers from '../helpers/index'; import Interaction from './core.interaction'; import layouts from './core.layouts'; -import {BasicPlatform, DomPlatform} from '../platforms/platforms'; +import {BasicPlatform, DomPlatform} from '../platform/platforms'; import plugins from './core.plugins'; import scaleService from '../core/core.scaleService'; @@ -253,13 +253,13 @@ class Chart { const me = this; if (config.platform) { - me.platform = new config.platform(config); + me.platform = new config.platform(); } else if (!isDomSupported()) { - me.platform = new BasicPlatform(config); + me.platform = new BasicPlatform(); } else if (window.OffscreenCanvas && canvas instanceof window.OffscreenCanvas) { - me.platform = new BasicPlatform(config); + me.platform = new BasicPlatform(); } else { - me.platform = new DomPlatform(config); + me.platform = new DomPlatform(); } } diff --git a/src/index.js b/src/index.js index d7adf4c3aee..b32770a0972 100644 --- a/src/index.js +++ b/src/index.js @@ -15,7 +15,8 @@ import Element from './core/core.element'; import elements from './elements'; import Interaction from './core/core.interaction'; import layouts from './core/core.layouts'; -import platforms from './platforms/platforms'; +import platforms from './platform/platforms'; +import platform from './platform/platform'; import pluginsCore from './core/core.plugins'; import Scale from './core/core.scale'; import scaleService from './core/core.scaleService'; @@ -34,6 +35,7 @@ Chart.elements = elements; Chart.Interaction = Interaction; Chart.layouts = layouts; Chart.platforms = platforms; +Chart.platform = platform; Chart.plugins = pluginsCore; Chart.Scale = Scale; Chart.scaleService = scaleService; diff --git a/src/platforms/platform.js b/src/platform/platform.base.js similarity index 92% rename from src/platforms/platform.js rename to src/platform/platform.base.js index 4bca682e4d4..5ef81f74498 100644 --- a/src/platforms/platform.js +++ b/src/platform/platform.base.js @@ -3,11 +3,9 @@ /** * Abstract class that allows abstracting platform dependencies away from the chart. */ -export default class Platform { +export default class BasePlatform { /** * @constructor - * The Platform constructor has no required parameters, but chart options are passed in as - * the first and only parameter to all Platform constructors. */ constructor() {} diff --git a/src/platforms/platform.basic.js b/src/platform/platform.basic.js similarity index 82% rename from src/platforms/platform.basic.js rename to src/platform/platform.basic.js index 6957b76883d..2f2d52c3bc3 100644 --- a/src/platforms/platform.basic.js +++ b/src/platform/platform.basic.js @@ -5,14 +5,14 @@ 'use strict'; -import Platform from './platform'; +import BasePlatform from './platform.base'; /** * Platform class for charts without access to the DOM or to many element properties * This platform is used by default for any chart passed an OffscreenCanvas. - * @extends Platform + * @extends BasePlatform */ -export default class BasicPlatform extends Platform { +export default class BasicPlatform extends BasePlatform { acquireContext(item) { // To prevent canvas fingerprinting, some add-ons undefine the getContext // method, for example: https://github.com/kkapsner/CanvasBlocker diff --git a/src/platforms/platform.dom.css b/src/platform/platform.dom.css similarity index 100% rename from src/platforms/platform.dom.css rename to src/platform/platform.dom.css diff --git a/src/platforms/platform.dom.js b/src/platform/platform.dom.js similarity index 98% rename from src/platforms/platform.dom.js rename to src/platform/platform.dom.js index d049af9b6ec..4b1cd2b7d59 100644 --- a/src/platforms/platform.dom.js +++ b/src/platform/platform.dom.js @@ -6,7 +6,8 @@ import helpers from '../helpers'; import stylesheet from './platform.dom.css'; -import Platform from './platform'; +import BasePlatform from './platform.base'; +import platform from './platform'; var EXPANDO_KEY = '$chartjs'; var CSS_PREFIX = 'chartjs-'; @@ -315,14 +316,13 @@ function injectCSS(rootNode, css) { /** * Platform class for charts that can access the DOM and global window/document properties - * @extends Platform + * @extends BasePlatform */ -export default class DomPlatform extends Platform { +export default class DomPlatform extends BasePlatform { /** * @constructor - * @param {object} options - The chart options */ - constructor(options) { + constructor() { super(); /** @@ -332,7 +332,7 @@ export default class DomPlatform extends Platform { * to be manually imported to make this library compatible with any CSP. * See https://github.com/chartjs/Chart.js/issues/5208 */ - this.disableCSSInjection = options.disableCSSInjection; + this.disableCSSInjection = platform.disableCSSInjection; } /** diff --git a/src/platform/platform.js b/src/platform/platform.js new file mode 100644 index 00000000000..ec81caef8b5 --- /dev/null +++ b/src/platform/platform.js @@ -0,0 +1,3 @@ +'use strict'; + +export default {disableCSSInjection: false}; diff --git a/src/platforms/platforms.js b/src/platform/platforms.js similarity index 57% rename from src/platforms/platforms.js rename to src/platform/platforms.js index 2484a3056c1..a8701f21354 100644 --- a/src/platforms/platforms.js +++ b/src/platform/platforms.js @@ -1,13 +1,13 @@ 'use strict'; -import Platform from './platform'; +import BasePlatform from './platform.base'; import BasicPlatform from './platform.basic'; import DomPlatform from './platform.dom'; -export {BasicPlatform, DomPlatform, Platform}; +export {BasicPlatform, DomPlatform, BasePlatform}; /** * @namespace Chart.platforms * @see https://chartjs.gitbooks.io/proposals/content/Platform.html */ -export default {BasicPlatform, DomPlatform, Platform}; +export default {BasicPlatform, DomPlatform, BasePlatform};