Вход Регистрация
Файл: error-kitty/node_modules/cheerio/lib/api/traversing.js
Строк: 379
<?php
var = require('lodash'),
    
select = require('CSSselect'),
    
utils = require('../utils'),
    
domEach utils.domEach,
    
uniqueSort = require('htmlparser2').DomUtils.uniqueSort,
    
isTag utils.isTag;

var 
find exports.find = function(selector) {
  var 
elems _.reduce(this, function(memoelem) {
    return 
memo.concat(_.filter(elem.childrenisTag));
  }, []);

  return 
this._make(select(selectorelemsthis.options));
};

// Get the parent of each element in the current set of matched elements,
// optionally filtered by a selector.
var parent exports.parent = function(selector) {
  var 
set = [];
  var 
$set;

  
domEach(this, function(idxelem) {
    var 
parentElem elem.parent;
    if (
parentElem && set.indexOf(parentElem) < 0) {
      
set.push(parentElem);
    }
  });

  if (
arguments.length) {
    
set filter.call(setselectorthis);
  }

  return 
this._make(set);
};

var 
parents exports.parents = function(selector) {
  var 
parentNodes = [];

  
// When multiple DOM elements are in the original set, the resulting set will
  // be in *reverse* order of the original elements as well, with duplicates
  // removed.
  
this.get().reverse().forEach(function(elem) {
    
traverseParents(thiselem.parentselectorInfinity)
      .forEach(function(
node) {
        if (
parentNodes.indexOf(node) === -1) {
          
parentNodes.push(node);
        }
      }
    );
  }, 
this);

  return 
this._make(parentNodes);
};

var 
parentsUntil exports.parentsUntil = function(selectorfilter) {
  var 
parentNodes = [], untilNodeuntilNodes;

  if (
typeof selector === 'string') {
    
untilNode select(selectorthis.parents().toArray(), this.options)[0];
  } else if (
selector && selector.cheerio) {
    
untilNodes selector.toArray();
  } else if (
selector) {
    
untilNode selector;
  }

  
// When multiple DOM elements are in the original set, the resulting set will
  // be in *reverse* order of the original elements as well, with duplicates
  // removed.

  
this.toArray().reverse().forEach(function(elem) {
    while ((
elem elem.parent)) {
      if ((
untilNode && elem !== untilNode) ||
        (
untilNodes && untilNodes.indexOf(elem) === -1) ||
        (!
untilNode && !untilNodes)) {
        if (
isTag(elem) && parentNodes.indexOf(elem) === -1) { parentNodes.push(elem); }
      } else {
        break;
      }
    }
  }, 
this);

  return 
this._make(filter select(filterparentNodesthis.options) : parentNodes);
};

// For each element in the set, get the first element that matches the selector
// by testing the element itself and traversing up through its ancestors in the
// DOM tree.
var closest exports.closest = function(selector) {
  var 
set = [];

  if (!
selector) {
    return 
this._make(set);
  }

  
domEach(this, function(idxelem) {
    var 
closestElem traverseParents(thiselemselector1)[0];

    
// Do not add duplicate elements to the set
    
if (closestElem && set.indexOf(closestElem) < 0) {
      
set.push(closestElem);
    }
  }.
bind(this));

  return 
this._make(set);
};

var 
next exports.next = function(selector) {
  if (!
this[0]) { return this; }
  var 
elems = [];

  
_.forEach(this, function(elem) {
    while ((
elem elem.next)) {
      if (
isTag(elem)) {
        
elems.push(elem);
        return;
      }
    }
  });

  return 
selector filter.call(elemsselectorthis) : this._make(elems);
};

var 
nextAll exports.nextAll = function(selector) {
  if (!
this[0]) { return this; }
  var 
elems = [];

  
_.forEach(this, function(elem) {
    while ((
elem elem.next)) {
      if (
isTag(elem) && elems.indexOf(elem) === -1) {
        
elems.push(elem);
      }
    }
  });

  return 
selector filter.call(elemsselectorthis) : this._make(elems);
};

var 
nextUntil exports.nextUntil = function(selectorfilterSelector) {
  if (!
this[0]) { return this; }
  var 
elems = [], untilNodeuntilNodes;

  if (
typeof selector === 'string') {
    
untilNode select(selectorthis.nextAll().get(), this.options)[0];
  } else if (
selector && selector.cheerio) {
    
untilNodes selector.get();
  } else if (
selector) {
    
untilNode selector;
  }

  
_.forEach(this, function(elem) {
    while ((
elem elem.next)) {
      if ((
untilNode && elem !== untilNode) ||
        (
untilNodes && untilNodes.indexOf(elem) === -1) ||
        (!
untilNode && !untilNodes)) {
        if (
isTag(elem) && elems.indexOf(elem) === -1) {
          
elems.push(elem);
        }
      } else {
        break;
      }
    }
  });

  return 
filterSelector ?
    
filter.call(elemsfilterSelectorthis) :
    
this._make(elems);
};

var 
prev exports.prev = function(selector) {
  if (!
this[0]) { return this; }
  var 
elems = [];

  
_.forEach(this, function(elem) {
    while ((
elem elem.prev)) {
      if (
isTag(elem)) {
        
elems.push(elem);
        return;
      }
    }
  });

  return 
selector filter.call(elemsselectorthis) : this._make(elems);
};

var 
prevAll exports.prevAll = function(selector) {
  if (!
this[0]) { return this; }
  var 
elems = [];

  
_.forEach(this, function(elem) {
    while ((
elem elem.prev)) {
      if (
isTag(elem) && elems.indexOf(elem) === -1) {
        
elems.push(elem);
      }
    }
  });

  return 
selector filter.call(elemsselectorthis) : this._make(elems);
};

var 
prevUntil exports.prevUntil = function(selectorfilterSelector) {
  if (!
this[0]) { return this; }
  var 
elems = [], untilNodeuntilNodes;

  if (
typeof selector === 'string') {
    
untilNode select(selectorthis.prevAll().get(), this.options)[0];
  } else if (
selector && selector.cheerio) {
    
untilNodes selector.get();
  } else if (
selector) {
    
untilNode selector;
  }

  
_.forEach(this, function(elem) {
    while ((
elem elem.prev)) {
      if ((
untilNode && elem !== untilNode) ||
        (
untilNodes && untilNodes.indexOf(elem) === -1) ||
        (!
untilNode && !untilNodes)) {
        if (
isTag(elem) && elems.indexOf(elem) === -1) {
          
elems.push(elem);
        }
      } else {
        break;
      }
    }
  });

  return 
filterSelector ?
    
filter.call(elemsfilterSelectorthis) :
    
this._make(elems);
};

var 
siblings exports.siblings = function(selector) {
  var 
parent this.parent();

  var 
elems _.filter(
    
parent parent.children() : this.siblingsAndMe(),
    function(
elem) { return isTag(elem) && !this.is(elem); },
    
this
  
);

  if (
selector !== undefined) {
    return 
filter.call(elemsselectorthis);
  } else {
    return 
this._make(elems);
  }
};

var 
children exports.children = function(selector) {

  var 
elems _.reduce(this, function(memoelem) {
    return 
memo.concat(_.filter(elem.childrenisTag));
  }, []);

  if (
selector === undefined) return this._make(elems);
  else if (
typeof selector === 'number') return this._make(elems[selector]);

  return 
filter.call(elemsselectorthis);
};

var 
contents exports.contents = function() {
  return 
this._make(_.reduce(this, function(allelem) {
    
all.push.apply(allelem.children);
    return 
all;
  }, []));
};

var 
each exports.each = function(fn) {
  var 
0len this.length;
  while (
len && fn.call(this[i], ithis[i]) !== false) ++i;
  return 
this;
};

var 
map exports.map = function(fn) {
  return 
this._make(_.reduce(this, function(memoeli) {
    var 
val fn.call(eliel);
    return 
val == null memo memo.concat(val);
  }, []));
};

var 
filter exports.filter = function(matchcontainer) {
  
container container || this;

  var 
make _.bind(container._makecontainer);
  var 
filterFn;

  if (
typeof match === 'string') {
    
filterFn select.compile(matchcontainer.options);
  } else if (
typeof match === 'function') {
    
filterFn = function(eli) {
      return 
match.call(eliel);
    };
  } else if (
match.cheerio) {
    
filterFn match.is.bind(match);
  } else {
    
filterFn = function(el) {
      return 
match === el;
    };
  }

  return 
make(_.filter(thisfilterFn));
};

var 
first exports.first = function() {
  return 
this.length this._make(this[0]) : this;
};

var 
last exports.last = function() {
  return 
this.length this._make(this[this.length 1]) : this;
};

// Reduce the set of matched elements to the one at the specified index.
var eq exports.eq = function(i) {
  
= +i;

  
// Use the first identity optimization if possible
  
if (=== && this.length <= 1) return this;

  if (
0this.length i;
  return 
this[i] ? this._make(this[i]) : this._make([]);
};

// Retrieve the DOM elements matched by the jQuery object.
var get exports.get = function(i) {
  if (
== null) {
    return Array.
prototype.slice.call(this);
  } else {
    return 
this[? (this.length i) : i];
  }
};

var 
slice exports.slice = function() {
  return 
this._make([].slice.apply(thisarguments));
};

function 
traverseParents(selfelemselectorlimit) {
  var 
elems = [];
  while (
elem && elems.length limit) {
    if (!
selector || filter.call([elem], selectorself).length) {
      
elems.push(elem);
    }
    
elem elem.parent;
  }
  return 
elems;
}

// End the most recent filtering operation in the current chain and return the
// set of matched elements to its previous state.
var end exports.end = function() {
  return 
this.prevObject || this._make([]);
};

var 
add exports.add = function(othercontext) {
  var 
selection this._make(othercontext);
  var 
contents uniqueSort(selection.get().concat(this.get()));

  for (var 
0contents.length; ++i) {
    
selection[i] = contents[i];
  }
  
selection.length contents.length;

  return 
selection;
};
?>
Онлайн: 1
Реклама