Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Similar to root.getElementsByTagName(tagName), but:
- *
- * 1. Returns root if root.tagName == tagName.
- * 2. Returns only one element (the first if there are many).
- * 3. Can take an array of tagName if there are alternatives.
- *
- * @param {Element} root Root node from which we start the search
- * @param {string|Array.<string>} tagNameOrArray Tag name we're looking for
- */
- function getElementByTagNameImperative(root, tagNameOrArray) {
- if (YAHOO.lang.isArray(tagNameOrArray)) {
- for (var tagNameIndex = 0; tagNameIndex < tagNameOrArray.length; tagNameIndex++) {
- var tagName = tagNameOrArray[tagNameIndex];
- var result = ORBEON.util.Dom.getElementByTagName(root, tagName);
- if (result != null) return result;
- }
- return null;
- } else {
- if (root.tagName.toLowerCase() == tagNameOrArray) {
- return root;
- } else {
- var matches = root.getElementsByTagName(tagNameOrArray);
- return matches.length != 0 ? matches[0] : null;
- }
- }
- }
- function getElementByTagNameFunctional(root, tagNameOrArray) {
- return _(tagNameOrArray).chain()
- .match(
- _.isArray, function(tagNameArray) {
- return _(tagNameArray).chain()
- .map(_.bind(ORBEON.util.Dom.getElementByTagName, null, root))
- .compact()
- .first()
- .value()
- },
- function(tagName) {
- return _(tagName).match(
- root.tagName.toLowerCase(), root,
- function() {
- return _(tagName).chain()
- .take(root.getElementsByTagName, root)
- .first()
- .value();
- }
- );
- })
- .match(_.isUndefined, null)
- .value();
- }
- function getElementByTagNamePragmatic(root, tagNameOrArray) {
- var result = _.isArray(tagNameOrArray)
- ? _(tagNameOrArray).chain()
- .map(_.bind(arguments.callee, null, root))
- .compact()
- .first()
- .value()
- : root.tagName.toLowerCase() == tagNameOrArray
- ? root
- : root.getElementsByTagName(tagNameOrArray)[0];
- return _.isUndefined(result) ? null : result;
- }
Add Comment
Please, Sign In to add comment