diff --git a/src/lexers/jsx-lexer.js b/src/lexers/jsx-lexer.js index eba68964..ffdf91f1 100644 --- a/src/lexers/jsx-lexer.js +++ b/src/lexers/jsx-lexer.js @@ -165,12 +165,14 @@ 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} />` + : `<${elementName}/>` default: throw new Error('Unknown parsed content: ' + child.type) } @@ -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 41cf7744..70bfc5fb 100644 --- a/test/lexers/jsx-lexer.test.js +++ b/test/lexers/jsx-lexer.test.js @@ -366,16 +366,23 @@ describe('JsxLexer', () => { it('keeps self-closing tags untouched when transSupportBasicHtmlNodes is true', (done) => { const Lexer = new JsxLexer({ transSupportBasicHtmlNodes: true }) const content = 'a
b
' - assert.equal(Lexer.extract(content)[0].defaultValue, 'a
b') + assert.equal(Lexer.extract(content)[0].defaultValue, 'a
b') 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() })