/** * 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 = DomHelper::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 = DomHelper::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 = DomHelper::getPreviousSiblingElement($node)) { $i++; if ($i > $this->_position) { return false; } } return $i == $this->_position; }
/** * Gets the child nodes. * * @param DOMElement $node DOMElement object * @param string $tagname Tag name * * @return array of DOMElement */ public function filter($node, $tagname) { return DomHelper::getChildElements($node); }
/** * 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 DomHelper::searchNode($node, $this->_items) === false; }
/** * 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 !DomHelper::getPreviousSiblingElement($node); }
/** * 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 = DomHelper::mergeNodes($nodes, $selector->filter($this->_node)); } while ($this->eq(",")); return new ArrayObject($nodes); }
/** * Gets inner HTML code. * * @return string */ private function _getInnerHtml() { $ret = ""; foreach ($this->elements() as $element) { $childNodes = $element->childNodes; $str = ""; foreach ($childNodes as $node) { $str .= DomHelper::dom2str($node); } $ret = TextHelper::concat("\n", $ret, $str); } return $ret; }
/** * Merges two lists of nodes in a single list. * * This function merges two list of nodes in a single list without repeating * nodes. * * @param array $items1 List of DOMNode objects * @param array $items2 List of DOMNode objects * * @return array of DOMNode objects */ public static function mergeNodes($items1, $items2) { $ret = array(); $items = array_merge($items1, $items2); $len = count($items); // retrieves non-repeated elements for ($i = 0; $i < $len; $i++) { $item = $items[$i]; $position = DomHelper::searchNode($item, $items, $i + 1); if ($position === false) { array_push($ret, $item); } } // sorts elements in order of appareance in the document usort($ret, function ($node0, $node1) { $path0 = DomHelper::_getNodePath($node0); $path1 = DomHelper::_getNodePath($node1); $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; }); return $ret; }
/** * Gets a string representation of the node. * * @return string */ public function __toString() { $ret = ""; foreach ($this->elements as $element) { $ret = TextHelper::concat("\n", $ret, DomHelper::dom2str($element)); } 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 DomHelper::getElementsByTagName($node, $tagname); }