Skip to content

Commit

Permalink
Fix: guard type of attributes' value
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed May 15, 2022
1 parent 1f693d2 commit 30c543d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/modules/collapseAttributeWhitespace.es6
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export function onAttrs() {
const newAttrs = attrs;

Object.entries(attrs).forEach(([attrName, attrValue]) => {
if (typeof attrValue !== 'string') return;

if (attributesWithLists.has(attrName)) {
const newAttrValue = attrValue.replace(/\s+/g, ' ').trim();
newAttrs[attrName] = newAttrValue;
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/deduplicateAttributeValues.es6
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export function onAttrs() {
return;
}

if (typeof attrs[attrName] !== 'string') {
return;
}

const attrValues = attrs[attrName].split(/\s/);
const uniqeAttrValues = new Set();
const deduplicatedAttrValues = [];
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/normalizeAttributeValues.es6
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function onAttrs() {
|| caseInsensitiveAttributes[attrName].includes(node.tag)
)
) {
newAttrValue = attrValue.toLowerCase ? attrValue.toLowerCase() : attrValue;
newAttrValue = typeof attrValue.toLowerCase === 'function' ? attrValue.toLowerCase() : attrValue;
}

if (
Expand Down
6 changes: 4 additions & 2 deletions lib/modules/removeEmptyAttributes.es6
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ export function onAttrs() {
)
)
) {
if (attrValue === '' || (attrValue || '').match(/^\s+$/)) {
delete newAttrs[attrName];
if (typeof attrValue === 'string') {
if (attrValue === '' || attrValue.trim() === '') {
delete newAttrs[attrName];
}
}
}
});
Expand Down

0 comments on commit 30c543d

Please sign in to comment.