react / react-0.13.3 / examples / basic-commonjs / node_modules / react / lib / DOMChildrenOperations.js
81143 views/**1* Copyright 2013-2015, 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 DOMChildrenOperations9* @typechecks static-only10*/1112'use strict';1314var Danger = require("./Danger");15var ReactMultiChildUpdateTypes = require("./ReactMultiChildUpdateTypes");1617var setTextContent = require("./setTextContent");18var invariant = require("./invariant");1920/**21* Inserts `childNode` as a child of `parentNode` at the `index`.22*23* @param {DOMElement} parentNode Parent node in which to insert.24* @param {DOMElement} childNode Child node to insert.25* @param {number} index Index at which to insert the child.26* @internal27*/28function insertChildAt(parentNode, childNode, index) {29// By exploiting arrays returning `undefined` for an undefined index, we can30// rely exclusively on `insertBefore(node, null)` instead of also using31// `appendChild(node)`. However, using `undefined` is not allowed by all32// browsers so we must replace it with `null`.33parentNode.insertBefore(34childNode,35parentNode.childNodes[index] || null36);37}3839/**40* Operations for updating with DOM children.41*/42var DOMChildrenOperations = {4344dangerouslyReplaceNodeWithMarkup: Danger.dangerouslyReplaceNodeWithMarkup,4546updateTextContent: setTextContent,4748/**49* Updates a component's children by processing a series of updates. The50* update configurations are each expected to have a `parentNode` property.51*52* @param {array<object>} updates List of update configurations.53* @param {array<string>} markupList List of markup strings.54* @internal55*/56processUpdates: function(updates, markupList) {57var update;58// Mapping from parent IDs to initial child orderings.59var initialChildren = null;60// List of children that will be moved or removed.61var updatedChildren = null;6263for (var i = 0; i < updates.length; i++) {64update = updates[i];65if (update.type === ReactMultiChildUpdateTypes.MOVE_EXISTING ||66update.type === ReactMultiChildUpdateTypes.REMOVE_NODE) {67var updatedIndex = update.fromIndex;68var updatedChild = update.parentNode.childNodes[updatedIndex];69var parentID = update.parentID;7071("production" !== process.env.NODE_ENV ? invariant(72updatedChild,73'processUpdates(): Unable to find child %s of element. This ' +74'probably means the DOM was unexpectedly mutated (e.g., by the ' +75'browser), usually due to forgetting a <tbody> when using tables, ' +76'nesting tags like <form>, <p>, or <a>, or using non-SVG elements ' +77'in an <svg> parent. Try inspecting the child nodes of the element ' +78'with React ID `%s`.',79updatedIndex,80parentID81) : invariant(updatedChild));8283initialChildren = initialChildren || {};84initialChildren[parentID] = initialChildren[parentID] || [];85initialChildren[parentID][updatedIndex] = updatedChild;8687updatedChildren = updatedChildren || [];88updatedChildren.push(updatedChild);89}90}9192var renderedMarkup = Danger.dangerouslyRenderMarkup(markupList);9394// Remove updated children first so that `toIndex` is consistent.95if (updatedChildren) {96for (var j = 0; j < updatedChildren.length; j++) {97updatedChildren[j].parentNode.removeChild(updatedChildren[j]);98}99}100101for (var k = 0; k < updates.length; k++) {102update = updates[k];103switch (update.type) {104case ReactMultiChildUpdateTypes.INSERT_MARKUP:105insertChildAt(106update.parentNode,107renderedMarkup[update.markupIndex],108update.toIndex109);110break;111case ReactMultiChildUpdateTypes.MOVE_EXISTING:112insertChildAt(113update.parentNode,114initialChildren[update.parentID][update.fromIndex],115update.toIndex116);117break;118case ReactMultiChildUpdateTypes.TEXT_CONTENT:119setTextContent(120update.parentNode,121update.textContent122);123break;124case ReactMultiChildUpdateTypes.REMOVE_NODE:125// Already removed by the for-loop above.126break;127}128}129}130131};132133module.exports = DOMChildrenOperations;134135136