UNPKG

1.33 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
12var escapeTextContentForBrowser = require('./escapeTextContentForBrowser');
13var setInnerHTML = require('./setInnerHTML');
14
15/**
16 * Set the textContent property of a node, ensuring that whitespace is preserved
17 * even in IE8. innerText is a poor substitute for textContent and, among many
18 * issues, inserts <br> instead of the literal newline chars. innerHTML behaves
19 * as it should.
20 *
21 * @param {DOMElement} node
22 * @param {string} text
23 * @internal
24 */
25var setTextContent = function (node, text) {
26 if (text) {
27 var firstChild = node.firstChild;
28
29 if (firstChild && firstChild === node.lastChild && firstChild.nodeType === 3) {
30 firstChild.nodeValue = text;
31 return;
32 }
33 }
34 node.textContent = text;
35};
36
37if (ExecutionEnvironment.canUseDOM) {
38 if (!('textContent' in document.documentElement)) {
39 setTextContent = function (node, text) {
40 if (node.nodeType === 3) {
41 node.nodeValue = text;
42 return;
43 }
44 setInnerHTML(node, escapeTextContentForBrowser(text));
45 };
46 }
47}
48
49module.exports = setTextContent;
\No newline at end of file