main
js 37 lines 893 Bytes
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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 * @flow
8 */
9
10 import {TEXT_NODE} from './HTMLNodeType';
11
12 /**
13 * Set the textContent property of a node. For text updates, it's faster
14 * to set the `nodeValue` of the Text node directly instead of using
15 * `.textContent` which will remove the existing node and create a new one.
16 *
17 * @param {DOMElement} node
18 * @param {string} text
19 * @internal
20 */
21 function setTextContent(node: Element, text: string): void {
22 if (text) {
23 const firstChild = node.firstChild;
24
25 if (
26 firstChild &&
27 firstChild === node.lastChild &&
28 firstChild.nodeType === TEXT_NODE
29 ) {
30 firstChild.nodeValue = text;
31 return;
32 }
33 }
34 node.textContent = text;
35 }
36
37 export default setTextContent;