Skip to content

Commit

Permalink
fix: XML entities should be decoded in string literals
Browse files Browse the repository at this point in the history
fix #516
  • Loading branch information
jsamr authored and gregberge committed Sep 12, 2021
1 parent 7e890a9 commit f0dfde2
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,18 @@ describe('hast-util-to-babel-ast', () => {

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

it('string litterals 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 litterals 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
Original file line number Diff line number Diff line change
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

0 comments on commit f0dfde2

Please sign in to comment.