-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix #418 Use of inline styles doesn't play nice with Content Security Policy #1976
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
Changes from all commits
170ebba
168b27a
0d01844
8545cc1
b89bee7
031b3ba
a2e3318
e3a032e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -229,6 +229,8 @@ export class GridStack { | |
public _gsEventHandler = {}; | ||
/** @internal */ | ||
protected _styles: GridCSSStyleSheet; | ||
/** @internal max row index of the gridstack*/ | ||
private _maxRowIndex: number; | ||
/** @internal flag to keep cells square during resize */ | ||
protected _isAutoCellHeight: boolean; | ||
/** @internal track event binding to window resize so we can remove */ | ||
|
@@ -476,6 +478,10 @@ export class GridStack { | |
|
||
this._triggerAddEvent(); | ||
this._triggerChangeEvent(); | ||
if(!Utils.isConstructableStyleSheetSupported()) { | ||
Utils.updatePositionStyleOnWidget(el, this.opts.cellHeight as number, this.opts.cellHeightUnit); | ||
this._updateElementChildrenStyling(el); | ||
} | ||
|
||
return el; | ||
} | ||
|
@@ -1180,16 +1186,18 @@ export class GridStack { | |
protected _removeStylesheet(): GridStack { | ||
|
||
if (this._styles) { | ||
Utils.removeStylesheet(this._styles._id); | ||
Utils.removeStylesheet(this._styles); | ||
delete this._styles; | ||
} | ||
return this; | ||
} | ||
|
||
/** @internal updated/create the CSS styles for row based layout and initial margin setting */ | ||
/** @internal updated/create the CSS styles for row based layout and initial margin setting | ||
* in case no constructable stylesheet support updated/create the CSS styles on elements for row based layout and initial margin setting */ | ||
protected _updateStyles(forceUpdate = false, maxH?: number): GridStack { | ||
// call to delete existing one if we change cellHeight / margin | ||
if (forceUpdate) { | ||
this._maxRowIndex = undefined; | ||
this._removeStylesheet(); | ||
} | ||
|
||
|
@@ -1204,12 +1212,56 @@ export class GridStack { | |
let cellHeightUnit = this.opts.cellHeightUnit; | ||
let prefix = `.${this.opts._styleSheetClass} > .${this.opts.itemClass}`; | ||
|
||
if(Utils.isConstructableStyleSheetSupported()) { | ||
return this._updateStyleRules(cellHeight, cellHeightUnit, prefix, maxH); | ||
} else { | ||
return this._updatedStylesForElements(cellHeight, cellHeightUnit, prefix, maxH); | ||
} | ||
} | ||
|
||
/** @internal update Styling */ | ||
protected _updateElementChildrenStyling(el?: HTMLElement, prefix?: string): void { | ||
let top: string = this.opts.marginTop + this.opts.marginUnit; | ||
let bottom: string = this.opts.marginBottom + this.opts.marginUnit; | ||
let right: string = this.opts.marginRight + this.opts.marginUnit; | ||
let left: string = this.opts.marginLeft + this.opts.marginUnit; | ||
let content: string; | ||
let placeholder = `.${this.opts._styleSheetClass} > .grid-stack-placeholder > .placeholder-content`; | ||
if (el!==undefined) { | ||
content = `.grid-stack-item-content` | ||
// content margins | ||
Utils.updateStyleOnElements([el.querySelector(content) as HTMLElement], {top, right, bottom, left}); | ||
Utils.updateStyleOnElements(placeholder, {top, right, bottom, left}); | ||
// resize handles offset (to match margin) | ||
Utils.updateStyleOnElements([el.querySelector(`.ui-resizable-ne`) as HTMLElement], {right}); | ||
Utils.updateStyleOnElements([el.querySelector(`.ui-resizable-e`) as HTMLElement], {right}); | ||
Utils.updateStyleOnElements([el.querySelector(`.ui-resizable-se`) as HTMLElement], {right, bottom}); | ||
Utils.updateStyleOnElements([el.querySelector(`.ui-resizable-nw`) as HTMLElement], {left}); | ||
Utils.updateStyleOnElements([el.querySelector(`.ui-resizable-w`) as HTMLElement], {left}); | ||
Utils.updateStyleOnElements([el.querySelector(`.ui-resizable-sw`) as HTMLElement], {left, bottom}); | ||
} else if (prefix!= undefined) { | ||
content = `${prefix} > .grid-stack-item-content`; | ||
// content margins | ||
Utils.updateStyleOnElements(content, {top, right, bottom, left}); | ||
Utils.updateStyleOnElements(placeholder, {top, right, bottom, left}); | ||
// resize handles offset (to match margin) | ||
Utils.updateStyleOnElements(`${prefix} > .ui-resizable-ne`, {right}); | ||
Utils.updateStyleOnElements(`${prefix} > .ui-resizable-e`, {right}); | ||
Utils.updateStyleOnElements(`${prefix} > .ui-resizable-se`, {right, bottom}); | ||
Utils.updateStyleOnElements(`${prefix} > .ui-resizable-nw`, {left}); | ||
Utils.updateStyleOnElements(`${prefix} > .ui-resizable-w`, {left}); | ||
Utils.updateStyleOnElements(`${prefix} > .ui-resizable-sw`, {left, bottom}); | ||
} | ||
} | ||
|
||
/**@internal */ | ||
protected _updateStyleRules(cellHeight:number, cellHeightUnit:string, prefix:string, maxH?: number): GridStack { | ||
// create one as needed | ||
if (!this._styles) { | ||
let id = 'gridstack-style-' + (Math.random() * 100000).toFixed(); | ||
// insert style to parent (instead of 'head' by default) to support WebComponent | ||
let styleLocation = this.opts.styleInHead ? undefined : this.el.parentNode as HTMLElement; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this._styles = Utils.createStylesheet(id, styleLocation); | ||
this._styles = Utils.createStylesheet(styleLocation); | ||
if (!this._styles) return this; | ||
this._styles._id = id; | ||
this._styles._max = 0; | ||
|
@@ -1250,6 +1302,32 @@ export class GridStack { | |
return this; | ||
} | ||
|
||
/**@internal updates styles for dirty nodes */ | ||
protected _updatedStylesForElements(cellHeight:number, cellHeightUnit:string, prefix:string, maxH?: number): GridStack { | ||
// create one as needed | ||
if (!this._maxRowIndex) { | ||
this._maxRowIndex = 0; | ||
|
||
// these are done once only | ||
Utils.updateStyleOnElements(prefix, {'min-height': cellHeight+cellHeightUnit}); | ||
this._updateElementChildrenStyling(undefined, prefix); | ||
} | ||
|
||
// apply position styling on all dirty node's elements | ||
this.getGridItems().forEach((w: GridItemHTMLElement) => { | ||
if (w.gridstackNode._dirty && !w.classList.contains("ui-resizable-resizing")) { | ||
Utils.updatePositionStyleOnWidget(w, this.opts.cellHeight as number, this.opts.cellHeightUnit); | ||
} | ||
}); | ||
|
||
// now update the height specific fields | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment doesn't apply to code below, which looks like what you should do instead of the |
||
maxH = maxH || this._maxRowIndex; | ||
if (maxH > this._maxRowIndex) { | ||
this._maxRowIndex = maxH; | ||
} | ||
return this; | ||
} | ||
|
||
/** @internal */ | ||
protected _updateContainerHeight(): GridStack { | ||
if (!this.engine || this.engine.batchMode) return this; | ||
|
@@ -1301,6 +1379,10 @@ export class GridStack { | |
if (n.y !== undefined && n.y !== null) { el.setAttribute('gs-y', String(n.y)); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the all reason for writing gs-x, gs-y, gs-w, gs-h IS to use global CSS styling to position them, but if we use attributes directly instead I don't see a reason to have any of this anymore... which is a much bigger change. |
||
if (n.w) { el.setAttribute('gs-w', String(n.w)); } | ||
if (n.h) { el.setAttribute('gs-h', String(n.h)); } | ||
if(!Utils.isConstructableStyleSheetSupported()) { | ||
Utils.updatePositionStyleOnWidget(el, this.opts.cellHeight as number, this.opts.cellHeightUnit); | ||
this._updateElementChildrenStyling(el); | ||
} | ||
return this; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
every single new element will also need those attributes, so how do you re-use this code since it isn't a css style anymore ?