Example #1
0
 /**
  * {@inheritDoc}
  */
 public function toXPath()
 {
     $xpath = $this->selector->toXPath();
     $attrib = $this->xpathAttrib();
     $value = $this->value;
     switch ($this->operator) {
         case 'exists':
             $xpath->addCondition($attrib);
             break;
         case '=':
             $xpath->addCondition(sprintf('%s = %s', $attrib, XPath\Expression::xpathLiteral($value)));
             break;
         case '!=':
             // FIXME: this seems like a weird hack...
             if ($value) {
                 $xpath->addCondition(sprintf('not(%s) or %s != %s', $attrib, $attrib, XPath\Expression::xpathLiteral($value)));
             } else {
                 $xpath->addCondition(sprintf('%s != %s', $attrib, XPath\Expression::xpathLiteral($value)));
             }
             break;
         case '^=':
             $xpath->addCondition(sprintf('starts-with(%s, %s)', $attrib, XPath\Expression::xpathLiteral($value)));
             break;
         case '*=':
             // FIXME: case sensitive?
             $xpath->addCondition(sprintf('contains(%s, %s)', $attrib, XPath\Expression::xpathLiteral($value)));
             break;
         case '$=':
             // Oddly there is a starts-with in XPath 1.0, but not ends-with
             $value = XPath\Expression::xpathLiteral($value);
             $xpath->addCondition(sprintf('substring(%s, string-length(%s) - string-length(%s) + 1, string-length(%s)) = %s', $attrib, $attrib, $value, $value, $value));
             break;
         case '|=':
             // Weird, but true...
             $value = XPath\Expression::xpathLiteral($value);
             $xpath->addCondition(sprintf('%s = %s or starts-with(%s, concat(%s, "-"))', $attrib, $value, $attrib, $value));
             break;
         case '~=':
             $xpath->addCondition(sprintf("contains(concat(' ', normalize-space(%s), ' '), concat(' ', %s, ' '))", $attrib, XPath\Expression::xpathLiteral($value)));
             break;
         default:
             throw new ParseException(sprintf('Unknown operator: %s', $this->operator));
             break;
     }
     return $xpath;
 }
Example #2
0
 /**
  * {@inheritDoc}
  */
 public function toXPath()
 {
     $path = $this->selector->toXPath();
     $path->addCondition(sprintf('@id = %s', XPath\Expression::xpathLiteral($this->id)));
     return $path;
 }
Example #3
0
 /**
  * Adds a condition to this XPath expression using the name of the element
  * as the desired value.
  * This method resets the element to '*'.
  */
 public function addNameTest()
 {
     if ($this->element == '*') {
         // We weren't doing a test anyway
         return;
     }
     $this->addCondition(sprintf('name() = %s', Expression::xpathLiteral($this->element)));
     $this->element = '*';
 }
Example #4
0
 /**
  * undocumented function
  *
  * @param XPath\Expression $xpath The XPath expression
  * @return XPath\Expression The modified expression
  */
 protected function xpath_empty($xpath)
 {
     $xpath->addCondition('not(*) and not(normalize-space())');
     return $xpath;
 }
Example #5
0
 /**
  * undocumented function
  *
  * @param XPath\Expression $xpath
  * @param mixed $expr
  * @return XPath\Expression
  */
 protected function xpath_not(XPath\Expression $xpath, $expr)
 {
     // everything for which not expr applies
     if ($expr instanceof ElementSelector) {
         $xpath->addCondition(sprintf("not(name() = '%s')", $expr->toXpath()));
         return $xpath;
     }
     $expr = $expr->toXPath();
     $cond = $expr->getCondition();
     if ($cond) {
         // FIXME: should I do something about element_path?
         $xpath->addCondition(sprintf('not(%s)', $cond));
     } else {
         $xpath->addCondition('0');
     }
     return $xpath;
 }