Skip to content

Commit

Permalink
Merge pull request #948 from ssi02014/refac/purify
Browse files Browse the repository at this point in the history
refac: refactoring nodeType by adding a NODE_TYPE object
  • Loading branch information
cure53 committed May 5, 2024
2 parents abb21f8 + c68783e commit 0b63a98
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 33 deletions.
32 changes: 25 additions & 7 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.

32 changes: 25 additions & 7 deletions dist/purify.es.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ var EXPRESSIONS = /*#__PURE__*/Object.freeze({
CUSTOM_ELEMENT: CUSTOM_ELEMENT
});

// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
const NODE_TYPE = {
element: 1,
attribute: 2,
text: 3,
cdataSection: 4,
entityReference: 5,
// Deprecated
entityNode: 6,
// Deprecated
progressingInstruction: 7,
comment: 8,
document: 9,
documentType: 10,
documentFragment: 11,
notation: 12 // Deprecated
};

const getGlobal = function getGlobal() {
return typeof window === 'undefined' ? null : window;
};
Expand Down Expand Up @@ -289,7 +307,7 @@ function createDOMPurify() {
* Empty if nothing was removed.
*/
DOMPurify.removed = [];
if (!window || !window.document || window.document.nodeType !== 9) {
if (!window || !window.document || window.document.nodeType !== NODE_TYPE.document) {
// Not running in a browser, provide a factory function
// so that you can pass your own Window
DOMPurify.isSupported = false;
Expand Down Expand Up @@ -1000,13 +1018,13 @@ function createDOMPurify() {
}

/* Remove any ocurrence of processing instructions */
if (currentNode.nodeType === 7) {
if (currentNode.nodeType === NODE_TYPE.progressingInstruction) {
_forceRemove(currentNode);
return true;
}

/* Remove any kind of possibly harmful comments */
if (SAFE_FOR_XML && currentNode.nodeType === 8 && regExpTest(/<[/\w]/g, currentNode.data)) {
if (SAFE_FOR_XML && currentNode.nodeType === NODE_TYPE.comment && regExpTest(/<[/\w]/g, currentNode.data)) {
_forceRemove(currentNode);
return true;
}
Expand Down Expand Up @@ -1053,7 +1071,7 @@ function createDOMPurify() {
}

/* Sanitize element content to be template-safe */
if (SAFE_FOR_TEMPLATES && currentNode.nodeType === 3) {
if (SAFE_FOR_TEMPLATES && currentNode.nodeType === NODE_TYPE.text) {
/* Get the element's text content */
content = currentNode.textContent;
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], expr => {
Expand Down Expand Up @@ -1271,7 +1289,7 @@ function createDOMPurify() {
const parentNode = getParentNode(shadowNode);

/* Set the nesting depth of an element */
if (shadowNode.nodeType === 1) {
if (shadowNode.nodeType === NODE_TYPE.element) {
if (parentNode && parentNode.__depth) {
/*
We want the depth of the node in the original tree, which can
Expand Down Expand Up @@ -1366,7 +1384,7 @@ function createDOMPurify() {
elements being stripped by the parser */
body = _initDocument('<!---->');
importedNode = body.ownerDocument.importNode(dirty, true);
if (importedNode.nodeType === 1 && importedNode.nodeName === 'BODY') {
if (importedNode.nodeType === NODE_TYPE.element && importedNode.nodeName === 'BODY') {
/* Node is already a body, use as is */
body = importedNode;
} else if (importedNode.nodeName === 'HTML') {
Expand Down Expand Up @@ -1409,7 +1427,7 @@ function createDOMPurify() {
const parentNode = getParentNode(currentNode);

/* Set the nesting depth of an element */
if (currentNode.nodeType === 1) {
if (currentNode.nodeType === NODE_TYPE.element) {
if (parentNode && parentNode.__depth) {
/*
We want the depth of the node in the original tree, which can
Expand Down
2 changes: 1 addition & 1 deletion dist/purify.es.mjs.map

Large diffs are not rendered by default.

32 changes: 25 additions & 7 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.

37 changes: 30 additions & 7 deletions src/purify.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ import {
objectHasOwnProperty,
} from './utils.js';

// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
const NODE_TYPE = {
element: 1,
attribute: 2,
text: 3,
cdataSection: 4,
entityReference: 5, // Deprecated
entityNode: 6, // Deprecated
progressingInstruction: 7,
comment: 8,
document: 9,
documentType: 10,
documentFragment: 11,
notation: 12, // Deprecated
};

const getGlobal = function () {
return typeof window === 'undefined' ? null : window;
};
Expand Down Expand Up @@ -88,7 +104,11 @@ function createDOMPurify(window = getGlobal()) {
*/
DOMPurify.removed = [];

if (!window || !window.document || window.document.nodeType !== 9) {
if (
!window ||
!window.document ||
window.document.nodeType !== NODE_TYPE.document
) {
// Not running in a browser, provide a factory function
// so that you can pass your own Window
DOMPurify.isSupported = false;
Expand Down Expand Up @@ -1024,15 +1044,15 @@ function createDOMPurify(window = getGlobal()) {
}

/* Remove any ocurrence of processing instructions */
if (currentNode.nodeType === 7) {
if (currentNode.nodeType === NODE_TYPE.progressingInstruction) {
_forceRemove(currentNode);
return true;
}

/* Remove any kind of possibly harmful comments */
if (
SAFE_FOR_XML &&
currentNode.nodeType === 8 &&
currentNode.nodeType === NODE_TYPE.comment &&
regExpTest(/<[/\w]/g, currentNode.data)
) {
_forceRemove(currentNode);
Expand Down Expand Up @@ -1096,7 +1116,7 @@ function createDOMPurify(window = getGlobal()) {
}

/* Sanitize element content to be template-safe */
if (SAFE_FOR_TEMPLATES && currentNode.nodeType === 3) {
if (SAFE_FOR_TEMPLATES && currentNode.nodeType === NODE_TYPE.text) {
/* Get the element's text content */
content = currentNode.textContent;

Expand Down Expand Up @@ -1387,7 +1407,7 @@ function createDOMPurify(window = getGlobal()) {
const parentNode = getParentNode(shadowNode);

/* Set the nesting depth of an element */
if (shadowNode.nodeType === 1) {
if (shadowNode.nodeType === NODE_TYPE.element) {
if (parentNode && parentNode.__depth) {
/*
We want the depth of the node in the original tree, which can
Expand Down Expand Up @@ -1485,7 +1505,10 @@ function createDOMPurify(window = getGlobal()) {
elements being stripped by the parser */
body = _initDocument('<!---->');
importedNode = body.ownerDocument.importNode(dirty, true);
if (importedNode.nodeType === 1 && importedNode.nodeName === 'BODY') {
if (
importedNode.nodeType === NODE_TYPE.element &&
importedNode.nodeName === 'BODY'
) {
/* Node is already a body, use as is */
body = importedNode;
} else if (importedNode.nodeName === 'HTML') {
Expand Down Expand Up @@ -1535,7 +1558,7 @@ function createDOMPurify(window = getGlobal()) {
const parentNode = getParentNode(currentNode);

/* Set the nesting depth of an element */
if (currentNode.nodeType === 1) {
if (currentNode.nodeType === NODE_TYPE.element) {
if (parentNode && parentNode.__depth) {
/*
We want the depth of the node in the original tree, which can
Expand Down

0 comments on commit 0b63a98

Please sign in to comment.