mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
4e044f553f
* Update comment on setTextContent update the comment explaining the reason for the helper * Use `setTextContent` in ReactDOM for consistency
38 lines
898 B
JavaScript
38 lines
898 B
JavaScript
/**
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import {TEXT_NODE} from '../shared/HTMLNodeType';
|
|
|
|
/**
|
|
* Set the textContent property of a node. For text updates, it's faster
|
|
* to set the `nodeValue` of the Text node directly instead of using
|
|
* `.textContent` which will remove the existing node and create a new one.
|
|
*
|
|
* @param {DOMElement} node
|
|
* @param {string} text
|
|
* @internal
|
|
*/
|
|
let setTextContent = function(node: Element, text: string): void {
|
|
if (text) {
|
|
let firstChild = node.firstChild;
|
|
|
|
if (
|
|
firstChild &&
|
|
firstChild === node.lastChild &&
|
|
firstChild.nodeType === TEXT_NODE
|
|
) {
|
|
firstChild.nodeValue = text;
|
|
return;
|
|
}
|
|
}
|
|
node.textContent = text;
|
|
};
|
|
|
|
export default setTextContent;
|