获取 HTML 元素的所有 IDL 属性

问题描述

我知道可以像这样在标准 HTMLElement 上获得所有内容属性

<input type="text" value="John" />
const input = document.querySelector("input");
const attrs = Array.from(input.attributes);
console.log(attrs.map(a => a.name + ': ' a.value)); // [ "type: text","value: John" ]

我在一个世界(Salesforce Lightning Web 组件)中工作,IDL attributes(即您可以通过点表示法访问的那些)并不总是在运行时公开为内容属性。例如,对于以下模板:

<lightning-input label="First Name" value="John" data-id="fname">
</lightning-input>

我得到以下行为:

const input = element.shadowRoot.querySelector("lightning-input");
// I can get all these IDL attributes via dot notation
console.log(input.label); // First Name
console.log(input.value); // John
console.log(input.dataset.id); // fname

const attrs = Array.from(input.attributes);
// But they don't all show up here
console.log(attrs.map(a => a.name + ': ' a.value)); // [ "data-id: fname" ]

如您所见,我可以通过点表示法访问 labelvalue 等属性,但不能通过 Element.attributes 访问。

我正在寻找一种方法来内省元素上的所有 IDL 属性,以用于测试目的。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)