Skip to content

Commit 12d58bd

Browse files
authored
fix(module:core): avoid using setAttribute to set style (#9292)
1 parent da97b02 commit 12d58bd

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

components/core/util/text-measure.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ export function pxToNumber(value: string | null): number {
3434
return match ? Number(match[0]) : 0;
3535
}
3636

37-
function styleToString(style: CSSStyleDeclaration): string {
37+
function styleToObject(style: CSSStyleDeclaration): Record<string, string> {
3838
// There are some different behavior between Firefox & Chrome.
3939
// We have to handle this ourself.
40+
const styles: Record<string, string> = {};
4041
const styleNames: string[] = Array.prototype.slice.apply(style);
41-
return styleNames.map(name => `${name}: ${style.getPropertyValue(name)};`).join('');
42+
for (const name of styleNames) {
43+
styles[name] = style.getPropertyValue(name);
44+
}
45+
return styles;
4246
}
4347

4448
function mergeChildren(children: Node[]): Node[] {
@@ -72,13 +76,16 @@ export function measure(
7276

7377
// Get origin style
7478
const originStyle = window.getComputedStyle(originEle);
75-
const originCSS = styleToString(originStyle);
79+
const originCSS = styleToObject(originStyle);
7680
const lineHeight = pxToNumber(originStyle.lineHeight);
7781
const maxHeight = Math.round(
7882
lineHeight * (rows + 1) + pxToNumber(originStyle.paddingTop) + pxToNumber(originStyle.paddingBottom)
7983
);
8084
// Set shadow
81-
ellipsisContainer.setAttribute('style', originCSS);
85+
for (const [name, value] of Object.entries(originCSS)) {
86+
// setAttribute('style', ...) is not allowed when strict CSP is in place.
87+
ellipsisContainer.style.setProperty(name, value);
88+
}
8289
ellipsisContainer.style.position = 'fixed';
8390
ellipsisContainer.style.left = '0';
8491
ellipsisContainer.style.height = 'auto';

0 commit comments

Comments
 (0)