Skip to content

Commit 1b09f31

Browse files
authored
Merge pull request #1085 from michalnieruchalski-tiugo/remove_attribute_when_necessary
Remove attribute only when necessary
2 parents f14c22f + 93741d0 commit 1b09f31

File tree

11 files changed

+123
-104
lines changed

11 files changed

+123
-104
lines changed

dist/purify.cjs.js

Lines changed: 20 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.cjs.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.es.mjs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,8 @@ function createDOMPurify() {
10561056
value: attrValue
10571057
} = attr;
10581058
const lcName = transformCaseFunc(name);
1059-
let value = name === 'value' ? attrValue : stringTrim(attrValue);
1059+
const initValue = attrValue;
1060+
let value = name === 'value' ? initValue : stringTrim(initValue);
10601061
/* Execute a hook if present */
10611062
hookEvent.attrName = lcName;
10621063
hookEvent.attrValue = value;
@@ -1082,10 +1083,9 @@ function createDOMPurify() {
10821083
if (hookEvent.forceKeepAttr) {
10831084
continue;
10841085
}
1085-
/* Remove attribute */
1086-
_removeAttribute(name, currentNode);
10871086
/* Did the hooks approve of the attribute? */
10881087
if (!hookEvent.keepAttr) {
1088+
_removeAttribute(name, currentNode);
10891089
continue;
10901090
}
10911091
/* Work around a security issue in jQuery 3.0 */
@@ -1102,6 +1102,7 @@ function createDOMPurify() {
11021102
/* Is `value` valid for this attribute? */
11031103
const lcTag = transformCaseFunc(currentNode.nodeName);
11041104
if (!_isValidAttribute(lcTag, lcName, value)) {
1105+
_removeAttribute(name, currentNode);
11051106
continue;
11061107
}
11071108
/* Handle attributes that require Trusted Types */
@@ -1122,19 +1123,23 @@ function createDOMPurify() {
11221123
}
11231124
}
11241125
/* Handle invalid data-* attribute set by try-catching it */
1125-
try {
1126-
if (namespaceURI) {
1127-
currentNode.setAttributeNS(namespaceURI, name, value);
1128-
} else {
1129-
/* Fallback to setAttribute() for browser-unrecognized namespaces e.g. "x-schema". */
1130-
currentNode.setAttribute(name, value);
1131-
}
1132-
if (_isClobbered(currentNode)) {
1133-
_forceRemove(currentNode);
1134-
} else {
1135-
arrayPop(DOMPurify.removed);
1126+
if (value !== initValue) {
1127+
try {
1128+
if (namespaceURI) {
1129+
currentNode.setAttributeNS(namespaceURI, name, value);
1130+
} else {
1131+
/* Fallback to setAttribute() for browser-unrecognized namespaces e.g. "x-schema". */
1132+
currentNode.setAttribute(name, value);
1133+
}
1134+
if (_isClobbered(currentNode)) {
1135+
_forceRemove(currentNode);
1136+
} else {
1137+
arrayPop(DOMPurify.removed);
1138+
}
1139+
} catch (_) {
1140+
_removeAttribute(name, currentNode);
11361141
}
1137-
} catch (_) {}
1142+
}
11381143
}
11391144
/* Execute a hook if present */
11401145
_executeHooks(hooks.afterSanitizeAttributes, currentNode, null);

dist/purify.es.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.js

Lines changed: 20 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/purify.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,8 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
12961296
const { name, namespaceURI, value: attrValue } = attr;
12971297
const lcName = transformCaseFunc(name);
12981298

1299-
let value = name === 'value' ? attrValue : stringTrim(attrValue);
1299+
const initValue = attrValue;
1300+
let value = name === 'value' ? initValue : stringTrim(initValue);
13001301

13011302
/* Execute a hook if present */
13021303
hookEvent.attrName = lcName;
@@ -1328,11 +1329,9 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
13281329
continue;
13291330
}
13301331

1331-
/* Remove attribute */
1332-
_removeAttribute(name, currentNode);
1333-
13341332
/* Did the hooks approve of the attribute? */
13351333
if (!hookEvent.keepAttr) {
1334+
_removeAttribute(name, currentNode);
13361335
continue;
13371336
}
13381337

@@ -1352,6 +1351,7 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
13521351
/* Is `value` valid for this attribute? */
13531352
const lcTag = transformCaseFunc(currentNode.nodeName);
13541353
if (!_isValidAttribute(lcTag, lcName, value)) {
1354+
_removeAttribute(name, currentNode);
13551355
continue;
13561356
}
13571357

@@ -1383,20 +1383,24 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
13831383
}
13841384

13851385
/* Handle invalid data-* attribute set by try-catching it */
1386-
try {
1387-
if (namespaceURI) {
1388-
currentNode.setAttributeNS(namespaceURI, name, value);
1389-
} else {
1390-
/* Fallback to setAttribute() for browser-unrecognized namespaces e.g. "x-schema". */
1391-
currentNode.setAttribute(name, value);
1392-
}
1386+
if (value !== initValue) {
1387+
try {
1388+
if (namespaceURI) {
1389+
currentNode.setAttributeNS(namespaceURI, name, value);
1390+
} else {
1391+
/* Fallback to setAttribute() for browser-unrecognized namespaces e.g. "x-schema". */
1392+
currentNode.setAttribute(name, value);
1393+
}
13931394

1394-
if (_isClobbered(currentNode)) {
1395-
_forceRemove(currentNode);
1396-
} else {
1397-
arrayPop(DOMPurify.removed);
1395+
if (_isClobbered(currentNode)) {
1396+
_forceRemove(currentNode);
1397+
} else {
1398+
arrayPop(DOMPurify.removed);
1399+
}
1400+
} catch (_) {
1401+
_removeAttribute(name, currentNode);
13981402
}
1399-
} catch (_) {}
1403+
}
14001404
}
14011405

14021406
/* Execute a hook if present */

0 commit comments

Comments
 (0)