-
-
Notifications
You must be signed in to change notification settings - Fork 422
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
@svgr/hast-util-to-babel-ast erroneously treats semicolons in XML entities as style delimiters #963
Comments
SethFalco
changed the title
@svgr/hast-util-to-babel-ast erroneously treats semicolons in XML entities are style property delimiters
@svgr/hast-util-to-babel-ast erroneously treats semicolons in XML entities as style property delimiters
May 20, 2024
SethFalco
changed the title
@svgr/hast-util-to-babel-ast erroneously treats semicolons in XML entities as style property delimiters
@svgr/hast-util-to-babel-ast erroneously treats semicolons in XML entities as style delimiters
May 20, 2024
I've written a version of the proposed
/**
* Split raw styles into separate properties.
*/
const splitStyles = (rawStyle: string) => {
const entries = []
let currentEntry = ''
for (const char of rawStyle) {
if (char !== ';' || /&(?:[a-zA-Z]+|#\d+)(?<!&semi|;)$/.test(currentEntry)) {
currentEntry += char
continue
}
entries.push(currentEntry)
currentEntry = ''
}
if (currentEntry.length !== 0) {
entries.push(currentEntry)
}
return entries
} Usage would be to change: - const entries = rawStyle.split(';')
+ const entries = splitStyles(rawStyle) |
Hi, folks. I encountered the issue when having |
7 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🐛 Bug Report
The
@svgr/hast-util-to-babel-ast
package doesn't split styles correctly, and so breaks some SVGs that are processed through it.It doesn't account for XML entities such as
"
or<
, which both contain a semicolon which is acceptable when styles are defined in HTML, and the semicolon should not be considered a delimiter.To Reproduce
I've written a test case for
hast-util-to-babel-ast
:packages/hast-util-to-babel-ast/src/index.test.ts
Or alternatively, a test case for
core
:packages/core/src/transform.test.ts
This currently creates the following snapshot, which is wrong but demonstrates the issue:
When SVGO serializing the
font-family
property back to a string, it uses XML entities to escape the quotes, so:SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace
becomes
SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace
Then
plugin-jsx
erroneously splits thestyle
attribute by;
, which splits the style in the middle of property values as XML entities contain a semicolon too.Expected behavior
Semicolons that are part of an XML entity should be treated as a delimiter/separator for style properties.
The best solution would probably be to use an actual CSS parser. If that's not favorable for whatever reason, then there should be a
splitStyles
method defined inpackages/hast-util-to-babel-ast/src/stringToObjectStyle.ts
which goes character by character through the raw styles to avoid XML entities.I'd be happy to look at this for you if you can confirm if you'd prefer to use a CSS parser, or a DIY solution?
Run
npx envinfo --system --binaries --npmPackages @svgr/core,@svgr/cli,@svgr/webpack,@svgr/rollup --markdown --clipboard
The text was updated successfully, but these errors were encountered: