Skip to content

Commit

Permalink
fix based on @jamuhl suggestion in #1468 (#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Mar 17, 2022
1 parent 45c01ce commit f8e197c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/Trans.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export function nodesToString(children, i18nOptions) {

// do not use `React.Children.toArray`, will fail at object children
const childrenArray = getAsArray(children);
const keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
const keepArray =
i18nOptions.transSupportBasicHtmlNodes && i18nOptions.transKeepBasicHtmlNodesFor
? i18nOptions.transKeepBasicHtmlNodesFor
: [];

// e.g. lorem <br/> ipsum {{ messageCount, format }} dolor <strong>bold</strong> amet
childrenArray.forEach((child, childIndex) => {
Expand Down
20 changes: 16 additions & 4 deletions test/trans.nodeToString.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ describe('trans nodeToString', () => {
);
const expected = 'lorem <br/> ipsum';
const transKeepBasicHtmlNodesFor = ['br', 'strong', 'i'];
const actual = nodesToString(fragment.props.children, { transKeepBasicHtmlNodesFor });
const actual = nodesToString(fragment.props.children, {
transSupportBasicHtmlNodes: true,
transKeepBasicHtmlNodesFor,
});
expect(actual).toEqual(expected);
});

Expand All @@ -49,7 +52,10 @@ describe('trans nodeToString', () => {
);
const expected = 'lorem <strong>bold</strong> ipsum';
const transKeepBasicHtmlNodesFor = ['br', 'strong', 'i'];
const actual = nodesToString(fragment.props.children, { transKeepBasicHtmlNodesFor });
const actual = nodesToString(fragment.props.children, {
transSupportBasicHtmlNodes: true,
transKeepBasicHtmlNodesFor,
});
expect(actual).toEqual(expected);
});
});
Expand All @@ -63,7 +69,10 @@ describe('trans nodeToString', () => {
);
const expected = 'lorem <1></1> ipsum';
const transKeepBasicHtmlNodesFor = ['br', 'strong', 'i'];
const actual = nodesToString(fragment.props.children, { transKeepBasicHtmlNodesFor });
const actual = nodesToString(fragment.props.children, {
transSupportBasicHtmlNodes: true,
transKeepBasicHtmlNodesFor,
});
expect(actual).toEqual(expected);
});

Expand All @@ -75,7 +84,10 @@ describe('trans nodeToString', () => {
);
const expected = 'lorem <1>bold</1> ipsum';
const transKeepBasicHtmlNodesFor = ['br', 'strong', 'i'];
const actual = nodesToString(fragment.props.children, { transKeepBasicHtmlNodesFor });
const actual = nodesToString(fragment.props.children, {
transSupportBasicHtmlNodes: true,
transKeepBasicHtmlNodesFor,
});
expect(actual).toEqual(expected);
});
});
Expand Down
36 changes: 36 additions & 0 deletions test/trans.render.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,39 @@ describe('trans should allow escaped html', () => {
`);
});
});

it('transSupportBasicHtmlNodes: false should not keep the name of simple nodes', () => {
const cloneInst = i18n.cloneInstance({
react: { transSupportBasicHtmlNodes: false, defaultTransParent: 'div' },
});

const TestComponent = () => (
<Trans i18n={cloneInst}>
<p>Plain paragraph</p>
<p>
Paragraph with <em>hack</em>
</p>
<p className="hack">Paragraph with hack</p>
</Trans>
);

const { container } = render(<TestComponent />);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
<p>
Plain paragraph
</p>
<p>
Paragraph with
<em>
hack
</em>
</p>
<p
class="hack"
>
Paragraph with hack
</p>
</div>
`);
});

0 comments on commit f8e197c

Please sign in to comment.