Avoiding Casting

From PowerUI
Revision as of 22:14, 9 February 2017 by 151.229.186.156 (talk) (NodeList)
Jump to: navigation, search

PowerUI 2 has a standardised DOM. That means that methods like getElementById return an Element rather than the more useful HtmlElement, which results in a lot of casting that previously wasn't required.

Why does it happen at all?

HTML5 can have content like SVG and MathML embedded within it; an SvgElement is not a HtmlElement. However, the vast majority of the time, you'll be interacting with a HtmlElement (and on the web, Javascript hides this from you).

Convenience functions

If you find an instance where a convenience function would be useful, please let us know!

Document

(these ones have the bonus of being a little shorter too - just omit the word Element from the standard Web API)

Standard name Convenience name What it does
getElementById getById Element to HtmlElement
getElementByTagName getByTagName Element to HtmlElement
getElementByAttribute getByAttribute Element to HtmlElement

Event

Standard name Convenience name What it does
target htmlTarget Element to HtmlElement

Element

Standard name Convenience name What it does
parentNode htmlParentNode Node to HtmlElement
document htmlDocument Document to HtmlDocument

NodeList

E.g. childNodes uses these

Standard name Convenience name What it does
item(x) htmlItem(x) Node to HtmlElement

HTMLCollection

Poorly named by the specification; these are collections of Element objects.

Standard name Convenience name What it does
item(x) htmlItem(x) Element to HtmlElement