Skip to content

Commit ac7f38d

Browse files
authored
Merge pull request #1512 from maizzle/fix-preferunitlessvalues
2 parents 917627d + e891c14 commit ac7f38d

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

src/transformers/inline.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -189,30 +189,31 @@ export async function inline(html = '', options = {}) {
189189

190190
// 1. `preferUnitlessValues`
191191
if (styleAttr) {
192-
// Parse the inline styles using postcss
193-
const root = postcss.parse(`* { ${styleAttr} }`)
194-
195-
root.first.each((decl) => {
196-
const property = decl.prop
197-
let value = decl.value
198-
199-
if (value && options.preferUnitlessValues) {
200-
value = value.replace(
201-
/\b0(px|rem|em|%|vh|vw|vmin|vmax|in|cm|mm|pt|pc|ex|ch)\b/g,
202-
'0'
203-
)
204-
}
205-
206-
if (property) {
207-
inlineStyles[property] = value
208-
}
209-
})
210-
211-
// Update the element's style attribute with the new value
212-
$(el).attr(
213-
'style',
214-
Object.entries(inlineStyles).map(([property, value]) => `${property}: ${value}`).join('; ')
215-
)
192+
try {
193+
const root = postcss.parse(`* { ${styleAttr} }`)
194+
195+
root.first.each((decl) => {
196+
const property = decl.prop
197+
let value = decl.value
198+
199+
if (value && options.preferUnitlessValues) {
200+
value = value.replace(
201+
/\b0(px|rem|em|%|vh|vw|vmin|vmax|in|cm|mm|pt|pc|ex|ch)\b/g,
202+
'0'
203+
)
204+
}
205+
206+
if (property) {
207+
inlineStyles[property] = value
208+
}
209+
})
210+
211+
// Update the element's style attribute with the new value
212+
$(el).attr(
213+
'style',
214+
Object.entries(inlineStyles).map(([property, value]) => `${property}: ${value}`).join('; ')
215+
)
216+
} catch {}
216217
}
217218

218219
// Get the classes from the element's class attribute

test/transformers/inlineCSS.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ describe.concurrent('Inline CSS', () => {
138138
<p style="margin: 0px">test</p>`))
139139
})
140140

141+
test('`preferUnitlessValues` skips invalid inline CSS', async () => {
142+
const result = cleanString(
143+
await inlineCSS(`
144+
<style>.m-0 {margin: 0px}</style>
145+
<p class="m-0" style="color: #{{ $foo->theme }}">test</p>`
146+
)
147+
)
148+
149+
expect(result).toBe(cleanString(`
150+
<style></style>
151+
<p class="m-0" style="margin: 0px; color: #{{ $foo->theme }};">test</p>`))
152+
})
153+
141154
test('Works with `excludedProperties` option', async () => {
142155
expect(
143156
cleanString(

0 commit comments

Comments
 (0)