Skip to content

Commit

Permalink
fix: verify object prototype before define 'remove' property in it (#…
Browse files Browse the repository at this point in the history
…1385)

Before define 'remove' property in the item's prototype, we need to check whether item is undefined or not.
If we don't check item, it could lead to an error like:
``Cannot use 'in' operator to search for 'remove' in undefined ``
For example, window.DocumentType.prototype can be undefined in some rare environment.
  • Loading branch information
caok2709 authored Nov 3, 2021
1 parent d289517 commit 94d81fa
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/js/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ if (typeof Element !== 'undefined') {
// Polyfill for array remove
(() => {
function polyfill (item) {
if ('remove' in item) {
return
}
Object.defineProperty(item, 'remove', {
configurable: true,
enumerable: true,
writable: true,
value: function remove () {
if (this.parentNode !== undefined) { this.parentNode.removeChild(this) }
if (typeof item !== 'undefined') {
if ('remove' in item) {
return
}
})
Object.defineProperty(item, 'remove', {
configurable: true,
enumerable: true,
writable: true,
value: function remove () {
if (this.parentNode !== undefined) { this.parentNode.removeChild(this) }
}
})
}
}

if (typeof window.Element !== 'undefined') { polyfill(window.Element.prototype) }
Expand Down

0 comments on commit 94d81fa

Please sign in to comment.