developit / undom

🍩 1kb minimally viable DOM Document implementation
https://npm.im/undom
MIT License
662 stars 25 forks source link

Add hasAttribute and hasAttributeNS #13

Open tunnckoCore opened 7 years ago

tunnckoCore commented 7 years ago

They are pretty small addition, which can easily be implemented.

tunnckoCore commented 7 years ago

It would be just that

  hasAttribute (key) {
    return this.hasAttributeNS(null, key)
  }

  hasAttributeNS (namespaceURI, key) {
    return (
      this.attributes.hasOwnProperty(key) &&
      this.attributes[key].namespaceURI === namespaceURI
    )
  }

if setting attribute is correctly implemented. The interesting thing about this.attribtues is that it's map, where we have both indices and key names. So if we have this html

<div id="foo" bar="qux">hello</div>

then el.attributes[1] will be AttributeNode, but also it can be accessed with el.attributes.bar

developit commented 6 years ago

Definitely open to a PR for this! I'm curious how small it could be.