From 40bb9c3a7b18342ab1cab087fd243436a56385a8 Mon Sep 17 00:00:00 2001 From: Andrey Popp <8mayday@gmail.com> Date: Thu, 21 Nov 2013 12:59:45 +0400 Subject: [PATCH 1/2] Remove support for full page rendering --- src/core/ReactComponentBrowserEnvironment.js | 15 +- src/core/ReactMount.js | 15 +- .../__tests__/ReactRenderDocument-test.js | 273 ------------------ .../ReactRenderInvalidContainer-test.js | 56 ++++ src/core/getReactRootElementInContainer.js | 8 +- src/dom/Danger.js | 8 +- .../mutateHTMLNodeWithMarkup-test.js | 52 ---- src/dom/mutateHTMLNodeWithMarkup.js | 100 ------- 8 files changed, 60 insertions(+), 467 deletions(-) delete mode 100644 src/core/__tests__/ReactRenderDocument-test.js create mode 100644 src/core/__tests__/ReactRenderInvalidContainer-test.js delete mode 100644 src/dom/__tests__/mutateHTMLNodeWithMarkup-test.js delete mode 100644 src/dom/mutateHTMLNodeWithMarkup.js diff --git a/src/core/ReactComponentBrowserEnvironment.js b/src/core/ReactComponentBrowserEnvironment.js index 719f91a62d4c3..de9f8974eb914 100644 --- a/src/core/ReactComponentBrowserEnvironment.js +++ b/src/core/ReactComponentBrowserEnvironment.js @@ -27,11 +27,9 @@ var ReactReconcileTransaction = require('ReactReconcileTransaction'); var getReactRootElementInContainer = require('getReactRootElementInContainer'); var invariant = require('invariant'); -var mutateHTMLNodeWithMarkup = require('mutateHTMLNodeWithMarkup'); var ELEMENT_NODE_TYPE = 1; -var DOC_NODE_TYPE = 9; /** @@ -82,10 +80,7 @@ var ReactComponentBrowserEnvironment = { */ mountImageIntoNode: function(markup, container, shouldReuseMarkup) { invariant( - container && ( - container.nodeType === ELEMENT_NODE_TYPE || - container.nodeType === DOC_NODE_TYPE && ReactMount.allowFullPageRender - ), + container && container.nodeType === ELEMENT_NODE_TYPE, 'mountComponentIntoNode(...): Target container is not valid.' ); if (shouldReuseMarkup) { @@ -108,14 +103,6 @@ var ReactComponentBrowserEnvironment = { } } - // You can't naively set the innerHTML of the entire document. You need - // to mutate documentElement which requires doing some crazy tricks. See - // mutateHTMLNodeWithMarkup() - if (container.nodeType === DOC_NODE_TYPE) { - mutateHTMLNodeWithMarkup(container.documentElement, markup); - return; - } - // Asynchronously inject markup by ensuring that the container is not in // the document when settings its `innerHTML`. var parent = container.parentNode; diff --git a/src/core/ReactMount.js b/src/core/ReactMount.js index 5d636a0a95e1c..7f0dc9f79661c 100644 --- a/src/core/ReactMount.js +++ b/src/core/ReactMount.js @@ -33,7 +33,6 @@ var ATTR_NAME = 'data-reactid'; var nodeCache = {}; var ELEMENT_NODE_TYPE = 1; -var DOC_NODE_TYPE = 9; /** Mapping from reactRootID to React component instance. */ var instancesByReactRootID = {}; @@ -175,11 +174,6 @@ function purgeID(id) { * Inside of `container`, the first element rendered is the "reactRoot". */ var ReactMount = { - /** - * Safety guard to prevent accidentally rendering over the entire HTML tree. - */ - allowFullPageRender: false, - /** Time spent generating markup. */ totalInstantiationTime: 0, @@ -213,10 +207,7 @@ var ReactMount = { */ prepareEnvironmentForDOM: function(container) { invariant( - container && ( - container.nodeType === ELEMENT_NODE_TYPE || - container.nodeType === DOC_NODE_TYPE - ), + container && container.nodeType === ELEMENT_NODE_TYPE 'prepareEnvironmentForDOM(...): Target container is not a DOM element.' ); var doc = container.nodeType === ELEMENT_NODE_TYPE ? @@ -431,10 +422,6 @@ var ReactMount = { unmountComponentFromNode: function(instance, container) { instance.unmountComponent(); - if (container.nodeType === DOC_NODE_TYPE) { - container = container.documentElement; - } - // http://jsperf.com/emptying-a-node while (container.lastChild) { container.removeChild(container.lastChild); diff --git a/src/core/__tests__/ReactRenderDocument-test.js b/src/core/__tests__/ReactRenderDocument-test.js deleted file mode 100644 index dd5250811ca0c..0000000000000 --- a/src/core/__tests__/ReactRenderDocument-test.js +++ /dev/null @@ -1,273 +0,0 @@ -/** - * Copyright 2013 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * @jsx React.DOM - * @emails react-core - */ - -/*jslint evil: true */ - -"use strict"; - -var React; -var ReactMount; - -var getTestDocument; - -var testDocument; - -describe('rendering React components at document', function() { - beforeEach(function() { - require('mock-modules').dumpCache(); - - React = require('React'); - ReactMount = require('ReactMount'); - getTestDocument = require('getTestDocument'); - - testDocument = getTestDocument(); - }); - - it('should be able to get root component id for document node', function() { - if (!testDocument) { - // These tests are not applicable in jst, since jsdom is buggy. - return; - } - - var Root = React.createClass({ - render: function() { - return ( - -
-, container); - }).toThrow( - 'Invariant Violation: mountComponentIntoNode(...): Target container is ' + - 'not valid.' - ); - ReactMount.allowFullPageRender = true; - expect(function() { - React.renderComponent(
, container); - }).not.toThrow(); - }); - - it('should throw on full document render of non-html', function() { - if (!testDocument) { - // These tests are not applicable in jst, since jsdom is buggy. - return; - } - - var container = testDocument; - ReactMount.allowFullPageRender = true; - expect(function() { - React.renderComponent(
, container); - }).toThrow( - 'Invariant Violation: mutateHTMLNodeWithMarkup(): ' + - 'markup must start with
, container); + }).toThrow( + 'Invariant Violation: prepareEnvironmentForDOM(...): Target container is not a DOM element.' + ); + }); + +}); diff --git a/src/core/getReactRootElementInContainer.js b/src/core/getReactRootElementInContainer.js index f90f5178982f1..98afb12cd29d4 100644 --- a/src/core/getReactRootElementInContainer.js +++ b/src/core/getReactRootElementInContainer.js @@ -18,8 +18,6 @@ "use strict"; -var DOC_NODE_TYPE = 9; - /** * @param {DOMElement|DOMDocument} container DOM element that may contain * a React component @@ -30,11 +28,7 @@ function getReactRootElementInContainer(container) { return null; } - if (container.nodeType === DOC_NODE_TYPE) { - return container.documentElement; - } else { - return container.firstChild; - } + return container.firstChild; } module.exports = getReactRootElementInContainer; diff --git a/src/dom/Danger.js b/src/dom/Danger.js index 1310673ee366e..c36973a9808f7 100644 --- a/src/dom/Danger.js +++ b/src/dom/Danger.js @@ -27,7 +27,6 @@ var createNodesFromMarkup = require('createNodesFromMarkup'); var emptyFunction = require('emptyFunction'); var getMarkupWrap = require('getMarkupWrap'); var invariant = require('invariant'); -var mutateHTMLNodeWithMarkup = require('mutateHTMLNodeWithMarkup'); var OPEN_TAG_NAME_EXP = /^(<[^ \/>]+)/; var RESULT_INDEX_ATTR = 'data-danger-index'; @@ -171,12 +170,7 @@ var Danger = { 'immediately.' ); invariant(markup, 'dangerouslyReplaceNodeWithMarkup(...): Missing markup.'); - // createNodesFromMarkup() won't work if the markup is rooted by - // since it has special semantic meaning. So we use an alternatie strategy. - if (oldChild.tagName.toLowerCase() === 'html') { - mutateHTMLNodeWithMarkup(oldChild, markup); - return; - } + var newChild = createNodesFromMarkup(markup, emptyFunction)[0]; oldChild.parentNode.replaceChild(newChild, oldChild); } diff --git a/src/dom/__tests__/mutateHTMLNodeWithMarkup-test.js b/src/dom/__tests__/mutateHTMLNodeWithMarkup-test.js deleted file mode 100644 index 73c3ae120ae27..0000000000000 --- a/src/dom/__tests__/mutateHTMLNodeWithMarkup-test.js +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Copyright 2013 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * @jsx React.DOM - * @emails react-core - */ - -/*jslint evil: true */ - -"use strict"; - -var getTestDocument = require('getTestDocument'); - -var mutateHTMLNodeWithMarkup = require('mutateHTMLNodeWithMarkup'); - -describe('mutateHTMLNodeWithMarkup', function() { - it('should mutate the document html', function() { - var html = '