Skip to content

Commit bfe3421

Browse files
authored
Use ResizeObserver and MutationObserver to detect detach/attach/resize (#7104)
* Use Resize/MutationObserver to detect detach/attach/resize * Cleanup * Review update * Restore infinite resize detection (#6011)
1 parent 7397a41 commit bfe3421

17 files changed

+187
-492
lines changed

docs/getting-started/integration.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,3 @@ require(['moment'], function() {
6464
});
6565
});
6666
```
67-
68-
## Content Security Policy
69-
70-
By default, Chart.js injects CSS directly into the DOM. For webpages secured using [Content Security Policy (CSP)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP), this requires to allow `style-src 'unsafe-inline'`. For stricter CSP environments, where only `style-src 'self'` is allowed, the following CSS file needs to be manually added to your webpage:
71-
72-
```html
73-
<link rel="stylesheet" type="text/css" href="path/to/chartjs/dist/Chart.min.css">
74-
```
75-
76-
And the style injection must be turned off **before creating the first chart**:
77-
78-
```javascript
79-
// Disable automatic style injection
80-
Chart.platform.disableCSSInjection = true;
81-
```

docs/getting-started/v3-migration.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Chart.js 3.0 introduces a number of breaking changes. Chart.js 2.0 was released
77
* Completely rewritten animation system
88
* Rewritten filler plugin with numerous bug fixes
99
* API Documentation generated and verified by TypeScript
10+
* No more CSS injection
1011
* Tons of bug fixes
1112

1213
## End user migration
@@ -88,6 +89,7 @@ Animation system was completely rewritten in Chart.js v3. Each property can now
8889
* `Chart.chart.chart`
8990
* `Chart.Controller`
9091
* `Chart.prototype.generateLegend`
92+
* `Chart.platform`. It only contained `disableCSSInjection`. CSS is never injected in v3.
9193
* `Chart.types`
9294
* `Chart.Tooltip` is now provided by the tooltip plugin. The positioners can be accessed from `tooltipPlugin.positioners`
9395
* `DatasetController.addElementAndReset`
@@ -253,6 +255,6 @@ Animation system was completely rewritten in Chart.js v3. Each property can now
253255

254256
#### Platform
255257

256-
* `Chart.platform` is no longer the platform object used by charts. It contains only a single configuration option, `disableCSSInjection`. Every chart instance now has a separate platform instance.
258+
* `Chart.platform` is no longer the platform object used by charts. Every chart instance now has a separate platform instance.
257259
* `Chart.platforms` is an object that contains two usable platform classes, `BasicPlatform` and `DomPlatform`. It also contains `BasePlatform`, a class that all platforms must extend from.
258260
* If the canvas passed in is an instance of `OffscreenCanvas`, the `BasicPlatform` is automatically used.

package-lock.json

Lines changed: 6 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"@babel/plugin-transform-object-assign": "^7.8.3",
3535
"@babel/preset-env": "^7.8.4",
3636
"babel-preset-es2015-rollup": "^3.0.0",
37-
"clean-css": "^4.2.3",
3837
"coveralls": "^3.0.9",
3938
"eslint": "^6.8.0",
4039
"eslint-config-chartjs": "^0.2.0",
@@ -66,6 +65,7 @@
6665
"merge-stream": "^1.0.1",
6766
"moment": "^2.10.2",
6867
"pixelmatch": "^5.0.0",
68+
"resize-observer-polyfill": "^1.5.1",
6969
"rollup": "^1.31.0",
7070
"rollup-plugin-babel": "^4.3.3",
7171
"rollup-plugin-cleanup": "^3.1.1",

rollup.config.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const babel = require('rollup-plugin-babel');
77
const cleanup = require('rollup-plugin-cleanup');
88
const terser = require('rollup-plugin-terser').terser;
99
const optional = require('./rollup.plugins').optional;
10-
const stylesheet = require('./rollup.plugins').stylesheet;
1110
const pkg = require('./package.json');
1211

1312
const input = 'src/index.js';
@@ -29,13 +28,12 @@ module.exports = [
2928
resolve(),
3029
commonjs(),
3130
babel(),
32-
stylesheet({
33-
extract: true
34-
}),
3531
optional({
3632
include: ['moment']
3733
}),
38-
cleanup(),
34+
cleanup({
35+
sourcemap: true
36+
})
3937
],
4038
output: {
4139
name: 'Chart',
@@ -60,10 +58,6 @@ module.exports = [
6058
optional({
6159
include: ['moment']
6260
}),
63-
stylesheet({
64-
extract: true,
65-
minify: true
66-
}),
6761
terser({
6862
output: {
6963
preamble: banner
@@ -93,10 +87,9 @@ module.exports = [
9387
resolve(),
9488
commonjs(),
9589
babel(),
96-
stylesheet({
97-
extract: true
98-
}),
99-
cleanup(),
90+
cleanup({
91+
sourcemap: true
92+
})
10093
],
10194
output: {
10295
name: 'Chart',
@@ -118,10 +111,6 @@ module.exports = [
118111
resolve(),
119112
commonjs(),
120113
babel(),
121-
stylesheet({
122-
extract: true,
123-
minify: true
124-
}),
125114
terser({
126115
output: {
127116
preamble: banner

rollup.plugins.js

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
/* eslint-env es6 */
2-
const cleancss = require('clean-css');
3-
const path = require('path');
4-
1+
/* eslint-disable import/no-commonjs */
52
const UMD_WRAPPER_RE = /(\(function \(global, factory\) \{)((?:\s.*?)*)(\}\(this,)/;
63
const CJS_FACTORY_RE = /(module.exports = )(factory\(.*?\))( :)/;
74
const AMD_FACTORY_RE = /(define\()(.*?, factory)(\) :)/;
@@ -24,7 +21,7 @@ function optional(config = {}) {
2421
let factory = (CJS_FACTORY_RE.exec(content) || [])[2];
2522
let updated = false;
2623

27-
for (let lib of chunk.imports) {
24+
for (const lib of chunk.imports) {
2825
if (!include || include.indexOf(lib) !== -1) {
2926
const regex = new RegExp(`require\\('${lib}'\\)`);
3027
if (!regex.test(factory)) {
@@ -58,53 +55,6 @@ function optional(config = {}) {
5855
};
5956
}
6057

61-
// https://github.com/chartjs/Chart.js/issues/5208
62-
function stylesheet(config = {}) {
63-
const minifier = new cleancss();
64-
const styles = [];
65-
66-
return {
67-
name: 'stylesheet',
68-
transform(code, id) {
69-
// Note that 'id' can be mapped to a CJS proxy import, in which case
70-
// 'id' will start with 'commonjs-proxy', so let's first check if we
71-
// are importing an existing css file (i.e. startsWith()).
72-
if (!id.startsWith(path.resolve('.')) || !id.endsWith('.css')) {
73-
return;
74-
}
75-
76-
if (config.minify) {
77-
code = minifier.minify(code).styles;
78-
}
79-
80-
// keep track of all imported stylesheets (already minified)
81-
styles.push(code);
82-
83-
return {
84-
code: 'export default ' + JSON.stringify(code)
85-
};
86-
},
87-
generateBundle(opts, bundle) {
88-
if (!config.extract) {
89-
return;
90-
}
91-
92-
const entry = Object.keys(bundle).find(v => bundle[v].isEntry);
93-
const name = (entry || '').replace(/\.js$/i, '.css');
94-
if (!name) {
95-
this.error('failed to guess the output file name');
96-
}
97-
98-
this.emitFile({
99-
type: 'asset',
100-
source: styles.filter(v => !!v).join(''),
101-
fileName: name
102-
});
103-
}
104-
};
105-
}
106-
10758
module.exports = {
108-
optional,
109-
stylesheet
59+
optional
11060
};

samples/advanced/content-security-policy.css

Lines changed: 0 additions & 20 deletions
This file was deleted.

samples/advanced/content-security-policy.html

Lines changed: 0 additions & 27 deletions
This file was deleted.

samples/advanced/content-security-policy.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

samples/samples.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,6 @@
244244
items: [{
245245
title: 'Progress bar',
246246
path: 'advanced/progress-bar.html'
247-
}, {
248-
title: 'Content Security Policy',
249-
path: 'advanced/content-security-policy.html'
250247
}, {
251248
title: 'Polar Area Radial Gradient',
252249
path: 'advanced/radial-gradient.html'

0 commit comments

Comments
 (0)