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

fix: dynamically create CSS rules via JS instead of innerHTML #883

Merged
merged 2 commits into from
Oct 27, 2023
Merged
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
45 changes: 20 additions & 25 deletions src/slick.grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
protected _topPanels!: HTMLDivElement[];
protected _viewport!: HTMLDivElement[];
protected _canvas!: HTMLDivElement[];
protected _style: any;
protected _style?: HTMLStyleElement;
protected _boundAncestors: HTMLElement[] = [];
protected stylesheet?: { cssRules: Array<{ selectorText: string; }>; rules: Array<{ selectorText: string; }>; } | null;
protected columnCssRulesL?: Array<{ selectorText: string; }>;
Expand Down Expand Up @@ -2299,33 +2299,28 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
}

protected createCssRules() {
const template = Utils.createDomElement('template', { innerHTML: '<style type="text/css" rel="stylesheet" />' });
this._style = template.content.firstChild;
this._style = document.createElement('style');
this._style.nonce = 'random-string';
document.head.appendChild(this._style);

const rowHeight = (this._options.rowHeight! - this.cellHeightDiff);
const rules = [
`.${this.uid} .slick-group-header-column { left: 1000px; }`,
`.${this.uid} .slick-header-column { left: 1000px; }`,
`.${this.uid} .slick-top-panel { height: ${this._options.topPanelHeight}px; }`,
`.${this.uid} .slick-preheader-panel { height: ${this._options.preHeaderPanelHeight}px; }`,
`.${this.uid} .slick-headerrow-columns { height: ${this._options.headerRowHeight}px; }`,
`.${this.uid} .slick-footerrow-columns { height: ${this._options.footerRowHeight}px; }`,
`.${this.uid} .slick-cell { height: ${rowHeight}px; }`,
`.${this.uid} .slick-row { height: ${this._options.rowHeight}px; }`
];
const sheet = this._style.sheet;
if (sheet) {
const rowHeight = (this._options.rowHeight! - this.cellHeightDiff);
sheet.insertRule(`.${this.uid} .slick-group-header-column { left: 1000px; }`);
sheet.insertRule(`.${this.uid} .slick-header-column { left: 1000px; }`);
sheet.insertRule(`.${this.uid} .slick-top-panel { height: ${this._options.topPanelHeight}px; }`);
sheet.insertRule(`.${this.uid} .slick-preheader-panel { height: ${this._options.preHeaderPanelHeight}px; }`);
sheet.insertRule(`.${this.uid} .slick-headerrow-columns { height: ${this._options.headerRowHeight}px; }`);
sheet.insertRule(`.${this.uid} .slick-footerrow-columns { height: ${this._options.footerRowHeight}px; }`);
sheet.insertRule(`.${this.uid} .slick-cell { height: ${rowHeight}px; }`);
sheet.insertRule(`.${this.uid} .slick-row { height: ${this._options.rowHeight}px; }`);

for (let i = 0; i < this.columns.length; i++) {
if (!this.columns[i] || this.columns[i].hidden) { continue; }

rules.push(`.${this.uid} .l${i} { }`);
rules.push(`.${this.uid} .r${i} { }`);
}
for (let i = 0; i < this.columns.length; i++) {
if (!this.columns[i] || this.columns[i].hidden) { continue; }

if (this._style.styleSheet) { // IE
this._style.styleSheet.cssText = rules.join(' ');
} else {
this._style.appendChild(document.createTextNode(rules.join(' ')));
sheet.insertRule(`.${this.uid} .l${i} { }`);
sheet.insertRule(`.${this.uid} .r${i} { }`);
}
}
}

Expand Down Expand Up @@ -2369,7 +2364,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
}

protected removeCssRules() {
this._style.remove();
this._style?.remove();
this.stylesheet = null;
}

Expand Down