Skip to content

Commit 7c5748a

Browse files
committed
Make prop value escaping more HTM-specific
Now prop values get wrapped into expressions only in the following cases: * The value contains one or more newlines. * The value contains both single AND double quotes. Added tests for unicode text and prop values and modified existing tests to match the new escaping scheme.
1 parent f2b0a0d commit 7c5748a

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

packages/babel-plugin-transform-jsx-to-tagged-templates/src/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
3636
function escapeStringLiteral(node) {
3737
const value = node.value;
3838

39-
if (value.match(/^[a-z0-9_'" ]*$/gi)) {
39+
if (value.match(/^.*$/u)) {
4040
if (value.indexOf('"') < 0) {
4141
return raw(`"${value}"`);
4242
}
@@ -45,7 +45,7 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
4545
}
4646
}
4747

48-
return expr(node);
48+
return expr(t.stringLiteral(node.value));
4949
}
5050

5151
function commit(force) {

packages/babel-plugin-transform-jsx-to-tagged-templates/test/index.test.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ describe('babel-plugin-transform-jsx-to-tagged-templates', () => {
3232
compile('(<Foo>a</Foo>);')
3333
).toBe('html`<${Foo}>a</${Foo}>`;');
3434
});
35+
36+
test('static text', () => {
37+
expect(
38+
compile(`(<div>Hello</div>);`)
39+
).toBe('html`<div>Hello</div>`;');
40+
expect(
41+
compile(`(<div>こんにちわ</div>);`)
42+
).toBe('html`<div>こんにちわ</div>`;');
43+
});
44+
45+
test('HTML entities get unescaped', () => {
46+
expect(
47+
compile(`(<div>&amp;</div>);`)
48+
).toBe('html`<div>&</div>`;');
49+
});
3550
});
3651

3752
describe('options.html = true', () => {
@@ -55,6 +70,15 @@ describe('babel-plugin-transform-jsx-to-tagged-templates', () => {
5570
expect(
5671
compile('(<div a="a" b="bb" c d />);')
5772
).toBe('html`<div a="a" b="bb" c d/>`;');
73+
expect(
74+
compile('(<div a="こんにちわ" />);')
75+
).toBe('html`<div a="こんにちわ"/>`;');
76+
});
77+
78+
test('HTML entities get unescaped', () => {
79+
expect(
80+
compile(`(<div a="&amp;" />);`)
81+
).toBe('html`<div a="&"/>`;');
5882
});
5983

6084
test('double quote values with single quotes', () => {
@@ -69,12 +93,18 @@ describe('babel-plugin-transform-jsx-to-tagged-templates', () => {
6993
).toBe(`html\`<div a='"b"'/>\`;`);
7094
});
7195

72-
test('escape non-trivial values as expressions', () => {
96+
test('escape values with newlines as expressions', () => {
7397
expect(
74-
compile(`(<div a="\x00" b="&abcd;" />);`)
75-
).toBe('html`<div a=${"\x00"} b=${"&abcd;"}/>`;');
98+
compile(`(<div a="\n" />);`)
99+
).toBe('html`<div a=${"\\n"}/>`;');
76100
});
77101

102+
test('escape values with both single and double quotes as expressions', () => {
103+
expect(
104+
compile(`(<div a="&#34;'" />);`)
105+
).toBe('html`<div a=${"\\"\'"}/>`;');
106+
});
107+
78108
test('expression values', () => {
79109
expect(
80110
compile('const Foo = (props, a) => <div a={a} b={"b"} c={{}} d={props.d} e />;')

0 commit comments

Comments
 (0)