Skip to content

Commit

Permalink
fix: fix XML entities in generated JavaScript (#553)
Browse files Browse the repository at this point in the history
Co-authored-by: Michel Jung <michel.jung89@gmail.com>
  • Loading branch information
jsamr and micheljung committed Sep 12, 2021
1 parent 7e890a9 commit b3998eb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/hast-util-to-babel-ast/package.json
Expand Up @@ -31,6 +31,7 @@
"prepublishOnly": "yarn run build"
},
"dependencies": {
"@babel/types": "^7.15.4"
"@babel/types": "^7.15.4",
"entities": "^2.2.0"
}
}
3 changes: 2 additions & 1 deletion packages/hast-util-to-babel-ast/src/handlers.js
@@ -1,4 +1,5 @@
import * as t from '@babel/types'
import { decodeXML } from 'entities'
import all from './all'
import getAttributes from './getAttributes'
import { ELEMENT_TAG_NAME_MAPPING } from './mappings'
Expand All @@ -24,7 +25,7 @@ export const text = (h, node, parent) => {
return null
}

return t.jsxExpressionContainer(t.stringLiteral(node.value))
return t.jsxExpressionContainer(t.stringLiteral(decodeXML(node.value)))
}

export const element = (h, node, parent) => {
Expand Down
14 changes: 14 additions & 0 deletions packages/hast-util-to-babel-ast/src/index.test.js
Expand Up @@ -69,4 +69,18 @@ describe('hast-util-to-babel-ast', () => {

expect(transform(code)).toMatchSnapshot()
})

it('string literals children of text nodes should have decoded XML entities', () => {
const code = `<svg><text>&lt;</text></svg>`
expect(transform(code)).toMatchInlineSnapshot(
`"<svg><text>{\\"<\\"}</text></svg>;"`,
)
})

it('string literals children of tspan nodes should have decoded XML entities', () => {
const code = `<svg><text><tspan>&lt;</tspan></text></svg>`
expect(transform(code)).toMatchInlineSnapshot(
`"<svg><text><tspan>{\\"<\\"}</tspan></text></svg>;"`,
)
})
})
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -4890,6 +4890,11 @@ entities@^2.0.0:
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==

entities@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==

env-paths@^2.2.0:
version "2.2.1"
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2"
Expand Down

1 comment on commit b3998eb

@vercel
Copy link

@vercel vercel bot commented on b3998eb Sep 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.