From: Alexander Ebert Date: Sat, 30 May 2015 19:19:42 +0000 (+0200) Subject: Added `childBy*` methods to `DOM/Traverse` X-Git-Tag: 3.0.0_Beta_1~2300 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=fa033f4fcfaa295e1903464449f67d2dff5a98b4;p=GitHub%2FWoltLab%2FWCF.git Added `childBy*` methods to `DOM/Traverse` 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()`. --- diff --git a/wcfsetup/install/files/js/WoltLab/WCF/DOM/Traverse.js b/wcfsetup/install/files/js/WoltLab/WCF/DOM/Traverse.js index d6fdea6a18..cc2416bc7a 100644 --- a/wcfsetup/install/files/js/WoltLab/WCF/DOM/Traverse.js +++ b/wcfsetup/install/files/js/WoltLab/WCF/DOM/Traverse.js @@ -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. *