Ei kuvausta

querying.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var isTag = require("domelementtype").isTag;
  2. module.exports = {
  3. filter: filter,
  4. find: find,
  5. findOneChild: findOneChild,
  6. findOne: findOne,
  7. existsOne: existsOne,
  8. findAll: findAll
  9. };
  10. function filter(test, element, recurse, limit){
  11. if(!Array.isArray(element)) element = [element];
  12. if(typeof limit !== "number" || !isFinite(limit)){
  13. limit = Infinity;
  14. }
  15. return find(test, element, recurse !== false, limit);
  16. }
  17. function find(test, elems, recurse, limit){
  18. var result = [], childs;
  19. for(var i = 0, j = elems.length; i < j; i++){
  20. if(test(elems[i])){
  21. result.push(elems[i]);
  22. if(--limit <= 0) break;
  23. }
  24. childs = elems[i].children;
  25. if(recurse && childs && childs.length > 0){
  26. childs = find(test, childs, recurse, limit);
  27. result = result.concat(childs);
  28. limit -= childs.length;
  29. if(limit <= 0) break;
  30. }
  31. }
  32. return result;
  33. }
  34. function findOneChild(test, elems){
  35. for(var i = 0, l = elems.length; i < l; i++){
  36. if(test(elems[i])) return elems[i];
  37. }
  38. return null;
  39. }
  40. function findOne(test, elems){
  41. var elem = null;
  42. for(var i = 0, l = elems.length; i < l && !elem; i++){
  43. if(!isTag(elems[i])){
  44. continue;
  45. } else if(test(elems[i])){
  46. elem = elems[i];
  47. } else if(elems[i].children.length > 0){
  48. elem = findOne(test, elems[i].children);
  49. }
  50. }
  51. return elem;
  52. }
  53. function existsOne(test, elems){
  54. for(var i = 0, l = elems.length; i < l; i++){
  55. if(
  56. isTag(elems[i]) && (
  57. test(elems[i]) || (
  58. elems[i].children.length > 0 &&
  59. existsOne(test, elems[i].children)
  60. )
  61. )
  62. ){
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. function findAll(test, elems){
  69. var result = [];
  70. for(var i = 0, j = elems.length; i < j; i++){
  71. if(!isTag(elems[i])) continue;
  72. if(test(elems[i])) result.push(elems[i]);
  73. if(elems[i].children.length > 0){
  74. result = result.concat(findAll(test, elems[i].children));
  75. }
  76. }
  77. return result;
  78. }