Difference between revisions of "Avoiding Casting"
From PowerUI
(Created page with "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 l...") |
(→Convenience functions) |
||
Line 8: | Line 8: | ||
If you find an instance where a convenience function would be useful, please let us know! | 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)'' | ''(these ones have the bonus of being a little shorter too - just omit the word Element from the standard Web API)'' | ||
Line 13: | Line 15: | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
− | |||
! Standard name | ! Standard name | ||
! Convenience name | ! Convenience name | ||
! What it does | ! What it does | ||
|- | |- | ||
− | |||
| getElementById | | getElementById | ||
| getById | | getById | ||
| Element to HtmlElement | | Element to HtmlElement | ||
|- | |- | ||
− | |||
| getElementByTagName | | getElementByTagName | ||
| getByTagName | | getByTagName | ||
| Element to HtmlElement | | Element to HtmlElement | ||
|- | |- | ||
− | |||
| getElementByAttribute | | getElementByAttribute | ||
| getByAttribute | | getByAttribute | ||
| Element to HtmlElement | | Element to HtmlElement | ||
+ | |} | ||
+ | |||
+ | === Event === | ||
+ | |||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! Standard name | ||
+ | ! Convenience name | ||
+ | ! What it does | ||
|- | |- | ||
− | |||
| target | | target | ||
| htmlTarget | | htmlTarget | ||
| Element to HtmlElement | | Element to HtmlElement | ||
+ | |} | ||
+ | |||
+ | === Element === | ||
+ | |||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! Standard name | ||
+ | ! Convenience name | ||
+ | ! What it does | ||
+ | |- | ||
+ | | parentNode | ||
+ | | htmlParentNode | ||
+ | | Node to HtmlElement | ||
+ | |- | ||
+ | | document | ||
+ | | htmlDocument | ||
+ | | Document to HtmlDocument | ||
+ | |} | ||
+ | |||
+ | === NodeList === | ||
+ | |||
+ | *E.g. childNodes uses these* | ||
+ | |||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! Standard name | ||
+ | ! Convenience name | ||
+ | ! What it does | ||
+ | |- | ||
+ | | get | ||
+ | | getHtml | ||
+ | | Node to HtmlElement | ||
|} | |} |
Revision as of 22:12, 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 |
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 |
---|---|---|
get | getHtml | Node to HtmlElement |