@samitouri / QOS-React-2 / commits / 9ee1d92b52

Remove unnecessary tests from insertChildAt and inline it instead

Andreas Svensson committed Jan 13, 2014 at 23:09 UTC 9ee1d92b5273732265698d67c5d8706894151e1e
1 file changed +8 -14
src/browser/ui/dom/DOMChildrenOperations.js
+8 -14
@@ -42,20 +42,14 @@ var textContentAccessor = getTextContentAccessor();
42 * @internal
43 */
44 function insertChildAt(parentNode, childNode, index) {
45 - var childNodes = parentNode.childNodes;
46 - if (childNodes[index] === childNode) {
47 - return;
48 - }
49 - // If `childNode` is already a child of `parentNode`, remove it so that
50 - // computing `childNodes[index]` takes into account the removal.
51 - if (childNode.parentNode === parentNode) {
52 - parentNode.removeChild(childNode);
53 - }
54 - if (index >= childNodes.length) {
55 - parentNode.appendChild(childNode);
56 - } else {
57 - parentNode.insertBefore(childNode, childNodes[index]);
58 - }
45 + // By exploiting arrays returning `undefined` for an undefined index, we can
46 + // rely exclusively on `insertBefore(node, null)` instead of also using
47 + // `appendChild(node)`. However, using `undefined` is not allowed by all
48 + // browsers so we must replace it with `null`.
49 + parentNode.insertBefore(
50 + childNode,
51 + parentNode.childNodes[index] || null
52 + );
53 }
54
55 var updateTextContent;