跳到主要内容

set text content

import { TEXT_NODE } from './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.
*
* 设置节点的 textContent 属性。对于文本更新,直接设置 Text 节点的 `nodeValue` 会
* 更快,而不是使用 `.textContent`,后者会删除现有的节点并创建一个新的节点。
*
* @param {DOMElement} node
* @param {string} text
* @internal
*/
function setTextContent(node: Element, text: string): void {
if (text) {
const firstChild = node.firstChild;

if (
firstChild &&
firstChild === node.lastChild &&
firstChild.nodeType === TEXT_NODE
) {
firstChild.nodeValue = text;
return;
}
}
node.textContent = text;
}

export default setTextContent;