Пример #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;
 }
Пример #2
0
 /**
  * {@inheritDoc}
  */
 public function toXPath()
 {
     $path = $this->selector->toXPath();
     $path->addCondition(sprintf('@id = %s', XPath\Expression::xpathLiteral($this->id)));
     return $path;
 }
Пример #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 = '*';
 }
Пример #4
0
 /**
  * undocumented function
  *
  * @param XPath\Expression $xpath
  * @param mixed $expr
  * @return XPath\Expression
  */
 protected function xpath_contains(XPath\Expression $xpath, $expr)
 {
     // text content, minus tags, must contain expr
     if ($expr instanceof ElementSelector) {
         $expr = $expr->getCssText();
     }
     // FIXME: lower-case is only available with XPath 2
     //$xpath->addCondition(sprintf('contains(lower-case(string(.)), %s)', XPath\Expression::xpathLiteral(strtolower($expr))));
     $xpath->addCondition(sprintf('contains(string(.), %s)', XPath\Expression::xpathLiteral($expr)));
     return $xpath;
 }