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(purify): fix _createIterator #850

Merged
merged 5 commits into from
Aug 11, 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
23 changes: 12 additions & 11 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.

23 changes: 12 additions & 11 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.

23 changes: 12 additions & 11 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.

24 changes: 12 additions & 12 deletions src/purify.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,19 +900,18 @@ function createDOMPurify(window = getGlobal()) {
};

/**
* _createIterator
* Creates a NodeIterator object that you can use to traverse filtered lists of nodes or elements in a document.
*
* @param {Document} root document/fragment to create iterator for
* @return {Iterator} iterator instance
* @param {Node} root The root element or node to start traversing on.
* @return {NodeIterator} The created NodeIterator
*/
const _createIterator = function (root) {
const _createNodeIterator = function (root) {
Comment on lines +903 to +908
Copy link
Contributor Author

@ssi02014 ssi02014 Aug 10, 2023

Choose a reason for hiding this comment

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

https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts#L7160
I've modified the jsdoc with reference to that documentation.

Also, the naming seems clearer for _createNodeIterator than _createIterator. (Returns a NodeIterator.)
What do you think of this fix? 🙏

return createNodeIterator.call(
root.ownerDocument || root,
root,
// eslint-disable-next-line no-bitwise
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_TEXT,
null,
false
null
Comment on lines -915 to +914
Copy link
Contributor Author

@ssi02014 ssi02014 Aug 10, 2023

Choose a reason for hiding this comment

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

I removed that argument. ("false")

);
};

Expand Down Expand Up @@ -1010,7 +1009,7 @@ function createDOMPurify(window = getGlobal()) {
/* Remove element if anything forbids its presence */
if (!ALLOWED_TAGS[tagName] || FORBID_TAGS[tagName]) {
/* Check if we have a custom element to handle */
if (!FORBID_TAGS[tagName] && _basicCustomElementTest(tagName)) {
if (!FORBID_TAGS[tagName] && _isBasicCustomElement(tagName)) {
if (
CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof RegExp &&
regExpTest(CUSTOM_ELEMENT_HANDLING.tagNameCheck, tagName)
Expand Down Expand Up @@ -1122,7 +1121,7 @@ function createDOMPurify(window = getGlobal()) {
// First condition does a very basic check if a) it's basically a valid custom element tagname AND
// b) if the tagName passes whatever the user has configured for CUSTOM_ELEMENT_HANDLING.tagNameCheck
// and c) if the attribute name passes whatever the user has configured for CUSTOM_ELEMENT_HANDLING.attributeNameCheck
(_basicCustomElementTest(lcTag) &&
(_isBasicCustomElement(lcTag) &&
((CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof RegExp &&
regExpTest(CUSTOM_ELEMENT_HANDLING.tagNameCheck, lcTag)) ||
(CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof Function &&
Expand Down Expand Up @@ -1183,13 +1182,14 @@ function createDOMPurify(window = getGlobal()) {
};

/**
* _basicCustomElementCheck
* _isBasicCustomElement
* checks if at least one dash is included in tagName, and it's not the first char
* for more sophisticated checking see https://github.com/sindresorhus/validate-element-name
*
* @param {string} tagName name of the tag of the node to sanitize
* @returns {boolean} Returns true if the tag name meets the basic criteria for a custom element, otherwise false.
*/
const _basicCustomElementTest = function (tagName) {
const _isBasicCustomElement = function (tagName) {
Copy link
Contributor Author

@ssi02014 ssi02014 Aug 10, 2023

Choose a reason for hiding this comment

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

The function name in the jsdoc was different from the actual function name, so I tried to modify it, and finally changed it to _isBasicCustomElement with the is prefix because it returns a boolean.

jsdoc: _basicCustomElementCheck
actual: _basicCustomElementTest

return tagName.indexOf('-') > 0;
};

Expand Down Expand Up @@ -1331,7 +1331,7 @@ function createDOMPurify(window = getGlobal()) {
*/
const _sanitizeShadowDOM = function (fragment) {
let shadowNode = null;
const shadowIterator = _createIterator(fragment);
const shadowIterator = _createNodeIterator(fragment);

/* Execute a hook if present */
_executeHook('beforeSanitizeShadowDOM', fragment, null);
Expand Down Expand Up @@ -1462,7 +1462,7 @@ function createDOMPurify(window = getGlobal()) {
}

/* Get node iterator */
const nodeIterator = _createIterator(IN_PLACE ? dirty : body);
const nodeIterator = _createNodeIterator(IN_PLACE ? dirty : body);

/* Now start iterating over the created document */
while ((currentNode = nodeIterator.nextNode())) {
Expand Down
Loading