跟隨標準與Webkit源碼探究DOM -- 擷取元素之getElementsByTagName

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   使用   sp   for   

按照標籤名擷取元素 -- getElementsByTagName標準
  • DOM 1在Element和Document兩個interface中均有定義,原型NodeList getElementsByTagName(in DOMString tagname),指明按照先序遍曆遇到的順序排列,不會拋出任何異常,參數"*"返回對應document或者element下所有元素。注意這裡指明返回的是一個live的僅含有Element的NodeList。
  • DOM 2裡定義仍在Element和Document,增加了帶namespace的NodeList getElementsByTagNameNS(in DOMString namespaceURI, in DOMString localName)(Element,Document),引入了localName的概念(只有ELEMENT_NODE和ATTRIBUTE_NODE才能有)。
  • DOM 3(Document,Element)特別聲明XML應當對標籤名的大小寫敏感,非XML則依照文件類型自己對待大小寫風格來決定是否敏感。實際上瀏覽器對HTML都會先將標籤轉統一換成成小寫後再去匹配,所以只能匹配到實際標籤名為小寫元素。
  • WHATWG (Document ,Element)將傳回型別修改為了HTMLCollection,並解釋了通過localName產生HTMLCollection的演算法。注意演算法的第二步實際上規定了在非HTML文檔裡,標籤名大小寫敏感;而在HTML文檔裡,任何大小寫標籤都會被統一轉換成小寫後再去匹配,所以只能匹配到實際標籤名為小寫元素。
  • DOM 4(Document,Element) 目前與 WHATWG 基本一致
DOM Tree Accessors

DOM 1 與 DOM 2 的 HTMLDocument interface 裡定義了一系列"DOM Tree Accessors"

  • readonly attribute HTMLCollection images
  • readonly attribute HTMLCollection applets
  • readonly attribute HTMLCollection links
  • readonly attribute HTMLCollection forms
  • readonly attribute HTMLCollection anchors
  • attribute HTMLElement body

這意味著在HTML文檔裡可以用document.images獲得所有<img>元素,用document.links擷取所有帶有href屬性的<a>元素,用document.links擷取所有帶有name屬性的<a>元素,用document.forms擷取所有<form>元素。另外擷取<body>可以使用document.body。

自 HTML5(W3C,WHATWG)開始還定義了document.head,刪去了document.anchors,增加了擷取<embed>的document.embeds和目前與之相同的document.plugins,以及擷取<script>的document.scripts。

自 DOM 1 便在Document 定義了document.documentElement來擷取根項目並保留至今,在HTML文檔裡即<html>元素。

相容性
  • IE 5.5 不支援*作為參數擷取所有元素。IE6以上的IE以及各大瀏覽器均按照標準實現了getElementsByTagName。
  • 雖然document.scripts,document.embeds和document.plugins直到HTML5才標準化,不過各版本IE和其他瀏覽器的現行版本都支援
  • document.head需要IE9+才支援。其他DOM Tree Accessor基本在各版本IE和現行的瀏覽器裡都有支援。
Webkit 程式碼分析

類似getElementsByName,getElementsByTagName在ContainerNode裡實現。由於標準裡對XML的特殊規定,這裡會依據文件類型,換用TagNodeList或者HTMLTagNodeList作為NodeListsNodeData::addCacheWithAtomicName<>的template specialization(參見WebCore/dom/ContainerNode.cpp)。

TagNodeList實現的elementMatches是:

if (m_localName != starAtom && m_localName != element.localName())    return false;return m_namespaceURI == starAtom || m_namespaceURI == element.namespaceURI();

這裡starAtom就是標準裡說的*。先比對localName是否相符或為*,然後比對namespaceURI是否相符或為*。由於沒有大小寫轉換步驟,所以遵循標準,是大小寫敏感的。注意這裡比對namespaceURI的步驟相對於getElementsByTagName是多餘的,之所以加上是因為getElementsByTagNameNS也用TagNodeList,這樣就可以偷懶不用再多寫一個比對namespaceURI的版本。不過getElementsByTagNameNS用的其實是addCacheWithQualifiedName而不是addCacheWithAtomicName,其實addCacheWithQualifiedName和addCacheWithAtomicName的不同也就是它拿TagNodeList直接提前做好了template specification而已(參見WebCore/dom/NodeRareData.h)

HTMLTagNodeList實現的elementMatches是:

if (m_localName == starAtom)    return true;const AtomicString& localName = element.isHTMLElement() ? m_loweredLocalName : m_localName;    return localName == element.localName();

按照標準所說,如果被比對的元素是HTML namespace裡的,轉換為小寫再比較。注意這裡沒有比對namespaceURI,畢竟getElementsByTagNameNS不用它(標準裡沒有指明getElementsByTagNameNS需要轉換大小寫,所以用TagNodeList那個大小寫敏感的過濾足矣)。

跟隨標準與Webkit源碼探究DOM -- 擷取元素之getElementsByTagName

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.