Skip to content

Commit

Permalink
Merge pull request #496 from securityMB/main
Browse files Browse the repository at this point in the history
Create a polyfill for __lookupGetter__ to make IE10 happy
  • Loading branch information
cure53 authored Dec 17, 2020
2 parents 8e29990 + 9dd47cb commit 77e740e
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 46 deletions.
38 changes: 30 additions & 8 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.

38 changes: 30 additions & 8 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.

38 changes: 30 additions & 8 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.

17 changes: 5 additions & 12 deletions src/purify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
stringTrim,
regExpTest,
typeErrorCreate,
unapply,
__lookupGetter__,
lookupGetter,
} from './utils';

const getGlobal = () => (typeof window === 'undefined' ? null : window);
Expand Down Expand Up @@ -109,16 +108,10 @@ function createDOMPurify(window = getGlobal()) {

const ElementPrototype = Element.prototype;

const cloneNode = unapply(ElementPrototype.cloneNode);
const getNextSibling = unapply(
__lookupGetter__(ElementPrototype, 'nextSibling')
);
const getChildNodes = unapply(
__lookupGetter__(ElementPrototype, 'childNodes')
);
const getParentNode = unapply(
__lookupGetter__(ElementPrototype, 'parentNode')
);
const cloneNode = lookupGetter(ElementPrototype, 'cloneNode');
const getNextSibling = lookupGetter(ElementPrototype, 'nextSibling');
const getChildNodes = lookupGetter(ElementPrototype, 'childNodes');
const getParentNode = lookupGetter(ElementPrototype, 'parentNode');

// As per issue #47, the web-components registry is inherited by a
// new document created via createHTMLDocument. As per the spec
Expand Down
38 changes: 33 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const { hasOwnProperty, setPrototypeOf, isFrozen, getPrototypeOf } = Object;
const {
hasOwnProperty,
setPrototypeOf,
isFrozen,
getPrototypeOf,
getOwnPropertyDescriptor,
} = Object;

let { freeze, seal, create } = Object; // eslint-disable-line import/no-mutable-exports
let { apply, construct } = typeof Reflect !== 'undefined' && Reflect;
Expand Down Expand Up @@ -43,9 +49,6 @@ const regExpTest = unapply(RegExp.prototype.test);

const typeErrorCreate = unconstruct(TypeError);

/* eslint-disable-next-line no-use-extend-native/no-use-extend-native */
const __lookupGetter__ = unapply(Object.prototype.__lookupGetter__);

export function unapply(func) {
return (thisArg, ...args) => apply(func, thisArg, args);
}
Expand Down Expand Up @@ -98,6 +101,29 @@ export function clone(object) {
return newObject;
}

/* IE10 doesn't support __lookupGetter__ so lets'
* simulate it. It also automatically checks
* if the prop is function or getter and behaves
* accordingly. */
function lookupGetter(object, prop) {
while (object !== null) {
const desc = getOwnPropertyDescriptor(object, prop);
if (desc) {
if (desc.get) {
return unapply(desc.get);
}

if (typeof desc.value === 'function') {
return unapply(desc.value);
}
}

object = getPrototypeOf(object);
}

return null;
}

export {
// Array
arrayForEach,
Expand All @@ -108,11 +134,11 @@ export {
// Object
freeze,
getPrototypeOf,
getOwnPropertyDescriptor,
hasOwnProperty,
isFrozen,
setPrototypeOf,
seal,
__lookupGetter__,
// RegExp
regExpTest,
// String
Expand All @@ -123,4 +149,6 @@ export {
stringTrim,
// Errors
typeErrorCreate,
// Other
lookupGetter,
};

0 comments on commit 77e740e

Please sign in to comment.