Skip to content

Commit

Permalink
fix unescape logic for Trans component
Browse files Browse the repository at this point in the history
  • Loading branch information
nicegamer7 committed Aug 22, 2023
1 parent 5fdd776 commit 905082a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/lexers/jsx-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,20 @@ export default class JsxLexer extends JavascriptLexer {
const nodeAsString = this.nodeToString.call(this, node, sourceText)
const defaultsProp = getPropValue(tagNode, 'defaults')
let defaultValue = defaultsProp || nodeAsString
if (entry.shouldUnescape === true) {

// If `shouldUnescape` is not true, it means the value cannot contain HTML entities,
// so we need to unescape these entities now so that they can be properly rendered later
if (entry.shouldUnescape !== true) {
defaultValue = unescape(defaultValue)
}

if (defaultValue !== '') {
entry.defaultValue = defaultValue

if (!entry.key) {
entry.key = nodeAsString
// If there's no key, default to the unescaped default value to match react-i18next's behavior:
// https://github.com/i18next/react-i18next/blob/079938cc11a92e0519ad8a33a47d15595bee5636/src/TransWithoutContext.js#L335
entry.key = unescape(nodeAsString)
}
}

Expand Down
12 changes: 10 additions & 2 deletions test/lexers/jsx-lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,20 @@ describe('JsxLexer', () => {
done()
})

it('unescapes key when i18nKey is not provided', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans>I&apos;m Cielquan</Trans>'
assert.equal(Lexer.extract(content)[0].key, "I'm Cielquan")
done()
})

it('supports the shouldUnescape options', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans shouldUnescape>I&apros;m Cielquan</Trans>'
const content = '<Trans shouldUnescape>I&apos;m Cielquan</Trans>'
assert.equal(Lexer.extract(content)[0].key, "I'm Cielquan")
assert.equal(
Lexer.extract(content)[0].defaultValue,
'I&apros;m Cielquan'
'I&apos;m Cielquan'
)
done()
})
Expand Down

0 comments on commit 905082a

Please sign in to comment.