[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/TemplateNormalizations/ -> NormalizeUrls.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\TemplateNormalizations;
   9  
  10  use DOMAttr;
  11  use DOMElement;
  12  use s9e\TextFormatter\Configurator\Helpers\AVTHelper;
  13  use s9e\TextFormatter\Configurator\Helpers\NodeLocator;
  14  use s9e\TextFormatter\Parser\AttributeFilters\UrlFilter;
  15  
  16  /**
  17  * @link http://dev.w3.org/html5/spec/links.html#attr-hyperlink-href
  18  */
  19  class NormalizeUrls extends AbstractNormalization
  20  {
  21      /**
  22      * {@inheritdoc}
  23      */
  24  	protected function getNodes()
  25      {
  26          return NodeLocator::getURLNodes($this->ownerDocument);
  27      }
  28  
  29      /**
  30      * {@inheritdoc}
  31      */
  32  	protected function normalizeAttribute(DOMAttr $attribute)
  33      {
  34          // Trim the URL and parse it
  35          $tokens = AVTHelper::parse(trim($attribute->value));
  36  
  37          $attrValue = '';
  38          foreach ($tokens as list($type, $content))
  39          {
  40              if ($type === 'literal')
  41              {
  42                  $attrValue .= UrlFilter::sanitizeUrl($content);
  43              }
  44              else
  45              {
  46                  $attrValue .= '{' . $content . '}';
  47              }
  48          }
  49  
  50          // Unescape brackets in the host part
  51          $attrValue = $this->unescapeBrackets($attrValue);
  52  
  53          // Update the attribute's value
  54          $attribute->value = htmlspecialchars($attrValue, ENT_COMPAT);
  55      }
  56  
  57      /**
  58      * {@inheritdoc}
  59      */
  60  	protected function normalizeElement(DOMElement $element)
  61      {
  62          $query = './/text()[normalize-space() != ""]';
  63          foreach ($this->xpath($query, $element) as $i => $node)
  64          {
  65              $value = UrlFilter::sanitizeUrl($node->nodeValue);
  66  
  67              if (!$i)
  68              {
  69                  $value = $this->unescapeBrackets(ltrim($value));
  70              }
  71  
  72              $node->nodeValue = $value;
  73          }
  74          if (isset($node))
  75          {
  76              $node->nodeValue = rtrim($node->nodeValue);
  77          }
  78      }
  79  
  80      /**
  81      * Unescape brackets in the host part of a URL if it looks like an IPv6 address
  82      *
  83      * @param  string $url
  84      * @return string
  85      */
  86  	protected function unescapeBrackets($url)
  87      {
  88          return preg_replace('#^(\\w+://)%5B([-\\w:._%]+)%5D#i', '$1[$2]', $url);
  89      }
  90  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1