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 #344 rendering body style attribute value as [object Object]. #400

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
},
"dependencies": {
"deep-equal": "^1.0.1",
"lodash.kebabcase": "^4.1.1",
"object-assign": "^4.1.1",
"react-side-effect": "^1.1.0",
"prop-types": "^15.5.4"
"prop-types": "^15.5.4",
"react-side-effect": "^1.1.0"
},
"devDependencies": {
"babel-cli": "^6.24.0",
Expand Down
12 changes: 10 additions & 2 deletions src/Helmet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
handleClientStateChange,
mapStateOnServer,
reducePropsToState,
warn
warn,
getBodyStyleAttributeValue
} from "./HelmetUtils.js";
import {TAG_NAMES, VALID_TAG_NAMES} from "./HelmetConstants.js";

Expand Down Expand Up @@ -149,9 +150,16 @@ const Helmet = Component =>
};

case TAG_NAMES.BODY:
if (newChildProps.style) {
newChildProps.style = getBodyStyleAttributeValue(
newChildProps.style
);
}
return {
...newProps,
bodyAttributes: {...newChildProps}
bodyAttributes: {
...newChildProps
}
};

case TAG_NAMES.HTML:
Expand Down
12 changes: 12 additions & 0 deletions src/HelmetUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from "react";
import objectAssign from "object-assign";
import kebabCase from "lodash.kebabcase";

import {
ATTRIBUTE_NAMES,
HELMET_ATTRIBUTE,
Expand Down Expand Up @@ -640,9 +642,19 @@ const mapStateOnServer = ({
title: getMethodsForTag(TAG_NAMES.TITLE, {title, titleAttributes}, encode)
});

const getBodyStyleAttributeValue = (style: ?any) => {
if (typeof style === "object") {
return Object.keys(style)
.map(key => `${kebabCase(key)}: ${style[key]}`)
.join(";");
}
return style;
};

export {convertReactPropstoHtmlAttributes};
export {handleClientStateChange};
export {mapStateOnServer};
export {reducePropsToState};
export {requestAnimationFrame};
export {warn};
export {getBodyStyleAttributeValue};
41 changes: 41 additions & 0 deletions test/HelmetDeclarativeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,47 @@ describe("Helmet - Declarative API", () => {
});
});

it("body style attribute as string", done => {
ReactDOM.render(
<Helmet>
<body style="color: red" />
</Helmet>,
container
);

requestAnimationFrame(() => {
const body = document.querySelector("body");
const styleAttributeValue = body.getAttribute("style");
expect(styleAttributeValue).to.be.a("string");
expect(styleAttributeValue).to.equal("color: red");
done();
});
});

it("body style attribute as object is translated to a string", done => {
ReactDOM.render(
<Helmet>
<body style={{color: "red"}} />
</Helmet>,
container
);

requestAnimationFrame(() => {
const body = document.querySelector("body");
const styleAttributeValue = body.getAttribute("style");

// normal behaviour
expect(styleAttributeValue).to.be.a("string");

// old behaviour
expect(styleAttributeValue).to.not.equal("[object Object]");

// new behaviour
expect(styleAttributeValue).to.equal("color: red");
done();
});
});

it("sets attributes based on the deepest nested component", done => {
ReactDOM.render(
<div>
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3248,6 +3248,10 @@ lodash.isarray@^3.0.0:
version "3.0.4"
resolved "https://registry.yarnpkg.com/lodash.isarray/-/lodash.isarray-3.0.4.tgz#79e4eb88c36a8122af86f844aa9bcd851b5fbb55"

lodash.kebabcase@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.kebabcase/-/lodash.kebabcase-4.1.1.tgz#8489b1cb0d29ff88195cceca448ff6d6cc295c36"

lodash.keys@^3.0.0, lodash.keys@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-3.1.2.tgz#4dbc0472b156be50a0b286855d1bd0b0c656098a"
Expand Down