diff --git a/packages/hast-util-to-babel-ast/package.json b/packages/hast-util-to-babel-ast/package.json index 49a193de..86e9e820 100644 --- a/packages/hast-util-to-babel-ast/package.json +++ b/packages/hast-util-to-babel-ast/package.json @@ -31,6 +31,7 @@ "prepublishOnly": "yarn run build" }, "dependencies": { - "@babel/types": "^7.15.4" + "@babel/types": "^7.15.4", + "entities": "^2.2.0" } } diff --git a/packages/hast-util-to-babel-ast/src/handlers.js b/packages/hast-util-to-babel-ast/src/handlers.js index 7c218e52..c0365c91 100644 --- a/packages/hast-util-to-babel-ast/src/handlers.js +++ b/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' @@ -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) => { diff --git a/packages/hast-util-to-babel-ast/src/index.test.js b/packages/hast-util-to-babel-ast/src/index.test.js index 4a12e906..53b40092 100644 --- a/packages/hast-util-to-babel-ast/src/index.test.js +++ b/packages/hast-util-to-babel-ast/src/index.test.js @@ -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 = `<` + expect(transform(code)).toMatchInlineSnapshot( + `"{\\"<\\"};"`, + ) + }) + + it('string literals children of tspan nodes should have decoded XML entities', () => { + const code = `<` + expect(transform(code)).toMatchInlineSnapshot( + `"{\\"<\\"};"`, + ) + }) }) diff --git a/yarn.lock b/yarn.lock index 53e548a0..b5f856c4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"