Skip to content

Commit 968b6bc

Browse files
authored
Merge pull request #2229 from jcoreio/nonce
feat: support nonce for CSP
2 parents 5ed51bb + e1a4aa1 commit 968b6bc

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

doc/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ gridstack.js API
118118
- `marginLeft`: numberOrString
119119
- `maxRow` - maximum rows amount. Default is `0` which means no max.
120120
- `minRow` - minimum rows amount which is handy to prevent grid from collapsing when empty. Default is `0`. You can also do this with `min-height` CSS attribute on the grid div in pixels, which will round to the closest row.
121+
- `nonce` - If you are using a nonce-based Content Security Policy, pass your nonce here and
122+
GridStack will add it to the <style> elements it creates.
121123
- `oneColumnSize` - minimal width. If grid width is less than or equal to, grid will be shown in one-column mode (default: `768`)
122124
- `oneColumnModeDomSort` - set to `true` if you want oneColumnMode to use the DOM order and ignore x,y from normal multi column layouts during sorting. This enables you to have custom 1 column layout that differ from the rest. (default?: `false`)
123125
- `placeholderClass` - class for placeholder (default: `'grid-stack-placeholder'`)

src/gridstack.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,9 @@ export class GridStack {
13271327
if (!this._styles) {
13281328
// insert style to parent (instead of 'head' by default) to support WebComponent
13291329
let styleLocation = this.opts.styleInHead ? undefined : this.el.parentNode as HTMLElement;
1330-
this._styles = Utils.createStylesheet(this._styleSheetClass, styleLocation);
1330+
this._styles = Utils.createStylesheet(this._styleSheetClass, styleLocation, {
1331+
nonce: this.opts.nonce,
1332+
});
13311333
if (!this._styles) return this;
13321334
this._styles._max = 0;
13331335

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ export interface GridStackOptions {
205205
*/
206206
minRow?: number;
207207

208+
/** If you are using a nonce-based Content Security Policy, pass your nonce here and
209+
* GridStack will add it to the <style> elements it creates. */
210+
nonce?: string;
211+
208212
/** minimal width before grid will be shown in one column mode (default?: 768) */
209213
oneColumnSize?: number;
210214

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@ export class Utils {
137137
* @param parent to insert the stylesheet as first child,
138138
* if none supplied it will be appended to the document head instead.
139139
*/
140-
static createStylesheet(id: string, parent?: HTMLElement): CSSStyleSheet {
140+
static createStylesheet(id: string, parent?: HTMLElement, options?: { nonce?: string }): CSSStyleSheet {
141141
let style: HTMLStyleElement = document.createElement('style');
142+
const nonce = options?.nonce
143+
if (nonce) style.nonce = nonce
142144
style.setAttribute('type', 'text/css');
143145
style.setAttribute('gs-style-id', id);
144146
// eslint-disable-next-line @typescript-eslint/no-explicit-any

0 commit comments

Comments
 (0)