Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove unexpected comma in server-rendered title #289

Merged
merged 1 commit into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/HelmetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,13 @@ const handleClientStateChange = (newState) => {
});
};

const flattenArray = (possibleArray) => {
return Array.isArray(possibleArray) ? possibleArray.join("") : possibleArray;
};

const updateTitle = (title, attributes) => {
if (typeof title !== "undefined" && document.title !== title) {
document.title = Array.isArray(title) ? title.join("") : title;
document.title = flattenArray(title);
}

updateAttributes(TAG_NAMES.TITLE, attributes);
Expand Down Expand Up @@ -410,9 +414,10 @@ const generateElementAttributesAsString = (attributes) => Object.keys(attributes

const generateTitleAsString = (type, title, attributes, encode) => {
const attributeString = generateElementAttributesAsString(attributes);
const flattenedTitle = flattenArray(title);
return attributeString
? `<${type} ${HELMET_ATTRIBUTE}="true" ${attributeString}>${encodeSpecialCharacters(title, encode)}</${type}>`
: `<${type} ${HELMET_ATTRIBUTE}="true">${encodeSpecialCharacters(title, encode)}</${type}>`;
? `<${type} ${HELMET_ATTRIBUTE}="true" ${attributeString}>${encodeSpecialCharacters(flattenedTitle, encode)}</${type}>`
: `<${type} ${HELMET_ATTRIBUTE}="true">${encodeSpecialCharacters(flattenedTitle, encode)}</${type}>`;
};

const generateTagsAsString = (type, tags, encode) => tags.reduce((str, tag) => {
Expand Down
25 changes: 25 additions & 0 deletions test/HelmetDeclarativeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,7 @@ describe("Helmet - Declarative API", () => {
const stringifiedTitle = `<title ${HELMET_ATTRIBUTE}="true">Dangerous &lt;script&gt; include</title>`;
const unEncodedStringifiedTitle = `<title ${HELMET_ATTRIBUTE}="true">This is text and & and '.</title>`;
const stringifiedTitleWithItemprop = `<title ${HELMET_ATTRIBUTE}="true" itemprop="name">Title with Itemprop</title>`;
const stringifiedTitleWithTitleExpression = `<title ${HELMET_ATTRIBUTE}="true">Title: Some Great Title</title>`;
const stringifiedBaseTag = `<base ${HELMET_ATTRIBUTE}="true" target="_blank" href="http://localhost/"/>`;

const stringifiedMetaTags = [
Expand Down Expand Up @@ -2568,6 +2569,30 @@ describe("Helmet - Declarative API", () => {
.that.equals(stringifiedTitle);
});

it("renders title and allows children containing expressions", (done) => {
const someValue = "Some Great Title";

ReactDOM.render(
<Helmet>
<title>Title: {someValue}</title>
</Helmet>,
container
);

const head = Helmet.rewind();

expect(head.title).to.exist;
expect(head.title).to.respondTo("toString");

requestIdleCallback(() => {
expect(head.title.toString())
.to.be.a("string")
.that.equals(stringifiedTitleWithTitleExpression);

done();
});
});

it("renders title with itemprop name as string", () => {
ReactDOM.render(
<Helmet>
Expand Down