From a8880f781333a75713045f4c807343bc8feef7a1 Mon Sep 17 00:00:00 2001 From: Duknam Yoo Date: Mon, 24 Oct 2022 19:08:35 +0900 Subject: [PATCH] Fix tag converting behavior to match with react-i18next --- src/lexers/jsx-lexer.js | 7 ++++--- test/lexers/jsx-lexer.test.js | 11 +++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/lexers/jsx-lexer.js b/src/lexers/jsx-lexer.js index 147a8bbe..ffdf91f1 100644 --- a/src/lexers/jsx-lexer.js +++ b/src/lexers/jsx-lexer.js @@ -165,10 +165,12 @@ export default class JsxLexer extends JavascriptLexer { const useTagName = child.isBasic && this.transSupportBasicHtmlNodes && - this.transKeepBasicHtmlNodesFor.includes(child.name) + this.transKeepBasicHtmlNodesFor.includes(child.name) && + child.children.length <= 1 && + child.children.every((v) => v.type === 'text') const elementName = useTagName ? child.name : index const childrenString = elemsToString(child.children) - return childrenString || !(useTagName && child.selfClosing) + return childrenString || !useTagName ? `<${elementName}>${childrenString}` : `<${elementName}/>` default: @@ -202,7 +204,6 @@ export default class JsxLexer extends JavascriptLexer { children: this.parseChildren(child.children, sourceText), name, isBasic, - selfClosing: child.kind === ts.SyntaxKind.JsxSelfClosingElement, } } else if (child.kind === ts.SyntaxKind.JsxExpression) { // strip empty expressions diff --git a/test/lexers/jsx-lexer.test.js b/test/lexers/jsx-lexer.test.js index 8519684f..70bfc5fb 100644 --- a/test/lexers/jsx-lexer.test.js +++ b/test/lexers/jsx-lexer.test.js @@ -370,12 +370,19 @@ describe('JsxLexer', () => { done() }) - it('keeps empty tag untouched when transSupportBasicHtmlNodes is true', (done) => { + it('converts empty tags to self-closing tags when transSupportBasicHtmlNodes is true', (done) => { const Lexer = new JsxLexer({ transSupportBasicHtmlNodes: true }) const content = 'ab' + assert.equal(Lexer.extract(content)[0].defaultValue, 'ab') + done() + }) + + it('ignores tagName when children is not a text when transSupportBasicHtmlNodes is true', (done) => { + const Lexer = new JsxLexer({ transSupportBasicHtmlNodes: true }) + const content = '

ab

c
d

{"e"}f

' assert.equal( Lexer.extract(content)[0].defaultValue, - 'ab' + '

ab

<1>c
d<2>ef' ) done() })