Symfony\Component\DomCrawler\Crawler::xpathLiteral PHP Method

xpathLiteral() public static method

Escaped characters are: quotes (") and apostrophe ('). Examples: echo Crawler::xpathLiteral('foo " bar'); prints 'foo " bar' echo Crawler::xpathLiteral("foo ' bar"); prints "foo ' bar" echo Crawler::xpathLiteral('a\'b"c'); prints concat('a', "'", 'b"c')
public static xpathLiteral ( string $s ) : string
$s string String to be escaped
return string Converted string
    public static function xpathLiteral($s)
    {
        if (false === strpos($s, "'")) {
            return sprintf("'%s'", $s);
        }
        if (false === strpos($s, '"')) {
            return sprintf('"%s"', $s);
        }
        $string = $s;
        $parts = array();
        while (true) {
            if (false !== ($pos = strpos($string, "'"))) {
                $parts[] = sprintf("'%s'", substr($string, 0, $pos));
                $parts[] = "\"'\"";
                $string = substr($string, $pos + 1);
            } else {
                $parts[] = "'{$string}'";
                break;
            }
        }
        return sprintf('concat(%s)', implode(', ', $parts));
    }

Usage Example

Exemplo n.º 1
0
Arquivo: Form.php Projeto: saj696/pipe
 /**
  * Adds form elements related to this form.
  *
  * Creates an internal copy of the submitted 'button' element and
  * the form node or the entire document depending on whether we need
  * to find non-descendant elements through HTML5 'form' attribute.
  */
 private function initialize()
 {
     $this->fields = new FormFieldRegistry();
     $xpath = new \DOMXPath($this->node->ownerDocument);
     // add submitted button if it has a valid name
     if ('form' !== $this->button->nodeName && $this->button->hasAttribute('name') && $this->button->getAttribute('name')) {
         if ('input' == $this->button->nodeName && 'image' == strtolower($this->button->getAttribute('type'))) {
             $name = $this->button->getAttribute('name');
             $this->button->setAttribute('value', '0');
             // temporarily change the name of the input node for the x coordinate
             $this->button->setAttribute('name', $name . '.x');
             $this->set(new Field\InputFormField($this->button));
             // temporarily change the name of the input node for the y coordinate
             $this->button->setAttribute('name', $name . '.y');
             $this->set(new Field\InputFormField($this->button));
             // restore the original name of the input node
             $this->button->setAttribute('name', $name);
         } else {
             $this->set(new Field\InputFormField($this->button));
         }
     }
     // find form elements corresponding to the current form
     if ($this->node->hasAttribute('id')) {
         // corresponding elements are either descendants or have a matching HTML5 form attribute
         $formId = Crawler::xpathLiteral($this->node->getAttribute('id'));
         $fieldNodes = $xpath->query(sprintf('descendant::input[@form=%s] | descendant::button[@form=%s] | descendant::textarea[@form=%s] | descendant::select[@form=%s] | //form[@id=%s]//input[not(@form)] | //form[@id=%s]//button[not(@form)] | //form[@id=%s]//textarea[not(@form)] | //form[@id=%s]//select[not(@form)]', $formId, $formId, $formId, $formId, $formId, $formId, $formId, $formId));
         foreach ($fieldNodes as $node) {
             $this->addField($node);
         }
     } else {
         // do the xpath query with $this->node as the context node, to only find descendant elements
         // however, descendant elements with form attribute are not part of this form
         $fieldNodes = $xpath->query('descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)]', $this->node);
         foreach ($fieldNodes as $node) {
             $this->addField($node);
         }
     }
     if ($this->baseHref && '' !== $this->node->getAttribute('action')) {
         $this->currentUri = $this->baseHref;
     }
 }
All Usage Examples Of Symfony\Component\DomCrawler\Crawler::xpathLiteral