Skip to content

Commit fa79005

Browse files
benjaurrbenja
andauthored
fix: Trans named tags with underscore (#1901)
Co-authored-by: benja <benja@fintual.com>
1 parent df41fb3 commit fa79005

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/TransWithoutContext.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ const escapeLiteralLessThan = (str, keepArray = [], knownComponentsMap = {}) =>
160160
let isValidTag = false;
161161

162162
// Check for closing tag: </number> or </name>
163-
const closingMatch = str.slice(i).match(/^<\/(\d+|[a-zA-Z][a-zA-Z0-9-]*)>/);
163+
const closingMatch = str.slice(i).match(/^<\/(\d+|[a-zA-Z][a-zA-Z0-9_-]*)>/);
164164
if (closingMatch) {
165165
const tagName = closingMatch[1];
166166
// Valid if it's a number or in our valid names list
@@ -179,7 +179,7 @@ const escapeLiteralLessThan = (str, keepArray = [], knownComponentsMap = {}) =>
179179
const openingMatch = str
180180
.slice(i)
181181
.match(
182-
/^<(\d+|[a-zA-Z][a-zA-Z0-9-]*)(\s+[\w-]+(?:=(?:"[^"]*"|'[^']*'|[^\s>]+))?)*\s*(\/)?>/,
182+
/^<(\d+|[a-zA-Z][a-zA-Z0-9_-]*)(\s+[\w-]+(?:=(?:"[^"]*"|'[^']*'|[^\s>]+))?)*\s*(\/)?>/,
183183
);
184184
if (openingMatch) {
185185
const tagName = openingMatch[1];

test/trans.render.object.spec.jsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,28 @@ describe('trans with hyphenated component names', () => {
416416
`);
417417
});
418418
});
419+
420+
describe('trans with underscore component names', () => {
421+
function TestComponent() {
422+
return (
423+
<Trans
424+
defaults="Click <my_button>here</my_button> to continue"
425+
components={{ my_button: <button type="button">dummy</button> }}
426+
/>
427+
);
428+
}
429+
it('should render underscore component names correctly', () => {
430+
const { container } = render(<TestComponent />);
431+
expect(container.firstChild).toMatchInlineSnapshot(`
432+
<div>
433+
Click
434+
<button
435+
type="button"
436+
>
437+
here
438+
</button>
439+
to continue
440+
</div>
441+
`);
442+
});
443+
});

0 commit comments

Comments
 (0)