react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / ReactComponentBrowserEnvironment.js
81155 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule ReactComponentBrowserEnvironment9*/1011/*jslint evil: true */1213"use strict";1415var ReactDOMIDOperations = require('ReactDOMIDOperations');16var ReactMarkupChecksum = require('ReactMarkupChecksum');17var ReactMount = require('ReactMount');18var ReactPerf = require('ReactPerf');19var ReactReconcileTransaction = require('ReactReconcileTransaction');2021var getReactRootElementInContainer = require('getReactRootElementInContainer');22var invariant = require('invariant');23var setInnerHTML = require('setInnerHTML');242526var ELEMENT_NODE_TYPE = 1;27var DOC_NODE_TYPE = 9;282930/**31* Abstracts away all functionality of `ReactComponent` requires knowledge of32* the browser context.33*/34var ReactComponentBrowserEnvironment = {35ReactReconcileTransaction: ReactReconcileTransaction,3637BackendIDOperations: ReactDOMIDOperations,3839/**40* If a particular environment requires that some resources be cleaned up,41* specify this in the injected Mixin. In the DOM, we would likely want to42* purge any cached node ID lookups.43*44* @private45*/46unmountIDFromEnvironment: function(rootNodeID) {47ReactMount.purgeID(rootNodeID);48},4950/**51* @param {string} markup Markup string to place into the DOM Element.52* @param {DOMElement} container DOM Element to insert markup into.53* @param {boolean} shouldReuseMarkup Should reuse the existing markup in the54* container if possible.55*/56mountImageIntoNode: ReactPerf.measure(57'ReactComponentBrowserEnvironment',58'mountImageIntoNode',59function(markup, container, shouldReuseMarkup) {60invariant(61container && (62container.nodeType === ELEMENT_NODE_TYPE ||63container.nodeType === DOC_NODE_TYPE64),65'mountComponentIntoNode(...): Target container is not valid.'66);6768if (shouldReuseMarkup) {69if (ReactMarkupChecksum.canReuseMarkup(70markup,71getReactRootElementInContainer(container))) {72return;73} else {74invariant(75container.nodeType !== DOC_NODE_TYPE,76'You\'re trying to render a component to the document using ' +77'server rendering but the checksum was invalid. This usually ' +78'means you rendered a different component type or props on ' +79'the client from the one on the server, or your render() ' +80'methods are impure. React cannot handle this case due to ' +81'cross-browser quirks by rendering at the document root. You ' +82'should look for environment dependent code in your components ' +83'and ensure the props are the same client and server side.'84);8586if (__DEV__) {87console.warn(88'React attempted to use reuse markup in a container but the ' +89'checksum was invalid. This generally means that you are ' +90'using server rendering and the markup generated on the ' +91'server was not what the client was expecting. React injected ' +92'new markup to compensate which works but you have lost many ' +93'of the benefits of server rendering. Instead, figure out ' +94'why the markup being generated is different on the client ' +95'or server.'96);97}98}99}100101invariant(102container.nodeType !== DOC_NODE_TYPE,103'You\'re trying to render a component to the document but ' +104'you didn\'t use server rendering. We can\'t do this ' +105'without using server rendering due to cross-browser quirks. ' +106'See renderComponentToString() for server rendering.'107);108109setInnerHTML(container, markup);110}111)112};113114module.exports = ReactComponentBrowserEnvironment;115116117