Skip to content

Commit 6e543e3

Browse files
committed
Wrap text parts containing < into expression containers
1 parent 7c5748a commit 6e543e3

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
3333
buffer += str;
3434
}
3535

36-
function escapeStringLiteral(node) {
36+
function escapeText(text) {
37+
if (text.indexOf('<') < 0) {
38+
return raw(text);
39+
}
40+
return expr(t.stringLiteral(text));
41+
}
42+
43+
function escapePropValue(node) {
3744
const value = node.value;
38-
45+
3946
if (value.match(/^.*$/u)) {
4047
if (value.indexOf('"') < 0) {
4148
return raw(`"${value}"`);
@@ -47,7 +54,7 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
4754

4855
return expr(t.stringLiteral(node.value));
4956
}
50-
57+
5158
function commit(force) {
5259
if (!buffer && !force) return;
5360
quasis.push(t.templateElement({
@@ -87,7 +94,7 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
8794
expr(value.expression);
8895
}
8996
else if (t.isStringLiteral(value)) {
90-
escapeStringLiteral(value);
97+
escapePropValue(value);
9198
}
9299
else {
93100
expr(value);
@@ -96,24 +103,20 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
96103
}
97104
}
98105

99-
if (htmlOutput || node.children && node.children.length !== 0) {
106+
const children = t.react.buildChildren(node);
107+
if (htmlOutput || children && children.length !== 0) {
100108
raw('>');
101-
for (let i = 0; i < node.children.length; i++) {
102-
let child = node.children[i];
103-
if (t.isJSXText(child)) {
109+
for (let i = 0; i < children.length; i++) {
110+
let child = children[i];
111+
if (t.isStringLiteral(child)) {
104112
// @todo - expose `whitespace: true` option?
105-
raw(child.value.trim());
113+
escapeText(child.value);
114+
}
115+
else if (t.isJSXElement(child)) {
116+
processNode(child);
106117
}
107118
else {
108-
if (t.isJSXExpressionContainer(child)) {
109-
child = child.expression;
110-
}
111-
if (t.isJSXElement(child)) {
112-
processNode(child);
113-
}
114-
else {
115-
expr(child);
116-
}
119+
expr(child);
117120
}
118121
}
119122

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ describe('babel-plugin-transform-jsx-to-tagged-templates', () => {
4747
compile(`(<div>&amp;</div>);`)
4848
).toBe('html`<div>&</div>`;');
4949
});
50+
51+
test('&lt; gets wrapped into an expression container', () => {
52+
expect(
53+
compile(`(<div>a&lt;b&lt;&lt;&lt;c</div>);`)
54+
).toBe('html`<div>${"a<b<<<c"}</div>`;');
55+
});
5056
});
5157

5258
describe('options.html = true', () => {

0 commit comments

Comments
 (0)