[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/s9e/sweetdom/src/ -> Element.php (source)

   1  <?php declare(strict_types=1);
   2  
   3  /**
   4  * @package   s9e\SweetDOM
   5  * @copyright Copyright (c) 2019-2020 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\SweetDOM;
   9  
  10  use BadMethodCallException;
  11  use DOMElement;
  12  use DOMNode;
  13  use DOMNodeList;
  14  use InvalidArgumentException;
  15  
  16  /**
  17  * @method self appendXslApplyTemplates(string $select = null)
  18  * @method self appendXslApplyTemplatesSibling(string $select = null)
  19  * @method self appendXslAttribute(string $name, string $text = '')
  20  * @method self appendXslAttributeSibling(string $name, string $text = '')
  21  * @method self appendXslChoose()
  22  * @method self appendXslChooseSibling()
  23  * @method self appendXslComment(string $text = '')
  24  * @method self appendXslCommentSibling(string $text = '')
  25  * @method self appendXslCopyOf(string $select)
  26  * @method self appendXslCopyOfSibling(string $select)
  27  * @method self appendXslIf(string $test, string $text = '')
  28  * @method self appendXslIfSibling(string $test, string $text = '')
  29  * @method self appendXslOtherwise(string $text = '')
  30  * @method self appendXslOtherwiseSibling(string $text = '')
  31  * @method self appendXslText(string $text = '')
  32  * @method self appendXslTextSibling(string $text = '')
  33  * @method self appendXslValueOf(string $select)
  34  * @method self appendXslValueOfSibling(string $select)
  35  * @method self appendXslVariable(string $name, string $select = null)
  36  * @method self appendXslVariableSibling(string $name, string $select = null)
  37  * @method self appendXslWhen(string $test, string $text = '')
  38  * @method self appendXslWhenSibling(string $test, string $text = '')
  39  * @method self prependXslApplyTemplates(string $select = null)
  40  * @method self prependXslApplyTemplatesSibling(string $select = null)
  41  * @method self prependXslAttribute(string $name, string $text = '')
  42  * @method self prependXslAttributeSibling(string $name, string $text = '')
  43  * @method self prependXslChoose()
  44  * @method self prependXslChooseSibling()
  45  * @method self prependXslComment(string $text = '')
  46  * @method self prependXslCommentSibling(string $text = '')
  47  * @method self prependXslCopyOf(string $select)
  48  * @method self prependXslCopyOfSibling(string $select)
  49  * @method self prependXslIf(string $test, string $text = '')
  50  * @method self prependXslIfSibling(string $test, string $text = '')
  51  * @method self prependXslOtherwise(string $text = '')
  52  * @method self prependXslOtherwiseSibling(string $text = '')
  53  * @method self prependXslText(string $text = '')
  54  * @method self prependXslTextSibling(string $text = '')
  55  * @method self prependXslValueOf(string $select)
  56  * @method self prependXslValueOfSibling(string $select)
  57  * @method self prependXslVariable(string $name, string $select = null)
  58  * @method self prependXslVariableSibling(string $name, string $select = null)
  59  * @method self prependXslWhen(string $test, string $text = '')
  60  * @method self prependXslWhenSibling(string $test, string $text = '')
  61  */
  62  class Element extends DOMElement
  63  {
  64  	public function __call(string $name, array $arguments)
  65      {
  66          if (preg_match('(^(append|prepend)(xsl\\w+?)(sibling|)$)', strtolower($name), $m))
  67          {
  68              $callback = [$this->ownerDocument, 'create' . $m[2]];
  69              if (is_callable($callback))
  70              {
  71                  $element = call_user_func_array($callback, $arguments);
  72                  $where   = [
  73                      'append'         => 'beforeend',
  74                      'appendsibling'  => 'afterend',
  75                      'prepend'        => 'afterbegin',
  76                      'prependsibling' => 'beforebegin'
  77                  ];
  78  
  79                  return $this->insertAdjacentElement($where[$m[1] . $m[3]], $element);
  80              }
  81          }
  82  
  83          throw new BadMethodCallException;
  84      }
  85  
  86      /**
  87      * Evaluate and return the result of a given XPath expression using this element as context node
  88      *
  89      * @param  string  $expr XPath expression
  90      * @return mixed
  91      */
  92  	public function evaluate(string $expr)
  93      {
  94          return $this->ownerDocument->evaluate($expr, $this);
  95      }
  96  
  97      /**
  98      * Evaluate and return the first element of a given XPath query using this element as context node
  99      *
 100      * @param  string       $expr XPath expression
 101      * @return DOMNode|null
 102      */
 103  	public function firstOf(string $expr): ?DOMNode
 104      {
 105          return $this->ownerDocument->firstOf($expr, $this);
 106      }
 107  
 108      /**
 109      * Insert given element relative to this element's position
 110      *
 111      * @param  string $where   One of 'beforebegin', 'afterbegin', 'beforeend', 'afterend'
 112      * @param  self   $element
 113      * @return self
 114      */
 115  	public function insertAdjacentElement(string $where, self $element): self
 116      {
 117          $this->insertAdjacentNode($where, $element);
 118  
 119          return $element;
 120      }
 121  
 122      /**
 123      * Insert given text relative to this element's position
 124      *
 125      * @param  string $where One of 'beforebegin', 'afterbegin', 'beforeend', 'afterend'
 126      * @param  string $text
 127      * @return void
 128      */
 129  	public function insertAdjacentText(string $where, string $text): void
 130      {
 131          $this->insertAdjacentXML($where, htmlspecialchars($text, ENT_XML1));
 132      }
 133  
 134      /**
 135      * Insert given XML relative to this element's position
 136      *
 137      * @param  string $where One of 'beforebegin', 'afterbegin', 'beforeend', 'afterend'
 138      * @param  string $xml
 139      * @return void
 140      */
 141  	public function insertAdjacentXML(string $where, string $xml): void
 142      {
 143          $fragment = $this->ownerDocument->createDocumentFragment();
 144          $fragment->appendXML($this->addMissingNamespaceDeclarations($xml));
 145  
 146          $this->insertAdjacentNode($where, $fragment);
 147      }
 148  
 149      /**
 150      * Evaluate and return the result of a given XPath query using this element as context node
 151      *
 152      * @param  string      $expr XPath expression
 153      * @return DOMNodeList
 154      */
 155  	public function query(string $expr): DOMNodeList
 156      {
 157          return $this->ownerDocument->query($expr, $this);
 158      }
 159  
 160      /**
 161      * Remove this element from the document
 162      *
 163      * @return void
 164      */
 165  	public function remove(): void
 166      {
 167          $this->parentNode->removeChild($this);
 168      }
 169  
 170      /**
 171      * Replace this element with given nodes/text
 172      *
 173      * @param  DOMNode|string $nodes
 174      * @return void
 175      */
 176  	public function replaceWith(...$nodes): void
 177      {
 178          foreach ($nodes as $node)
 179          {
 180              if (!($node instanceof DOMNode))
 181              {
 182                  $node = $this->ownerDocument->createTextNode((string) $node);
 183              }
 184              $this->parentNode->insertBefore($node, $this);
 185          }
 186          $this->parentNode->removeChild($this);
 187      }
 188  
 189      /**
 190      * Add namespace declarations that may be missing in given XML
 191      *
 192      * @param  string $xml Original XML
 193      * @return string      Modified XML
 194      */
 195  	protected function addMissingNamespaceDeclarations(string $xml): string
 196      {
 197          preg_match_all('(xmlns:\\K[-\\w]++(?==))', $xml, $m);
 198          $prefixes = array_flip($m[0]);
 199  
 200          return preg_replace_callback(
 201              '(<([-\\w]++):[^>]*?\\K\\s*/?>)',
 202              function ($m) use ($prefixes)
 203              {
 204                  $return = $m[0];
 205                  $prefix = $m[1];
 206                  if (!isset($prefixes[$prefix]))
 207                  {
 208                      $nsURI  = $this->lookupNamespaceURI($prefix);
 209                      $return = ' xmlns:' . $prefix . '="' . htmlspecialchars($nsURI, ENT_XML1) . '"' . $return;
 210                  }
 211  
 212                  return $return;
 213              },
 214              $xml
 215          );
 216      }
 217  
 218      /**
 219      * Insert given node relative to this element's position
 220      *
 221      * @param  string  $where One of 'beforebegin', 'afterbegin', 'beforeend', 'afterend'
 222      * @param  DOMNode $node
 223      * @return void
 224      */
 225  	protected function insertAdjacentNode(string $where, DOMNode $node): void
 226      {
 227          $where = strtolower($where);
 228          if ($where === 'beforebegin')
 229          {
 230              $this->parentNode->insertBefore($node, $this);
 231          }
 232          elseif ($where === 'beforeend')
 233          {
 234              $this->appendChild($node);
 235          }
 236          elseif ($where === 'afterend')
 237          {
 238              $this->parentNode->insertBefore($node, $this->nextSibling);
 239          }
 240          elseif ($where === 'afterbegin')
 241          {
 242              $this->insertBefore($node, $this->firstChild);
 243          }
 244          else
 245          {
 246              throw new InvalidArgumentException;
 247          }
 248      }
 249  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1