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: avoid inline styles to ensure CSP compliancy #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
71 changes: 35 additions & 36 deletions src/calculateNodeHeight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@
* calculateNodeHeight(uiTextNode, useCache = false)
*/

const HIDDEN_TEXTAREA_STYLE = `
min-height:0 !important;
max-height:none !important;
height:0 !important;
visibility:hidden !important;
overflow:hidden !important;
position:absolute !important;
z-index:-1000 !important;
top:0 !important;
right:0 !important;
pointer-events: none !important;
`;
const HIDDEN_TEXTAREA_STYLE = {
minHeight: '0 !important',
maxHeight: 'none !important',
height: '0 !important',
visibility: 'hidden !important',
overflow: 'hidden !important',
position: 'absolute !important',
zIndex: '-1000 !important',
top: '0 !important',
right: '0 !important',
pointerEvents: 'none !important',
};

const SIZING_STYLE = [
'letter-spacing',
'line-height',
'padding-top',
'padding-bottom',
'font-family',
'font-weight',
'font-size',
'font-variant',
'text-rendering',
'text-transform',
'letterSpacing',
'lineHeight',
'paddingTop',
'paddingBottom',
'fontFamily',
'fontWeight',
'fontSize',
'fontVariant',
'textRendering',
'textTransform',
'width',
'text-indent',
'padding-left',
'padding-right',
'border-width',
'box-sizing',
'word-break',
'white-space',
'textIndent',
'paddingLeft',
'paddingRight',
'borderWidth',
'boxSizing',
'wordBreak',
'whiteSpace',
];

export interface NodeType {
Expand Down Expand Up @@ -73,9 +73,9 @@
parseFloat(style.getPropertyValue('border-bottom-width')) +
parseFloat(style.getPropertyValue('border-top-width'));

const sizingStyle = SIZING_STYLE.map(
(name) => `${name}:${style.getPropertyValue(name)}`,
).join(';');
const sizingStyle = Object.fromEntries(SIZING_STYLE.map(
(name) => [name, style.getPropertyValue(name.replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`))],
));

const nodeInfo: NodeType = {
sizingStyle,
Expand Down Expand Up @@ -123,10 +123,9 @@
// Need to have the overflow attribute to hide the scrollbar otherwise
// text-lines will not calculated properly as the shadow will technically be
// narrower for content
hiddenTextarea.setAttribute(
'style',
`${sizingStyle};${HIDDEN_TEXTAREA_STYLE}`,
);
for (const [name, value] of Object.entries({ ...sizingStyle, ...HIDDEN_TEXTAREA_STYLE })) {
hiddenTextarea.style[name] = value

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (96% of all statements in
the enclosing function
have an explicit semicolon).
}
hiddenTextarea.value = uiTextNode.value || uiTextNode.placeholder || '';

let minHeight: number | undefined = undefined;
Expand Down
Loading