/** * @param mixed $content * @param bool $includeTextNodes * @param int $limit * @return array|\Traversable null */ private function getNodeList($content, $includeTextNodes = TRUE, $limit = -1) { if ($callback = Constraints::isCallable($content)) { $content = $callback(); } if ($content instanceof \DOMElement) { return array($content); } elseif ($includeTextNodes && Constraints::isNode($content)) { return array($content); } elseif (Constraints::isNodeList($content)) { return $this->getLimitedArray($content, $limit); } return NULL; }
/** * Execute a function within the context of every matched element. * * If $elementsOnly is a callable the return value defines if * it is called for that node. * * If $elementsOnly is set to TRUE, only element nodes are used. * * @param callable $function * @param callable|bool|NULL $elementsFilter * @return $this */ public function each(callable $function, $elementsFilter = NULL) { if (TRUE === $elementsFilter) { $filter = function ($node) { return $node instanceof \DOMElement; }; } else { $filter = Constraints::isCallable($elementsFilter); } foreach ($this->_nodes as $index => $node) { if (NULL === $filter || $filter($node, $index)) { call_user_func($function, $node, $index); } } return $this; }
/** * Adds the specified classes if the switch is TRUE, * removes the specified classes if the switch is FALSE, * toggles the specified classes if the switch is NULL. * * @example toggleClass.php Usage Example: FluentDOM\Query::toggleClass() * @param string|callable $class * @param NULL|boolean $switch toggle if NULL, add if TRUE, remove if FALSE * @return Query */ public function toggleClass($class, $switch = NULL) { $callback = Constraints::isCallable($class); $this->each(function (\DOMElement $node, $index) use($class, $switch, $callback) { if ($callback) { $classString = $callback($node, $index, $node->getAttribute('class')); } else { $classString = $class; } if (empty($classString) && !(bool) $switch) { if ($node->hasAttribute('class')) { $node->removeAttribute('class'); } } else { $modified = $this->changeClassString($node->getAttribute('class'), $classString, $switch); if (FALSE !== $modified) { if (empty($modified)) { $node->removeAttribute('class'); } else { $node->setAttribute('class', $modified); } } } }, TRUE); return $this; }