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 update attributes #369

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 9 additions & 15 deletions src/HelmetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,11 @@ const updateAttributes = (tagName, attributes) => {
}

const helmetAttributeString = elementTag.getAttribute(HELMET_ATTRIBUTE);
const helmetAttributes = helmetAttributeString
const attributesToRemove = helmetAttributeString
? helmetAttributeString.split(",")
: [];
const attributesToRemove = [].concat(helmetAttributes);
const attributeKeys = Object.keys(attributes);

const attributeKeys = Object.keys(attributes);
for (let i = 0; i < attributeKeys.length; i++) {
const attribute = attributeKeys[i];
const value = attributes[attribute] || "";
Expand All @@ -393,26 +392,21 @@ const updateAttributes = (tagName, attributes) => {
elementTag.setAttribute(attribute, value);
}

if (helmetAttributes.indexOf(attribute) === -1) {
helmetAttributes.push(attribute);
}

const indexToSave = attributesToRemove.indexOf(attribute);
if (indexToSave !== -1) {
attributesToRemove.splice(indexToSave, 1);
const indexToRemove = attributesToRemove.indexOf(attribute);
if (indexToRemove !== -1) {
attributesToRemove.splice(indexToRemove, 1);
}
}

for (let i = attributesToRemove.length - 1; i >= 0; i--) {
elementTag.removeAttribute(attributesToRemove[i]);
}

if (helmetAttributes.length === attributesToRemove.length) {
const newHelmetAttributeString = attributeKeys.join(",");
if (newHelmetAttributeString === "") {
elementTag.removeAttribute(HELMET_ATTRIBUTE);
} else if (
elementTag.getAttribute(HELMET_ATTRIBUTE) !== attributeKeys.join(",")
) {
elementTag.setAttribute(HELMET_ATTRIBUTE, attributeKeys.join(","));
} else if (helmetAttributeString !== newHelmetAttributeString) {
elementTag.setAttribute(HELMET_ATTRIBUTE, newHelmetAttributeString);
}
};

Expand Down