unicorn/prefer-dom-node-text-content Style
作用
强制在 DOM 节点上使用 .textContent,而不是 .innerText。
为什么这不好?
使用 .innerText 有一些缺点。
.innerText的性能开销大得多,因为它需要布局信息来返回结果。.innerText仅针对 HTMLElement 对象定义,而.textContent针对所有 Node 对象定义。.innerText不是标准的,例如,Firefox 中并没有它。
示例
此规则的错误代码示例:
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 中新增。
