Skip to content

Commit b3998eb

Browse files
jsamrmicheljung
andauthoredSep 12, 2021
fix: fix XML entities in generated JavaScript (#553)
Co-authored-by: Michel Jung <michel.jung89@gmail.com>
1 parent 7e890a9 commit b3998eb

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed
 

‎packages/hast-util-to-babel-ast/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"prepublishOnly": "yarn run build"
3232
},
3333
"dependencies": {
34-
"@babel/types": "^7.15.4"
34+
"@babel/types": "^7.15.4",
35+
"entities": "^2.2.0"
3536
}
3637
}

‎packages/hast-util-to-babel-ast/src/handlers.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as t from '@babel/types'
2+
import { decodeXML } from 'entities'
23
import all from './all'
34
import getAttributes from './getAttributes'
45
import { ELEMENT_TAG_NAME_MAPPING } from './mappings'
@@ -24,7 +25,7 @@ export const text = (h, node, parent) => {
2425
return null
2526
}
2627

27-
return t.jsxExpressionContainer(t.stringLiteral(node.value))
28+
return t.jsxExpressionContainer(t.stringLiteral(decodeXML(node.value)))
2829
}
2930

3031
export const element = (h, node, parent) => {

‎packages/hast-util-to-babel-ast/src/index.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,18 @@ describe('hast-util-to-babel-ast', () => {
6969

7070
expect(transform(code)).toMatchSnapshot()
7171
})
72+
73+
it('string literals children of text nodes should have decoded XML entities', () => {
74+
const code = `<svg><text>&lt;</text></svg>`
75+
expect(transform(code)).toMatchInlineSnapshot(
76+
`"<svg><text>{\\"<\\"}</text></svg>;"`,
77+
)
78+
})
79+
80+
it('string literals children of tspan nodes should have decoded XML entities', () => {
81+
const code = `<svg><text><tspan>&lt;</tspan></text></svg>`
82+
expect(transform(code)).toMatchInlineSnapshot(
83+
`"<svg><text><tspan>{\\"<\\"}</tspan></text></svg>;"`,
84+
)
85+
})
7286
})

‎yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -4890,6 +4890,11 @@ entities@^2.0.0:
48904890
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
48914891
integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==
48924892

4893+
entities@^2.2.0:
4894+
version "2.2.0"
4895+
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
4896+
integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==
4897+
48934898
env-paths@^2.2.0:
48944899
version "2.2.1"
48954900
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2"

1 commit comments

Comments
 (1)

vercel[bot] commented on Sep 12, 2021

@vercel[bot]
Please sign in to comment.