File tree Expand file tree Collapse file tree 2 files changed +38
-24
lines changed
Expand file tree Collapse file tree 2 files changed +38
-24
lines changed Original file line number Diff line number Diff 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- / \b 0 ( p x | r e m | e m | % | v h | v w | v m i n | v m a x | i n | c m | m m | p t | p c | e x | c h ) \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+ / \b 0 ( p x | r e m | e m | % | v h | v w | v m i n | v m a x | i n | c m | m m | p t | p c | e x | c h ) \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
Original file line number Diff line number Diff 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 (
You can’t perform that action at this time.
0 commit comments