Open History to see the last commit
1// Browser page translation (Chrome, Edge) swaps text nodes for <font> wrappers React does not
2// know about, so a later removeChild or insertBefore on the original node throws NotFoundError
3// and takes down the whole tree. See facebook/react#11538.
4export function guardDomAgainstPageTranslation(): void {
5 const proto = Node.prototype;
6 const removeChild = proto.removeChild;
7 const insertBefore = proto.insertBefore;
8
9 proto.removeChild = function <T extends Node>(this: Node, child: T): T {
10 // Already detached by the translator: nothing left to remove.
11 if (child.parentNode !== this) return child;
12 return removeChild.call(this, child) as T;
13 };
14
15 proto.insertBefore = function <T extends Node>(this: Node, node: T, reference: Node | null): T {
16 // The anchor was replaced; append so the new node still shows.
17 const anchor = reference && reference.parentNode !== this ? null : reference;
18 return insertBefore.call(this, node, anchor) as T;
19 };
20}