(
inlineStyle: string,
options: { removeUnit?: boolean } = {},
)
| 50 | }; |
| 51 | |
| 52 | export const inlineCssToJs = ( |
| 53 | inlineStyle: string, |
| 54 | options: { removeUnit?: boolean } = {}, |
| 55 | ) => { |
| 56 | const styleObject: { [key: string]: string } = {}; |
| 57 | |
| 58 | if (!inlineStyle || inlineStyle === '' || typeof inlineStyle === 'object') { |
| 59 | return styleObject; |
| 60 | } |
| 61 | |
| 62 | splitDeclarations(inlineStyle).forEach((style: string) => { |
| 63 | const trimmed = style.trim(); |
| 64 | if (trimmed) { |
| 65 | const separatorIndex = trimmed.indexOf(':'); |
| 66 | if (separatorIndex === -1) return; |
| 67 | |
| 68 | const key = trimmed.slice(0, separatorIndex); |
| 69 | const value = trimmed.slice(separatorIndex + 1); |
| 70 | const valueTrimmed = value.trim(); |
| 71 | |
| 72 | if (!valueTrimmed) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | const formattedKey = key |
| 77 | .trim() |
| 78 | .replace(/-\w/g, (match) => match[1].toUpperCase()); |
| 79 | |
| 80 | const UNIT_REGEX = /px|%/g; |
| 81 | const sanitizedValue = options?.removeUnit |
| 82 | ? valueTrimmed.replace(UNIT_REGEX, '') |
| 83 | : valueTrimmed; |
| 84 | |
| 85 | styleObject[formattedKey] = sanitizedValue; |
| 86 | } |
| 87 | }); |
| 88 | |
| 89 | return styleObject; |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * Expands CSS shorthand properties (margin, padding) into their longhand equivalents. |
no test coverage detected