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 34cdad7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
6 changes: 4 additions & 2 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 All @@ -82,7 +84,7 @@ export function onAttrs() {
if (
isEventHandler(attrName)
|| (
Object.hasOwnProperty.call(attributesWithSingleValue, attrName)
Object.prototype.hasOwnProperty.call(attributesWithSingleValue, attrName)
&& (
attributesWithSingleValue[attrName] === null
|| attributesWithSingleValue[attrName].includes(node.tag)
Expand All @@ -98,5 +100,5 @@ export function onAttrs() {
}

function minifySingleAttributeValue(value) {
return typeof value === 'string' ? String(value).trim() : value;
return typeof value === 'string' ? value.trim() : value;
}
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 34cdad7

Please sign in to comment.