Guest User

Untitled

a guest
Jun 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. /**
  2. * Similar to root.getElementsByTagName(tagName), but:
  3. *
  4. * 1. Returns root if root.tagName == tagName.
  5. * 2. Returns only one element (the first if there are many).
  6. * 3. Can take an array of tagName if there are alternatives.
  7. *
  8. * @param {Element} root Root node from which we start the search
  9. * @param {string|Array.<string>} tagNameOrArray Tag name we're looking for
  10. */
  11.  
  12. function getElementByTagNameImperative(root, tagNameOrArray) {
  13.  
  14. if (YAHOO.lang.isArray(tagNameOrArray)) {
  15. for (var tagNameIndex = 0; tagNameIndex < tagNameOrArray.length; tagNameIndex++) {
  16. var tagName = tagNameOrArray[tagNameIndex];
  17. var result = ORBEON.util.Dom.getElementByTagName(root, tagName);
  18. if (result != null) return result;
  19. }
  20. return null;
  21. } else {
  22. if (root.tagName.toLowerCase() == tagNameOrArray) {
  23. return root;
  24. } else {
  25. var matches = root.getElementsByTagName(tagNameOrArray);
  26. return matches.length != 0 ? matches[0] : null;
  27. }
  28. }
  29. }
  30.  
  31. function getElementByTagNameFunctional(root, tagNameOrArray) {
  32.  
  33. return _(tagNameOrArray).chain()
  34. .match(
  35. _.isArray, function(tagNameArray) {
  36. return _(tagNameArray).chain()
  37. .map(_.bind(ORBEON.util.Dom.getElementByTagName, null, root))
  38. .compact()
  39. .first()
  40. .value()
  41. },
  42. function(tagName) {
  43. return _(tagName).match(
  44. root.tagName.toLowerCase(), root,
  45. function() {
  46. return _(tagName).chain()
  47. .take(root.getElementsByTagName, root)
  48. .first()
  49. .value();
  50. }
  51. );
  52. })
  53. .match(_.isUndefined, null)
  54. .value();
  55. }
  56.  
  57. function getElementByTagNamePragmatic(root, tagNameOrArray) {
  58.  
  59. var result = _.isArray(tagNameOrArray)
  60. ? _(tagNameOrArray).chain()
  61. .map(_.bind(arguments.callee, null, root))
  62. .compact()
  63. .first()
  64. .value()
  65. : root.tagName.toLowerCase() == tagNameOrArray
  66. ? root
  67. : root.getElementsByTagName(tagNameOrArray)[0];
  68. return _.isUndefined(result) ? null : result;
  69. }
Add Comment
Please, Sign In to add comment