From bd1fbebf7a5b7c9b4bc65be0f3ef2ebf91075957 Mon Sep 17 00:00:00 2001 From: Andreas Svensson Date: Mon, 13 Oct 2014 23:08:43 +0200 Subject: [PATCH] Move repeated code to removeAllChildren --- src/browser/ui/ReactMount.js | 6 ++---- src/browser/ui/dom/removeAllChildren.js | 25 ++++++++++++++++++++++++ src/vendor/core/createNodesFromMarkup.js | 5 ++--- 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 src/browser/ui/dom/removeAllChildren.js diff --git a/src/browser/ui/ReactMount.js b/src/browser/ui/ReactMount.js index 29e611d22013e..fb30f8190d556 100644 --- a/src/browser/ui/ReactMount.js +++ b/src/browser/ui/ReactMount.js @@ -30,6 +30,7 @@ var containsNode = require('containsNode'); var getReactRootElementInContainer = require('getReactRootElementInContainer'); var instantiateReactComponent = require('instantiateReactComponent'); var invariant = require('invariant'); +var removeAllChildren = require('removeAllChildren'); var setInnerHTML = require('setInnerHTML'); var shouldUpdateReactComponent = require('shouldUpdateReactComponent'); var warning = require('warning'); @@ -612,10 +613,7 @@ var ReactMount = { container = container.documentElement; } - // http://jsperf.com/emptying-a-node - while (container.lastChild) { - container.removeChild(container.lastChild); - } + removeAllChildren(container); }, /** diff --git a/src/browser/ui/dom/removeAllChildren.js b/src/browser/ui/dom/removeAllChildren.js new file mode 100644 index 0000000000000..f5efe10f04117 --- /dev/null +++ b/src/browser/ui/dom/removeAllChildren.js @@ -0,0 +1,25 @@ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule removeAllChildren + */ + +'use strict'; + +/** + * @param {DOMElement} node + */ +function removeAllChildren(node) { + // http://jsperf.com/emptying-a-node + var firstChild; + while ((firstChild = node.firstChild)) { + node.removeChild(firstChild); + } +} + +module.exports = removeAllChildren; diff --git a/src/vendor/core/createNodesFromMarkup.js b/src/vendor/core/createNodesFromMarkup.js index 176829401218e..54019ebb9a562 100644 --- a/src/vendor/core/createNodesFromMarkup.js +++ b/src/vendor/core/createNodesFromMarkup.js @@ -17,6 +17,7 @@ var ExecutionEnvironment = require('ExecutionEnvironment'); var createArrayFrom = require('createArrayFrom'); var getMarkupWrap = require('getMarkupWrap'); var invariant = require('invariant'); +var removeAllChildren = require('removeAllChildren'); /** * Dummy container used to render all markup. @@ -77,9 +78,7 @@ function createNodesFromMarkup(markup, handleScript) { } var nodes = createArrayFrom(node.childNodes); - while (node.lastChild) { - node.removeChild(node.lastChild); - } + removeAllChildren(node); return nodes; }