Browse Source

Patch the DOM to fix Google Translate (#1148)

* Fix Google Translate

* Tweaks
main
Dan Abramov 7 years ago
committed by GitHub
parent
commit
1ee1c3085f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/layouts/index.js
  2. 45
      src/utils/patchDOMForGoogleTranslate.js

3
src/layouts/index.js

@ -10,6 +10,7 @@ import 'array-from';
import 'string.prototype.includes';
import 'string.prototype.repeat';
import patchDOMForGoogleTranslate from 'utils/patchDOMForGoogleTranslate';
import React, {Component} from 'react';
import Flex from 'components/Flex';
import Footer from 'components/LayoutFooter';
@ -22,6 +23,8 @@ import 'glamor/reset';
import 'css/reset.css';
import 'css/algolia.css';
patchDOMForGoogleTranslate();
type Props = {
children: Function,
location: Location,

45
src/utils/patchDOMForGoogleTranslate.js

@ -0,0 +1,45 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* @emails react-core
* @flow
*/
// This is not pretty.
// See https://github.com/facebook/react/issues/11538#issuecomment-417504600
// We need this because we don't even offer official translations.
// https://github.com/facebook/react/issues/12460
export default function patchDOMForGoogleTranslate() {
const originalRemoveChild = Node.prototype.removeChild;
// $FlowFixMe Intentionally monkepatching.
Node.prototype.removeChild = function(child) {
if (child.parentNode !== this) {
if (typeof console !== 'undefined') {
console.error(
'Cannot remove a child from a different parent',
child,
this,
);
}
return child;
}
return originalRemoveChild.apply(this, arguments);
};
const originalInsertBefore = Node.prototype.insertBefore;
// $FlowFixMe Intentionally monkepatching.
Node.prototype.insertBefore = function(newNode, referenceNode) {
if (referenceNode && referenceNode.parentNode !== this) {
if (typeof console !== 'undefined') {
console.error(
'Cannot insert before a reference node from a different parent',
referenceNode,
this,
);
}
return newNode;
}
return originalInsertBefore.apply(this, arguments);
};
}
Loading…
Cancel
Save