react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / setInnerHTML.js
81159 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 setInnerHTML9*/1011"use strict";1213var ExecutionEnvironment = require('ExecutionEnvironment');1415var WHITESPACE_TEST = /^[ \r\n\t\f]/;16var NONVISIBLE_TEST = /<(!--|link|noscript|meta|script|style)[ \r\n\t\f\/>]/;1718/**19* Set the innerHTML property of a node, ensuring that whitespace is preserved20* even in IE8.21*22* @param {DOMElement} node23* @param {string} html24* @internal25*/26var setInnerHTML = function(node, html) {27node.innerHTML = html;28};2930if (ExecutionEnvironment.canUseDOM) {31// IE8: When updating a just created node with innerHTML only leading32// whitespace is removed. When updating an existing node with innerHTML33// whitespace in root TextNodes is also collapsed.34// @see quirksmode.org/bugreports/archives/2004/11/innerhtml_and_t.html3536// Feature detection; only IE8 is known to behave improperly like this.37var testElement = document.createElement('div');38testElement.innerHTML = ' ';39if (testElement.innerHTML === '') {40setInnerHTML = function(node, html) {41// Magic theory: IE8 supposedly differentiates between added and updated42// nodes when processing innerHTML, innerHTML on updated nodes suffers43// from worse whitespace behavior. Re-adding a node like this triggers44// the initial and more favorable whitespace behavior.45// TODO: What to do on a detached node?46if (node.parentNode) {47node.parentNode.replaceChild(node, node);48}4950// We also implement a workaround for non-visible tags disappearing into51// thin air on IE8, this only happens if there is no visible text52// in-front of the non-visible tags. Piggyback on the whitespace fix53// and simply check if any non-visible tags appear in the source.54if (WHITESPACE_TEST.test(html) ||55html[0] === '<' && NONVISIBLE_TEST.test(html)) {56// Recover leading whitespace by temporarily prepending any character.57// \uFEFF has the potential advantage of being zero-width/invisible.58node.innerHTML = '\uFEFF' + html;5960// deleteData leaves an empty `TextNode` which offsets the index of all61// children. Definitely want to avoid this.62var textNode = node.firstChild;63if (textNode.data.length === 1) {64node.removeChild(textNode);65} else {66textNode.deleteData(0, 1);67}68} else {69node.innerHTML = html;70}71};72}73}7475module.exports = setInnerHTML;767778