unicorn/prefer-dom-node-text-content Style
作用
强制在 DOM 节点上使用 .textContent,而不是 .innerText。
为什么这不好?
使用 .innerText 有一些缺点。
.innerText返回渲染后的文本并忽略隐藏内容,而.textContent返回节点的完整文本内容。.innerText可能会触发重排,因为它会考虑 CSS 样式。.innerText只对 HTMLElement 对象定义,而.textContent对所有 Node 对象都定义。
示例
此规则的错误代码示例:
javascript
const text = foo.innerText;此规则的正确代码示例:
javascript
const text = foo.textContent;如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"unicorn/prefer-dom-node-text-content": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"unicorn/prefer-dom-node-text-content": "error",
},
});bash
oxlint --deny unicorn/prefer-dom-node-text-content版本
此规则在 v0.0.21 中新增。
