Difference between revisions of "Avoiding Casting"
From PowerUI
(→Convenience functions) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 45: | Line 45: | ||
|} | |} | ||
− | === Element === | + | === Node (Element, HtmlElement etc) === |
{| class="wikitable" | {| class="wikitable" | ||
Line 64: | Line 64: | ||
=== NodeList === | === NodeList === | ||
− | + | ''E.g. childNodes uses these'' | |
{| class="wikitable" | {| class="wikitable" | ||
Line 72: | Line 72: | ||
! What it does | ! What it does | ||
|- | |- | ||
− | | | + | | item(x) |
− | | | + | | htmlItem(x) |
| Node to HtmlElement | | Node to HtmlElement | ||
+ | |} | ||
+ | |||
+ | === HTMLCollection === | ||
+ | |||
+ | ''Poorly named by the specification; these are collections of Element objects.'' | ||
+ | |||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! Standard name | ||
+ | ! Convenience name | ||
+ | ! What it does | ||
+ | |- | ||
+ | | item(x) | ||
+ | | htmlItem(x) | ||
+ | | Element to HtmlElement | ||
|} | |} |
Latest revision as of 22:19, 9 February 2017
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.
Contents
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 |
Node (Element, HtmlElement etc)
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 |