我一直在試圖檢查一些屬性是否屬於DOM HTML5
屬性。代碼遍歷html
標記並保存在Set
上。我的想法是在此示例中標識屬性ng-controller
,ng-repeat
,ng-bind
,因爲其他屬性(id
和class
)屬於DOM HTML5
屬性的一部分。是否有某種方法可用於檢查HTML屬性是否不屬於DOM HTML5?
HTML
<div id="html-markup">
<div class="class-1" ng-controller="myController">
<table id="the-table">
<thead class="table-header" >
<th ng-repeat="(key, val) in myList[0] track by key">
{{ key }}
</th>
</thead>
<tbody class="table-body">
<tr ng-repeat="item in myList track by item.id">
<td ng-repeat="(key, value) in item">
<span ng-bind="value"></span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
的Javascript
const element = document.getElementById("html-markup");
const allChildrensNodes = element.childNodes;
const allChildrensElements = element.getElementsByTagName("*");
const attributesArray = Array.from(allChildrensElements)
.reduce((set, child) => {
Array.from(child.attributes).forEach((attr) => {
//if(ATTRIBUTE IS NOT PART OF DOM HTML5 ATTRIBUTES)
set.add(attr.name)
});
return set;
}, new Set())
console.log(attributesArray);//Set(5) {"class", "ng-controller", "id", "ng-repeat", "ng-bind"}
有一些黑客或欺騙檢查呢?
一個娛樂codepen
與此:https://codepen.io/gpincheiraa/pen/veWKOj?editors=1010
能你確切地說明了這個最終目標是什麼?因爲做相反的事情並且檢查確定的屬性(或者像「ng-」的前綴)而不是過濾掉「常規」屬性會容易得多。網頁上有常用的JSON列表,但我不認爲這是保證是瀏覽器變體的證明。 – Kaddath
由於HTML5是一個「活的東西」,並且沒有任何版本,所以這樣的列表會不是靜態的開始,但隨着時間的推移而變化... – CBroe
@Kaddath我的想法是識別非標準屬性來替換佔位符的值。這個想法將做快照測試(https://facebook.github.io/jest/docs/en/snapshot-testing.html)。通過例子檢查「ng-」的問題是存在另一個前綴,比如「ui-」或「md-」,或者如果您創建自定義指令,則需要任何其他前綴。 –