public function search($query) { $query = trim($query); $query_arg = $this->match_selector($query); if (empty($query_arg)) { return false; } if (empty($query_arg['pattern'])) { return false; } if (empty($query_arg['selectors'])) { return false; } $namespace = $this->_namespace; $count = 0; $all = $this->perform_query($query_arg); $ismulti = false; $totals[$query_arg['selectors']] = count($all[1]); if (isset($this->_multiselector) and is_array($this->_multiselector)) { $query_args = array($query_arg['selectors'] => $query_arg); $matches = $all; if (empty($matches)) { $matches = array_fill(0, 7, array()); } $matches[5] = array_fill(0, $totals[$query_arg['pattern']], $query_arg['pattern']); if ($matches[5] === false || empty($matches[5])) { $matches[5] = array(); } foreach ($this->_multiselector as $pattern) { $arg = $this->match_selector($pattern); $query_args[$arg['selectors']] = $arg; $ar = $this->perform_query($arg); array_shift($this->_multiselector); if (empty($ar) || empty($ar[0])) { continue; } $matches[0] = array_merge($matches[0], $ar[0]); $matches[1] = array_merge($matches[1], $ar[1]); $matches[2] = array_merge($matches[2], $ar[2]); $matches[3] = array_merge($matches[3], $ar[3]); $matches[4] = array_merge($matches[4], $ar[4]); $matches[6] = array_merge($matches[6], $ar[6]); $totals[$arg['selectors']] = count($ar[1]); $fills = array_fill(0, $totals[$arg['pattern']], $arg['pattern']); $matches[5] = array_merge($matches[5], $fills); unset($arg); unset($ar); } $ismulti = true; $all = $matches; unset($matches); } $this->removeMatchDuplicates($all); $counts = count($totals); $total = count($all[1]); $stops = $stop2 = array(); for ($i = 0; $total > $i; $i++) { $m = array($all[0][$i], $all[1][$i], $all[2][$i], $all[3][$i], $all[4][$i]); $paths = explode('|', $all[6][$i]); if ($ismulti and isset($query_args) and is_array($query_args) and isset($query_args[$all[5][$i]])) { $query_arg = $query_args[$all[5][$i]]; } --$totals[$query_arg['selectors']]; if (empty($m)) { continue; } if (array_key_exists('first', $query_arg['filters'])) { if (!empty($stops) and in_array($query_arg['selectors'], $stops)) { continue; } } if (array_key_exists('contains', $query_arg['filters']) and !empty($query_arg['filters']['contains'])) { if (strpos(strip_tags($m[3]), jqueryphp_abstracts_element::escape($query_arg['filters']['contains'])) === false) { continue; } } $repair = preg_replace('/=([\'"])\\s/', '=$1', $m[2]); if (preg_match_all('#([\\w\\-_]{2,}) =* (?:(?:[\'"])(.*?)[\'"])?#ix', $repair, $test)) { if (empty($test[1])) { $attr = array(); } else { $attr = @array_combine($test[1], $test[2]); } } if (array_key_exists('button', $query_arg['filters']) and in_array($m[1], array('input', 'button'))) { if ($m[1] === 'input' and !isset($attr['type']) || $attr['type'] !== 'button') { continue; } } if (array_key_exists('not', $query_arg['filters']) and $found == false) { if (!empty($query_arg['filters']['not'])) { } else { $found = true; } } if (array_key_exists('eq', $query_arg['filters']) and is_numeric($query_arg['filters']['eq'])) { if ($query_arg['filters']['eq'] != $i) { continue; } } if (array_key_exists('gt', $query_arg['filters']) and is_numeric($query_arg['filters']['gt'])) { if ($i > (int) $query_arg['filters']['gt'] == false) { continue; } } if (array_key_exists('lt', $query_arg['filters']) and is_numeric($query_arg['filters']['lt'])) { if ($i < (int) $query_arg['filters']['lt'] == false) { continue; } } if (array_key_exists('odd', $query_arg['filters'])) { if (($i % 2 == 0) == true) { continue; } } if (array_key_exists('even', $query_arg['filters'])) { if (($i % 2 == 0) == false) { continue; } } if (array_key_exists('parent', $query_arg['filters'])) { if (isset($query_arg['filters']['parent']) and !empty($query_arg['filters']['parent'])) { $parents = explode('/', $paths[3]); $ps = strpos(end($parents), $query_arg['filters']['parent']); if ($ps === false) { continue; } if ($ps !== 0) { continue; } } if (empty($m[3])) { continue; } if (strlen($m[3]) < 1) { continue; } } if (array_key_exists('has', $query_arg['filters'])) { if (empty($m[3])) { continue; } if (strip_tags($m[3]) == $m[3]) { continue; } if (!empty($query_arg['filters']['has'])) { if (preg_match('/<(' . $query_arg['filters']['has'] . '+)\\b(.*?>?:(.*?)<\\/\\1>)?/ix', strip_tags($m[3], '<' . $query_arg['filters']['has'] . '>')) != true) { continue; } } } if (array_key_exists('first', $query_arg['filters'])) { $stops[] = $query_arg['selectors']; } if (array_key_exists('last', $query_arg['filters'])) { if ($totals[$query_arg['selectors']] === 0) { $stop2[] = $query_arg['selectors']; } } if (array_key_exists('last', $query_arg['filters'])) { if (!empty($stop2) and in_array($query_arg['selectors'], $stop2)) { } else { continue; } } $ele = $this->toElement($m); $ele->_selector = $this->_selector . ' ' . $query_arg['selectors']; $ele->_nodeLevel = $query_arg['pattern']; $ele->_length = 1; $ele->length = 1; $ele->_localName = $all[4][$i]; $ele->_parentElement = $namespace; $ele->_token = $count; $ele->_path = $paths[0]; $ele->_prev_path = $paths[1]; $ele->_next_path = $paths[2]; $ele->_parent_path = $paths[3]; $ele->_attributes = $attr; $ele->selector = new jqueryphp_abstracts_selector($query_arg); $ele->setDomID($namespace . $i); $count++; $eles[] = $ele; } $this->__documentMap = $eles; if ($count > 1) { //Deselect $this->_currentNode = false; } elseif ($count === 1) { //Select the current element $this->seek(0); } unset($ele); $this->__mapLength = $count; $this->_length = $this->length = $count; return $this; }
public function run($new = Null) { if (is_a($new, jqmel)) { return $this->done = $new === $this->node; } elseif (is_callable($new)) { return $this->done = call_user_func($new, $this->node) == true; } else { $query = $this->match_selector($new, 'self::', false); } $attr = $this->node->data('_attributes')->get(); $path = $this->node->data('_path')->get(); $ns = $this->node->data('_parentElement')->get(); $tag = $this->node->data('_name')->get(); $done = false; $d = jqm_use($ns); $this->query_arg = $query; if (array_key_exists('button', $query['filters'])) { if (!in_array($tag, array('input', 'button'))) { return $this->done = false; } if ($tag == 'input' and !array_key_exists('type', $attr)) { return $this->done = false; } if ($tag == 'input' and $attr['type'] !== 'button') { return $this->done = false; } } if (array_key_exists('header', $query['filters'])) { $regex = '/h1|h2|h3|h4|h5|h6|h[1-9]/ix'; if (preg_match($regex, $tag) == false) { return $this->done = false; } } if (array_key_exists('first', $query['filters'])) { if (!isset($this->node->trace)) { $this->done = true; } else { $i = key($this->node->trace); $first = $this->node->trace[$i]; if ($first !== $this->node) { return $this->done = false; } } } if (array_key_exists('last', $query['filters'])) { if (!isset($this->node->trace)) { $this->done = true; } else { $last = end($this->node->trace); if ($last !== $this->node) { return $this->done = false; } } } if (array_key_exists('checked', $query['filters'])) { if ($tag !== 'input') { return $this->done = false; } if (!in_array($attr['type'], array('radio', 'checkbox'))) { return $this->done = false; } } if (array_key_exists('file', $query['filters'])) { if ($tag !== 'input') { return $this->done = false; } if ($attr['type'] !== 'file') { return $this->done = false; } } if (array_key_exists('text', $query['filters'])) { if ($tag !== 'input') { return $this->done = false; } if ($attr['type'] !== 'text') { return $this->done = false; } } if (array_key_exists('submit', $query['filters'])) { if (!in_array($tag, array('input', 'button'))) { return $this->done = false; } if ($attr['type'] !== 'submit') { return $this->done = false; } } if (array_key_exists('checkbox', $query['filters'])) { if ($tag !== 'input') { return $this->done = false; } if ($attr['type'] !== 'checkbox') { return $this->done = false; } } if (array_key_exists('radio', $query['filters'])) { if ($tag !== 'input') { return $this->done = false; } if ($attr['type'] !== 'radio') { return $this->done = false; } } if (array_key_exists('selected', $query['filters'])) { if ($tag !== 'option') { return $this->done = false; } if (!array_key_exists('selected', $attr)) { return $this->done = false; } $query['filters']['parent'] = 'select'; } if (array_key_exists('has', $query['filters'])) { $html = $this->node->html()->get(); if (empty($html) || strip_tags($html) == $html) { return $this->done = false; } if (!empty($query['filters']['has'])) { if (preg_match('/<(' . $query['filters']['has'] . ')\\b(.*?)>(?:(.*?)(<\\/\\1>))?/ix', $html) == false) { return $this->done = false; } } } if (array_key_exists('disabled', $query['filters']) and !array_key_exists('disabled', $attr)) { return $this->done = false; } if (array_key_exists('contains', $query['filters'])) { $html = $this->node->html()->get(); $html = strip_tags($html); if (empty($html)) { return $this->done = false; } if (!empty($query['filters']['contains'])) { if (strpos($html, jqueryphp_abstracts_element::escape($query['filters']['contains'])) === false) { return $this->done = false; } } } if (array_key_exists('visible', $query['filters'])) { if ($this->isHidden() === true) { return $this->done = false; } } if (array_key_exists('hidden', $query['filters'])) { if ($this->isHidden() === false) { return $this->done = false; } } if (array_key_exists('only-child', $query['filters'])) { $doc = $d->_DOM; $xpath = new domXpath($doc); $node = $xpath->query($path); if ($node->length < 1) { return $this->done = false; } if (!$node->item(0)->parentNode) { return $this->done = false; } if (!$node->item(0)->parentNode->childNodes) { return $this->done = false; } if ($node->item(0)->parentNode->childNodes->length > 1) { return $this->done = false; } } if (array_key_exists('nth-child', $query['filters'])) { if ($query['filters']['nth-child'] == NULL || strlen($query['filters']['nth-child']) === 0) { return $this->done = false; } $pos = strrpos($path, '['); if ($pos === false) { return $this->done = false; } if ($pos == strlen($path) - 1) { return $this->done = false; } $pos = trim(substr($path, $pos), '[]'); if (!is_numeric($pos)) { return $this->done = false; } $pos = (int) $pos; if (is_numeric($query['filters']['nth-child'])) { if ((int) $query['filters']['nth-child'] < 1) { return $this->done = false; } if ($pos !== (int) $query['filters']['nth-child']) { return $this->done = false; } } elseif ($query['filters']['nth-child'] === 'even') { if (($pos % 2 == 0) == false) { return $this->done = false; } } elseif ($query['filters']['nth-child'] === 'odd') { if (($pos % 2 == 0) == true) { return $this->done = false; } } else { return $this->done = false; } } if (array_key_exists('nth-last-child', $query['filters'])) { $doc = $d->_DOM; $xpath = new domXpath($doc); $node = $xpath->query($path); if ($node->length < 1) { return $this->done = false; } if (!$node->item(0)->parentNode) { return $this->done = false; } $cht = $node->item(0)->parentNode->childNodes->length; if (is_numeric($query['filters']['nth-last-child'])) { $pos = (int) $query['filters']['nth-last-child']; if ($pos < 1) { return $this->done = false; } $pos = $cht - $pos; } elseif (in_array($query['filters']['nth-last-child'], array('even', 'odd'))) { $pos = strrpos($path, '['); if ($pos === false) { return $this->done = false; } if ($pos == strlen($path) - 1) { return $this->done = false; } $pos = trim(substr($path, $pos), '[]'); if (!is_numeric($pos)) { return $this->done = false; } $pos = (int) $pos; if ($query['filters']['nth-last-child'] === 'even') { if ((($cht - $pos) % 2 == 0) === true) { return $this->done = false; } } if ($query['filters']['nth-last-child'] === 'odd') { if ((($cht - $pos) % 2 == 0) === false) { return $this->done = false; } } } else { return $this->done = false; } if ($pos > $cht) { return $this->done = false; } if ($pos < 0) { return $this->done = false; } $chd = $node->item(0)->parentNode->childNodes->item($pos - 1); if (!$chd) { return $this->done = false; } if ($node->item(0)->isSameNode($chd) === false) { return $this->done = false; } unset($chd); } if (array_key_exists('only-of-type', $query['filters'])) { $doc = $d->_DOM; $xpath = new domXpath($doc); $node = $xpath->query($path); if ($node->length < 1) { return $this->done = false; } if ($node->item(0)->childNodes) { if ($node->item(0)->childNodes->length > 0 and $node->item(0)->childNodes->item(0)->nodeType !== 3) { return $this->done = false; } } } if (array_key_exists('first-child', $query['filters'])) { $doc = $d->_DOM; $xpath = new domXpath($doc); $node = $xpath->query($path); if ($node->length < 1) { return $this->done = false; } if (!$node->item(0)->parentNode) { return $this->done = false; } if ($node->item(0)->parentNode->firstChild->isSameNode($node->item(0)) === false) { return $this->done = false; } } if (array_key_exists('last-child', $query['filters'])) { $doc = $d->_DOM; $xpath = new domXpath($doc); $node = $xpath->query($path); if ($node->length < 1) { return $this->done = false; } if (!$node->item(0)->parentNode) { return $this->done = false; } if ($node->item(0)->parentNode->lastChild->isSameNode($node->item(0)) === false) { return $this->done = false; } } if (isset($query['specifier'])) { $v = array_intersect_key($query['specifier'], $attr); if (isset($query['tag'])) { $ipt = explode('|', $query['tag']); if (!in_array($this->node->data('_name')->get(), $ipt)) { return $this->done = false; } } if (count($query['specifier']) > 0 and empty($v)) { return $this->done = false; } } //Find Has attr if (array_key_exists('id', $query['specifier'])) { if ($query['specifier']['id'] !== $v['id']) { return $this->done = false; } } if (array_key_exists('class', $query['specifier'])) { $classes = $v['class']; $hasclass = $this->node->hasClass($classes)->get(); if ($hasclass) { $v['class'] = $attr['class']; } } $this->matched = $v; $v = count(array_intersect($v, $attr)) >= count($query['specifier']); if ($v === false) { return $this->false = true; } if (!empty($query['attributes'])) { $found = false; $attrkeys = array_keys($attr); if (isset($query['attributes']['colExists'])) { $validAttr = array_intersect($query['attributes']['colExists'], $attrkeys); $found = $validAttr; $found = (count($validAttr) > 0 and count($validAttr) === count($query['attributes']['colExists'])); } if (isset($query['attributes']['colNotExists'])) { $validAttr = array_diff($query['attributes']['colNotExists'], $attrkeys); $found = (!empty($found) and $validAttr == $query['attributes']['colNotExists']); } $check = $query['attributes']; unset($check['colNotExists']); unset($check['colExists']); if (isset($check) and is_array($check) and !empty($check)) { $found = $this->matchAttributeSelectors($check, $attr); } if (!$found) { $this->done = false; return; } } if (!empty($query['filters'])) { $path = $this->node->data('_path')->get(); if (array_key_exists('empty', $query['filters'])) { $inner = $this->node->data('_innerHtml')->get(); if (!empty($inner)) { return $this->done = false; } } if (array_key_exists('parent', $query['filters'])) { if (isset($query['filters']['parent']) and !empty($query['filters']['parent'])) { $parents = explode('/', $path); array_pop($parents); $ps = strpos(end($parents), $query['filters']['parent']); if ($ps === false) { $this->done = false; } return; if ($ps !== 0) { $this->done = false; return; } } $inner = $this->node->data('_innerHtml')->get(); if (empty($inner)) { return $this->done = false; } if (strlen($inner) < 1) { $this->done = false; return; } $this->done = true; } if (strripos($path, ']') == strlen($path) - 1) { $path = substr($path, strripos($path, '[')); if (preg_match('/\\[(\\d)\\]/', $path, $match)) { $path = $match[1]; } else { $path = 1; } } else { $path = 1; } if (is_numeric($path)) { $i = (int) $path - 1; unset($path); if ($i < 0) { $this->done = false; return; } if (array_key_exists('eq', $query['filters']) and is_numeric($query['filters']['eq'])) { if ($query['filters']['eq'] != $i) { $this->done = false; return; } } if (array_key_exists('gt', $query['filters']) and is_numeric($query['filters']['gt'])) { if ($i > (int) $query['filters']['gt'] == false) { $this->done = false; return; } } if (array_key_exists('lt', $query['filters']) and is_numeric($query['filters']['lt'])) { if ($i < (int) $query['filters']['lt'] == false) { $this->done = false; return; } } if (array_key_exists('odd', $query['filters'])) { if (($i % 2 == 0) == true) { $this->done = false; return; } } if (array_key_exists('even', $query['filters'])) { if (($i % 2 == 0) == false) { $this->done = false; return; } } } } return $this->done = true; }