Skip to content

Commit 079baa0

Browse files
committed
Add ignoreInvalidStyle option to swallow error
1 parent 08cfe9c commit 079baa0

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

lib/index.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
*
8181
* Passed in source info to `jsxDEV` when using the automatic runtime with
8282
* `development: true`.
83+
* @property {boolean | null | undefined} [ignoreInvalidStyle=false]
84+
* Ignore invalid CSS in `style` props (default: `false`);
85+
* the default behavior is to throw an error.
8386
* @property {boolean | null | undefined} [passKeys=true]
8487
* Generate keys to optimize frameworks that support them (default: `true`).
8588
*
@@ -170,6 +173,8 @@
170173
* Casing to use for attribute names.
171174
* @property {string | undefined} filePath
172175
* File path.
176+
* @property {boolean} ignoreInvalidStyle
177+
* Ignore invalid CSS in `style` props.
173178
* @property {boolean} passKeys
174179
* Generate keys to optimize frameworks that support them.
175180
* @property {boolean} passNode
@@ -285,6 +290,7 @@ export function toJsxRuntime(tree, options) {
285290
create,
286291
elementAttributeNameCase: options.elementAttributeNameCase || 'react',
287292
filePath,
293+
ignoreInvalidStyle: options.ignoreInvalidStyle || false,
288294
passKeys: options.passKeys !== false,
289295
passNode: options.passNode || false,
290296
schema: options.space === 'svg' ? svg : html,
@@ -607,19 +613,20 @@ function parseStyle(state, ancestors, value) {
607613
try {
608614
styleToObject(value, replacer)
609615
} catch (error) {
610-
const cause = /** @type {Error} */ (error)
611-
612-
const message = new VFileMessage('Cannot parse `style` attribute', {
613-
ancestors,
614-
cause,
615-
source: 'hast-util-to-jsx-runtime',
616-
ruleId: 'style'
617-
})
618-
message.file = state.filePath || undefined
619-
message.url =
620-
'https://github.com/syntax-tree/hast-util-to-jsx-runtime#cannot-parse-style-attribute'
621-
622-
throw message
616+
if (!state.ignoreInvalidStyle) {
617+
const cause = /** @type {Error} */ (error)
618+
const message = new VFileMessage('Cannot parse `style` attribute', {
619+
ancestors,
620+
cause,
621+
source: 'hast-util-to-jsx-runtime',
622+
ruleId: 'style'
623+
})
624+
message.file = state.filePath || undefined
625+
message.url =
626+
'https://github.com/syntax-tree/hast-util-to-jsx-runtime#cannot-parse-style-attribute'
627+
628+
throw message
629+
}
623630
}
624631

625632
return result

readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ CSS, and pass it as an object.
170170
But when broken CSS is used, such as `style="color:red; /*"`, we crash.
171171

172172
To solve the error, make sure authors write valid CSS.
173+
Alternatively, pass `options.ignoreInvalidStyle: true` to swallow theses
174+
errors.
173175

174176
### `Options`
175177

test/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,21 @@ test('properties', async function (t) {
341341
}
342342
)
343343

344+
await t.test(
345+
'should not crash on invalid style strings w/ `ignoreInvalidStyle`',
346+
async function () {
347+
assert.equal(
348+
renderToStaticMarkup(
349+
toJsxRuntime(h('div', {style: 'color:red; /*'}), {
350+
...production,
351+
ignoreInvalidStyle: true
352+
})
353+
),
354+
'<div></div>'
355+
)
356+
}
357+
)
358+
344359
await t.test('should support properties in the SVG space', async function () {
345360
assert.equal(
346361
renderToStaticMarkup(

0 commit comments

Comments
 (0)