Skip to content

Commit

Permalink
Merge pull request #68 from Pita/feature/singleSpaceFix
Browse files Browse the repository at this point in the history
Correctly remove all empty jsx text nodes, fixes #67
  • Loading branch information
gilbsgilbs authored Sep 23, 2019
2 parents 1997a1e + 7cbdb23 commit 7d9568c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
22 changes: 4 additions & 18 deletions src/extractors/transComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,24 +146,10 @@ function parseTransComponentKeyFromChildren(
let children = path.get('children');
let result = '';

// Remove trailing empty JSXText nodes. They should not be taken into
// account for the indices.
const firstChild = children[0];
if (
firstChild &&
firstChild.isJSXText() &&
firstChild.node.value.trim() === ''
) {
children = children.slice(1);
}
const lastChild = children[children.length - 1];
if (
lastChild &&
lastChild.isJSXText() &&
lastChild.node.value.trim() === ''
) {
children = children.slice(0, children.length - 1);
}
// Remove empty JSXText nodes. They should not be taken into account for the indices.
children = children.filter(child => {
return !(child.isJSXText() && child.node.value.trim() === '');
});

// Filter out empty containers. They do not affect indices.
children = children.filter(p => {
Expand Down
21 changes: 21 additions & 0 deletions tests/__fixtures__/testTransComponent/singleSpaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Trans } from "react-i18next";

<Trans i18nKey="key1">
I read the{" "}
<a
href="https://www.example.com/tos"
target="_blank"
rel="noopener noreferrer"
>
TOS
</a>{" "}
and the{" "}
<a
href="https://www.example.com/privacy"
target="_blank"
rel="noopener noreferrer"
>
Privacy Policy
</a>{" "}
and accept them.
</Trans>;
9 changes: 9 additions & 0 deletions tests/__fixtures__/testTransComponent/singleSpaces.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"description": "test that i18next can deal with single spaces correctly",
"pluginOptions": {
"useI18nextDefaultValue": true
},
"expectValues": {
"key1": "I read the <2>TOS</2> and the <6>Privacy Policy</6> and accept them."
}
}

0 comments on commit 7d9568c

Please sign in to comment.