Skip to content

Commit

Permalink
Fix tag converting behavior to match with react-i18next
Browse files Browse the repository at this point in the history
  • Loading branch information
yoo2001818 committed Oct 24, 2022
1 parent 7e604e7 commit a8880f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/lexers/jsx-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}>`
: `<${elementName}/>`
default:
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions test/lexers/jsx-lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<Trans>a<strong></strong>b</Trans>'
assert.equal(Lexer.extract(content)[0].defaultValue, 'a<strong/>b')
done()
})

it('ignores tagName when children is not a text when transSupportBasicHtmlNodes is true', (done) => {
const Lexer = new JsxLexer({ transSupportBasicHtmlNodes: true })
const content = '<Trans><p>ab</p><p>c<br />d</p><p>{"e"}f</p></Trans>'
assert.equal(
Lexer.extract(content)[0].defaultValue,
'a<strong></strong>b'
'<p>ab</p><1>c<br/>d</1><2>ef</2>'
)
done()
})
Expand Down

0 comments on commit a8880f7

Please sign in to comment.