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

refac(purify): Refactoring purify #849

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
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
31 changes: 13 additions & 18 deletions dist/purify.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/purify.cjs.js.map

Large diffs are not rendered by default.

31 changes: 13 additions & 18 deletions dist/purify.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/purify.es.js.map

Large diffs are not rendered by default.

31 changes: 13 additions & 18 deletions dist/purify.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/purify.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/purify.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/purify.min.js.map

Large diffs are not rendered by default.

33 changes: 16 additions & 17 deletions src/purify.js
Original file line number Diff line number Diff line change
Expand Up @@ -1068,9 +1068,11 @@ function createDOMPurify(window = getGlobal()) {
if (SAFE_FOR_TEMPLATES && currentNode.nodeType === 3) {
/* Get the element's text content */
content = currentNode.textContent;
content = stringReplace(content, MUSTACHE_EXPR, ' ');
content = stringReplace(content, ERB_EXPR, ' ');
content = stringReplace(content, TMPLIT_EXPR, ' ');

arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], (expr) => {
content = stringReplace(content, expr, ' ');
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a love/hate thing. I think your thoughts will be important. 😂 @cure53

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it 🙂


if (currentNode.textContent !== content) {
arrayPush(DOMPurify.removed, { element: currentNode.cloneNode() });
currentNode.textContent = content;
Expand Down Expand Up @@ -1202,10 +1204,6 @@ function createDOMPurify(window = getGlobal()) {
* @param {Node} currentNode to sanitize
*/
const _sanitizeAttributes = function (currentNode) {
let attr = null;
let value = null;
let lcName = null;
let l = null;
Copy link
Contributor Author

@ssi02014 ssi02014 Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, moving the variables inside the "while" block would make the code more predictable.

/* Execute a hook if present */
_executeHook('beforeSanitizeAttributes', currentNode, null);

Expand All @@ -1222,14 +1220,15 @@ function createDOMPurify(window = getGlobal()) {
keepAttr: true,
allowedAttributes: ALLOWED_ATTR,
};
l = attributes.length;
let l = attributes.length;

/* Go backwards over all attributes; safely remove bad ones */
while (l--) {
attr = attributes[l];
const attr = attributes[l];
const { name, namespaceURI, value: attrValue } = attr;
value = name === 'value' ? attrValue : stringTrim(attrValue);
lcName = transformCaseFunc(name);
const lcName = transformCaseFunc(name);

let value = name === 'value' ? attrValue : stringTrim(attrValue);

/* Execute a hook if present */
hookEvent.attrName = lcName;
Expand Down Expand Up @@ -1259,9 +1258,9 @@ function createDOMPurify(window = getGlobal()) {

/* Sanitize attribute content to be template-safe */
if (SAFE_FOR_TEMPLATES) {
value = stringReplace(value, MUSTACHE_EXPR, ' ');
value = stringReplace(value, ERB_EXPR, ' ');
value = stringReplace(value, TMPLIT_EXPR, ' ');
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], (expr) => {
value = stringReplace(value, expr, ' ');
});
}

/* Is `value` valid for this attribute? */
Expand Down Expand Up @@ -1530,9 +1529,9 @@ function createDOMPurify(window = getGlobal()) {

/* Sanitize final string template-safe */
if (SAFE_FOR_TEMPLATES) {
serializedHTML = stringReplace(serializedHTML, MUSTACHE_EXPR, ' ');
serializedHTML = stringReplace(serializedHTML, ERB_EXPR, ' ');
serializedHTML = stringReplace(serializedHTML, TMPLIT_EXPR, ' ');
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], (expr) => {
serializedHTML = stringReplace(serializedHTML, expr, ' ');
});
}

return trustedTypesPolicy && RETURN_TRUSTED_TYPE
Expand Down
Loading