forked from chartjs/Chart.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move CSS in a separate file to be CSP-compliant (chartjs#6048)
In order to be compatible with any CSP, we need to prevent the automatic creation of the DOM 'style' element and offer our CSS as a separate file that can be manually loaded (`Chart.js` or `Chart.min.js`). Users can now opt-out the style injection using `Chart.platform.disableCSSInjection = true` (note that the style sheet is now injected on the first chart creation). To prevent duplicating and maintaining the same CSS code at different places, move all these rules in `platform.dom.css` and write a minimal rollup plugin to inject that style as string in `platform.dom.js`. Additionally, this plugin extract the imported style in `./dist/Chart.js` and `./dist/Chart.min.js`.
- Loading branch information
1 parent
22360ce
commit 7f24019
Showing
14 changed files
with
266 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.content { | ||
max-width: 640px; | ||
margin: auto; | ||
padding: 1rem; | ||
} | ||
|
||
.note { | ||
font-family: sans-serif; | ||
color: #5050a0; | ||
line-height: 1.4; | ||
margin-bottom: 1rem; | ||
padding: 1rem; | ||
} | ||
|
||
code { | ||
background-color: #f5f5ff; | ||
border: 1px solid #d0d0fa; | ||
border-radius: 4px; | ||
padding: 0.05rem 0.25rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!DOCTYPE html> | ||
<html lang="en-US"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=Edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta http-equiv="Content-Security-Policy" content="default-src 'self'"> | ||
<title>Scriptable > Bubble | Chart.js sample</title> | ||
<link rel="stylesheet" type="text/css" href="../../dist/Chart.min.css"> | ||
<link rel="stylesheet" type="text/css" href="./content-security-policy.css"> | ||
<script src="../../dist/Chart.min.js"></script> | ||
<script src="../utils.js"></script> | ||
<script src="content-security-policy.js"></script> | ||
</head> | ||
<body> | ||
<div class="content"> | ||
<div class="note"> | ||
In order to support a strict content security policy (<code>default-src 'self'</code>), | ||
this page manually loads <code>Chart.min.css</code> and turns off the automatic style | ||
injection by setting <code>Chart.platform.disableCSSInjection = true;</code>. | ||
</div> | ||
<div class="wrapper"> | ||
<canvas id="chart-0"></canvas> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
var utils = Samples.utils; | ||
|
||
// CSP: disable automatic style injection | ||
Chart.platform.disableCSSInjection = true; | ||
|
||
utils.srand(110); | ||
|
||
function generateData() { | ||
var DATA_COUNT = 16; | ||
var MIN_XY = -150; | ||
var MAX_XY = 100; | ||
var data = []; | ||
var i; | ||
|
||
for (i = 0; i < DATA_COUNT; ++i) { | ||
data.push({ | ||
x: utils.rand(MIN_XY, MAX_XY), | ||
y: utils.rand(MIN_XY, MAX_XY), | ||
v: utils.rand(0, 1000) | ||
}); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
window.addEventListener('load', function() { | ||
new Chart('chart-0', { | ||
type: 'bubble', | ||
data: { | ||
datasets: [{ | ||
backgroundColor: utils.color(0), | ||
data: generateData() | ||
}, { | ||
backgroundColor: utils.color(1), | ||
data: generateData() | ||
}] | ||
}, | ||
options: { | ||
aspectRatio: 1, | ||
legend: false, | ||
tooltip: false, | ||
elements: { | ||
point: { | ||
radius: function(context) { | ||
var value = context.dataset.data[context.dataIndex]; | ||
var size = context.chart.width; | ||
var base = Math.abs(value.v) / 1000; | ||
return (size / 24) * base; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* DOM element rendering detection | ||
* https://davidwalsh.name/detect-node-insertion | ||
*/ | ||
@keyframes chartjs-render-animation { | ||
from { opacity: 0.99; } | ||
to { opacity: 1; } | ||
} | ||
|
||
.chartjs-render-monitor { | ||
animation: chartjs-render-animation 0.001s; | ||
} | ||
|
||
/* | ||
* DOM element resizing detection | ||
* https://github.com/marcj/css-element-queries | ||
*/ | ||
.chartjs-size-monitor, | ||
.chartjs-size-monitor-expand, | ||
.chartjs-size-monitor-shrink { | ||
position: absolute; | ||
left: 0; | ||
top: 0; | ||
right: 0; | ||
bottom: 0; | ||
overflow: hidden; | ||
pointer-events: none; | ||
visibility: hidden; | ||
z-index: -1; | ||
} | ||
|
||
.chartjs-size-monitor-expand > div { | ||
position: absolute; | ||
width: 1000000px; | ||
height: 1000000px; | ||
left: 0; | ||
top: 0; | ||
} | ||
|
||
.chartjs-size-monitor-shrink > div { | ||
position: absolute; | ||
width: 200%; | ||
height: 200%; | ||
left: 0; | ||
top: 0; | ||
} |
Oops, something went wrong.