Skip to content

Commit 9dd47cb

Browse files
committed
Create a polyfill for __lookupGetter__ to make IE10 happy
1 parent 8e29990 commit 9dd47cb

10 files changed

+133
-46
lines changed

dist/purify.cjs.js

+30-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.cjs.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.es.js

+30-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.es.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.js

+30-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/purify.js

+5-12
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import {
1515
stringTrim,
1616
regExpTest,
1717
typeErrorCreate,
18-
unapply,
19-
__lookupGetter__,
18+
lookupGetter,
2019
} from './utils';
2120

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

110109
const ElementPrototype = Element.prototype;
111110

112-
const cloneNode = unapply(ElementPrototype.cloneNode);
113-
const getNextSibling = unapply(
114-
__lookupGetter__(ElementPrototype, 'nextSibling')
115-
);
116-
const getChildNodes = unapply(
117-
__lookupGetter__(ElementPrototype, 'childNodes')
118-
);
119-
const getParentNode = unapply(
120-
__lookupGetter__(ElementPrototype, 'parentNode')
121-
);
111+
const cloneNode = lookupGetter(ElementPrototype, 'cloneNode');
112+
const getNextSibling = lookupGetter(ElementPrototype, 'nextSibling');
113+
const getChildNodes = lookupGetter(ElementPrototype, 'childNodes');
114+
const getParentNode = lookupGetter(ElementPrototype, 'parentNode');
122115

123116
// As per issue #47, the web-components registry is inherited by a
124117
// new document created via createHTMLDocument. As per the spec

src/utils.js

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
const { hasOwnProperty, setPrototypeOf, isFrozen, getPrototypeOf } = Object;
1+
const {
2+
hasOwnProperty,
3+
setPrototypeOf,
4+
isFrozen,
5+
getPrototypeOf,
6+
getOwnPropertyDescriptor,
7+
} = Object;
28

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

4450
const typeErrorCreate = unconstruct(TypeError);
4551

46-
/* eslint-disable-next-line no-use-extend-native/no-use-extend-native */
47-
const __lookupGetter__ = unapply(Object.prototype.__lookupGetter__);
48-
4952
export function unapply(func) {
5053
return (thisArg, ...args) => apply(func, thisArg, args);
5154
}
@@ -98,6 +101,29 @@ export function clone(object) {
98101
return newObject;
99102
}
100103

104+
/* IE10 doesn't support __lookupGetter__ so lets'
105+
* simulate it. It also automatically checks
106+
* if the prop is function or getter and behaves
107+
* accordingly. */
108+
function lookupGetter(object, prop) {
109+
while (object !== null) {
110+
const desc = getOwnPropertyDescriptor(object, prop);
111+
if (desc) {
112+
if (desc.get) {
113+
return unapply(desc.get);
114+
}
115+
116+
if (typeof desc.value === 'function') {
117+
return unapply(desc.value);
118+
}
119+
}
120+
121+
object = getPrototypeOf(object);
122+
}
123+
124+
return null;
125+
}
126+
101127
export {
102128
// Array
103129
arrayForEach,
@@ -108,11 +134,11 @@ export {
108134
// Object
109135
freeze,
110136
getPrototypeOf,
137+
getOwnPropertyDescriptor,
111138
hasOwnProperty,
112139
isFrozen,
113140
setPrototypeOf,
114141
seal,
115-
__lookupGetter__,
116142
// RegExp
117143
regExpTest,
118144
// String
@@ -123,4 +149,6 @@ export {
123149
stringTrim,
124150
// Errors
125151
typeErrorCreate,
152+
// Other
153+
lookupGetter,
126154
};

0 commit comments

Comments
 (0)