feat: modify the each function of class elements

This commit is contained in:
Jaeger 2020-03-15 14:17:18 +08:00
parent 52bbdeae14
commit b3d84cf057

View File

@ -114,7 +114,6 @@ use phpQueryObject;
* @method Elements removeClass($className) * @method Elements removeClass($className)
* @method Elements toggleClass($className) * @method Elements toggleClass($className)
* @method Elements _empty() * @method Elements _empty()
* @method Elements each($callback,$param1=null,$param2=null,$param3=null)
* @method Elements callback($callback, $param1 = null, $param2 = null, $param3 = null) * @method Elements callback($callback, $param1 = null, $param2 = null, $param3 = null)
* @method string data($key, $value = null) * @method string data($key, $value = null)
* @method Elements removeData($key) * @method Elements removeData($key)
@ -133,7 +132,6 @@ use phpQueryObject;
* @method Elements dumpTree($html, $title) * @method Elements dumpTree($html, $title)
* @method dumpDie() * @method dumpDie()
*/ */
class Elements class Elements
{ {
/** /**
@ -175,21 +173,12 @@ class Elements
*/ */
public function each(callable $callback) public function each(callable $callback)
{ {
$break = false; foreach ($this->elements as $key => $element) {
$this->elements->each(function ($dom) use ($callback, &$break) { $break = $callback(new self(pq($element)), $key);
if ( ! $dom || $break) { if ($break === false) {
return; break;
} }
$orig = $dom;
$dom = new Elements(pq($dom));
if (false === call_user_func($callback, $dom)) {
$dom = $orig;
$break = true;
} else {
$dom = $dom->getDOMDocument();
} }
unset($orig);
});
return $this; return $this;
} }