-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: catch Google Translate DOM errors
Monkey-patch `node.removeChild` and `node.insertBefore` to catch crashing errors generated by Google Translate. This should make projects usable in Chrome, in other languages, until the underlying bug is fixed. See facebook/react#11538 (comment)
- Loading branch information
1 parent
04a4bbe
commit b882de0
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Monkey-patch node.removeChild and node.insertBefore, | ||
to work around a crashing bug with Google Translate. | ||
https://github.com/facebook/react/issues/11538#issuecomment-417504600 | ||
*/ | ||
if (typeof Node === 'function' && Node.prototype) { | ||
const originalRemoveChild = Node.prototype.removeChild; | ||
Node.prototype.removeChild = function(child) { | ||
if (child.parentNode !== this) { | ||
if (console) { | ||
console.error('Cannot remove a child from a different parent', child, this); | ||
} | ||
return child; | ||
} | ||
return originalRemoveChild.apply(this, arguments); | ||
} | ||
|
||
const originalInsertBefore = Node.prototype.insertBefore; | ||
Node.prototype.insertBefore = function(newNode, referenceNode) { | ||
if (referenceNode && referenceNode.parentNode !== this) { | ||
if (console) { | ||
console.error('Cannot insert before a reference node from a different parent', referenceNode, this); | ||
} | ||
return newNode; | ||
} | ||
return originalInsertBefore.apply(this, arguments); | ||
} | ||
} |