Skip to content

Commit

Permalink
FIX: titleTemplate bug with multiple title children components (nfl#351)
Browse files Browse the repository at this point in the history
Regexp replacement with a function will call .toString() on the
replacement candidate. That would include extra commas in page titles
where the <title> component had multiple children.
  • Loading branch information
joshgummersall authored and jamsea committed Feb 26, 2018
1 parent 86276f3 commit 6e65388
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/HelmetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ const getTitleFromPropsList = propsList => {

if (innermostTemplate && innermostTitle) {
// use function arg to avoid need to escape $ characters
return innermostTemplate.replace(/%s/g, () => innermostTitle);
return innermostTemplate.replace(
/%s/g,
() =>
Array.isArray(innermostTitle)
? innermostTitle.join("")
: innermostTitle
);
}

const innermostDefaultTitle = getInnermostProperty(
Expand Down
15 changes: 15 additions & 0 deletions test/HelmetDeclarativeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ describe("Helmet - Declarative API", () => {
});
});

it("properly handles title with children and titleTemplate", done => {
ReactDOM.render(
<Helmet titleTemplate={"This is an %s"}>
<title>{"extra"} + {"test"}</title>
</Helmet>,
container
);

requestAnimationFrame(() => {
expect(document.title).to.equal("This is an extra + test");

done();
});
});

it("does not encode all characters with HTML character entity equivalents", done => {
const chineseTitle = "膣膗 鍆錌雔";

Expand Down

0 comments on commit 6e65388

Please sign in to comment.