/** * Gets the general sibling nodes. * * @param DOMElement $node DOMElement object * @param string $tagname Tag name * * @return array of DOMElement */ public function filter($node, $tagname) { $ret = array(); while ($node = Dom::getNextSiblingElement($node)) { array_push($ret, $node); } return $ret; }
/** * Gets nodes from a list by a given factor. * * @param array $nodes List of DOMNode objects * @param CssParserModelFactor $factor Factor object * * @return array of DOMElement objects */ private function _getNodesByFactor($nodes, $factor) { $ret = array(); foreach ($nodes as $node) { $items = $factor->filter($node); $ret = Dom::mergeNodes($ret, $items); } return $ret; }
/** * Does the node match? * * @param DOMElement $node DOMElement object * @param integer $position Node position * @param array $items List of nodes * * @return boolean */ public function match($node, $position, $items) { $i = 1; while ($node = Dom::getPreviousSiblingElement($node)) { $i++; if ($i > $this->_position) { return false; } } return $i == $this->_position; }
/** * Is the next thing a selectorList? * * A selectorList is one or more css selectors separated by commas. * * For example: * ``` * div > div.class div#id, div > p, pre * ``` * * In the above example, the following strings are terms: * ``` * div > div.class div * div > p * pre * ``` * * @return ArrayObject of DOMElement objects */ protected function selectorList() { $nodes = array(); do { if (!($selector = $this->is("selector"))) { // throw new TextParserException("Invalid expression", $this); break; } $nodes = Dom::mergeNodes($nodes, $selector->filter($this->_node)); } while ($this->eq(",")); return new ArrayObject(Dom::sortNodes($nodes)); }
/** * Does the node match? * * @param DOMElement $node DOMElement object * @param integer $position Node position * @param array $items List of nodes * * @return boolean */ public function match($node, $position, $items) { return !Dom::getPreviousSiblingElement($node); }
/** * Gets a string representation of the node. * * @return string */ public function __toString() { $ret = ""; foreach ($this->elements as $element) { $ret = Text::concat("\n", $ret, Dom::dom2str($element)); } return $ret; }
/** * Gets the child nodes. * * @param DOMElement $node DOMElement object * @param string $tagname Tag name * * @return array of DOMElement */ public function filter($node, $tagname) { return Dom::getChildElements($node); }
/** * Sort nodes in the same order they appear in the document. * * @param array $nodes List of DOMNode objects * * @return array of DOMNode objects */ public static function sortNodes($nodes) { // saves node paths foreach ($nodes as $node) { if (!isset($node->__path__)) { $node->__path__ = Dom::_getNodePath($node); } } // sorts elements in the same order they appear in the document usort($nodes, function ($node0, $node1) { $path0 = $node0->__path__; $path1 = $node1->__path__; $count0 = count($path0); $count1 = count($path1); $len = min($count0, $count1); for ($i = 0; $i < $len; $i++) { if ($path0[$i] != $path1[$i]) { return $path0[$i] > $path1[$i]; } } return $count0 > $count1; }); // unsets __path__ foreach ($nodes as $node) { unset($node->__path__); } return $nodes; }
/** * Does the node match? * * @param DOMElement $node DOMElement object * @param integer $position Node position * @param array $items List of nodes * * @return boolean */ public function match($node, $position, $items) { return Dom::searchNode($node, $this->_items) === false; }
/** * Gets inner HTML code. * * @return string */ private function _getInnerHtml() { $ret = ""; foreach ($this->elements() as $element) { $childNodes = $element->childNodes; $str = ""; foreach ($childNodes as $node) { $str .= Dom::dom2str($node); } $ret = Text::concat("\n", $ret, $str); } return $ret; }
/** * Gets the descendant nodes. * * @param DOMElement $node DOMElement object * @param string $tagname Tag name * * @return array of DOMElement */ public function filter($node, $tagname) { return Dom::getElementsByTagName($node, $tagname); }