Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow nonce values for style elements #4091

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/apexcharts.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export default class ApexCharts {
this.css = document.createElement('style')
this.css.id = 'apexcharts-css'
this.css.textContent = apexCSS
const nonce = this.opts.chart?.nonce || this.w.config.chart.nonce;
if (nonce) {
this.css.setAttribute('nonce', nonce);
}

if (inShadowRoot) {
// We are in Shadow DOM, add to shadow root
Expand Down
4 changes: 4 additions & 0 deletions src/modules/legend/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export default class Helpers {
getLegendStyles() {
let stylesheet = document.createElement('style')
stylesheet.setAttribute('type', 'text/css')
const nonce = this.lgCtx.ctx?.opts?.chart?.nonce || this.w.config.chart.nonce;
if (nonce) {
stylesheet.setAttribute('nonce', nonce);
}

const text = `

Expand Down
1 change: 1 addition & 0 deletions src/modules/settings/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ export default class Options {
redrawOnWindowResize: true,
id: undefined,
group: undefined,
nonce: undefined,
offsetX: 0,
offsetY: 0,
selection: {
Expand Down
91 changes: 91 additions & 0 deletions tests/unit/nonce.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { createChartWithOptions } from './utils/utils.js'

describe('chart.nonce option', () => {
beforeEach(() => {
window.Apex = {};
});
afterEach(() => {
document.getElementsByTagName('html')[0].innerHTML = ''
})

it('undefined (default) will not render a nonce attribute', () => {
createChartWithOptions({
series: [
{
name: "A",
data: [
[1, 1],
[4, 4],
[3, 3],
],
},
{
name: "B",
data: [
[2, 2],
[5, 5],
[6, 6],
],
},
],
chart: {
type: 'bar',
},
})
expect(document.head.querySelectorAll('#apexcharts-css').length).toBe(1)
expect(
document.head.querySelectorAll('#apexcharts-css[nonce]').length
).toBe(0)
expect(document.body.querySelectorAll('foreignObject style').length).toBe(1)
})

it('will render a nonce attribute when provided as an option', () => {
createChartWithOptions({
series: [
{
data: [
[1, 1],
[4, 4],
[3, 3],
],
},
],
chart: {
type: 'bar',
nonce: 'noncevalue1',
},
})
expect(document.head.querySelectorAll('#apexcharts-css').length).toBe(1)
expect(
document.head.querySelectorAll("#apexcharts-css[nonce='noncevalue1']")
.length
).toBe(1)
})

it('will render a nonce attribute when defined as a global config', () => {
window.Apex = {
chart: {
nonce: 'noncevalue2'
}
};
createChartWithOptions({
series: [
{
data: [
[1, 1],
[4, 4],
[3, 3],
],
},
],
chart: {
type: 'bar',
},
})
expect(document.head.querySelectorAll('#apexcharts-css').length).toBe(1)
expect(
document.head.querySelectorAll("#apexcharts-css[nonce='noncevalue2']")
.length
).toBe(1)
})
})