Skip to content

Commit f0b971c

Browse files
matthewheckcwelch5
authored andcommitted
handle null values for tags (and fix eslinting error) (#165)
1 parent aff3858 commit f0b971c

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Helmet.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const getTagsFromPropsList = (tagName, primaryAttributes, propsList) => {
111111
}
112112
}
113113

114-
if (!primaryAttributeKey) {
114+
if (!primaryAttributeKey || !tag[primaryAttributeKey]) {
115115
return false;
116116
}
117117

src/test/HelmetTest.js

+27
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,33 @@ describe("Helmet", () => {
997997
expect(secondTag.getAttribute("href")).to.equal("http://localhost/helmet/innercomponent");
998998
expect(secondTag.outerHTML).to.equal(`<link rel="canonical" href="http://localhost/helmet/innercomponent" ${HELMET_ATTRIBUTE}="true">`);
999999
});
1000+
1001+
it("will remove tags with null values", () => {
1002+
ReactDOM.render(
1003+
<Helmet
1004+
link={[
1005+
{"rel": "icon", "sizes": "192x192", "href": null},
1006+
{"rel": "canonical", "href": "http://localhost/helmet/component"}
1007+
]}
1008+
/>,
1009+
container
1010+
);
1011+
1012+
const tagNodes = headElement.querySelectorAll(`link[${HELMET_ATTRIBUTE}]`);
1013+
const existingTags = Array.prototype.slice.call(tagNodes);
1014+
const firstTag = existingTags[0];
1015+
1016+
expect(existingTags).to.not.equal(undefined);
1017+
expect(existingTags.length).to.be.equal(1);
1018+
1019+
expect(existingTags)
1020+
.to.have.deep.property("[0]")
1021+
.that.is.an.instanceof(Element);
1022+
expect(firstTag).to.have.property("getAttribute");
1023+
expect(firstTag.getAttribute("rel")).to.equal("canonical");
1024+
expect(firstTag.getAttribute("href")).to.equal("http://localhost/helmet/component");
1025+
expect(firstTag.outerHTML).to.equal(`<link rel="canonical" href="http://localhost/helmet/component" ${HELMET_ATTRIBUTE}="true">`);
1026+
});
10001027
});
10011028

10021029
describe("script tags", () => {

0 commit comments

Comments
 (0)