Added `childBy*` methods to `DOM/Traverse`
authorAlexander Ebert <ebert@woltlab.com>
Sat, 30 May 2015 19:19:42 +0000 (21:19 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 30 May 2015 19:19:42 +0000 (21:19 +0200)
The already existing `childrenBy*` methods are great, but searching for the first matching child yields rather much code. The new `childBy*` methods behave similar to the result of `querySelector()`.

wcfsetup/install/files/js/WoltLab/WCF/DOM/Traverse.js

index d6fdea6a18ec52da71265ea53c944811765db32f..cc2416bc7abcbc05cb410e358d2885d2f12fd9bc 100644 (file)
@@ -62,6 +62,39 @@ define(['DOM/Util'], function(DOMUtil) {
         */
        function DOMTraverse() {};
        DOMTraverse.prototype = {
+               /**
+                * Examines child elements and returns the first child matching the given selector.
+                * 
+                * @param       {Element}               el              element
+                * @param       {string}                selector        CSS selector to match child elements against
+                * @return      {(Element|null)}        null if there is no child node matching the selector
+                */
+               childBySel: function(el, selector) {
+                       return _children(el, SELECTOR, selector)[0] || null;
+               },
+               
+               /**
+                * Examines child elements and returns the first child that has the given CSS class set.
+                * 
+                * @param       {Element}               el              element
+                * @param       {string}                className       CSS class name
+                * @return      {(Element|null)}        null if there is no child node with given CSS class
+                */
+               childByClass: function(el, className) {
+                       return _children(el, CLASS_NAME, className)[0] || null;
+               },
+               
+               /**
+                * Examines child elements and returns the first child which equals the given tag.
+                * 
+                * @param       {Element}               el              element
+                * @param       {string}                tagName         element tag name
+                * @return      {(Element|null)}        null if there is no child node which equals given tag
+                */
+               childByTag: function(el, tagName) {
+                       return _children(el, TAG_NAME, tagName)[0] || null;
+               },
+               
                /**
                 * Examines child elements and returns all children matching the given selector.
                 *