[ Index ]

PHP Cross Reference of phpBB-3.3.12-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/TemplateNormalizations/ -> UninlineAttributes.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  
  14  /**
  15  * Uninline element attributes
  16  *
  17  * Will replace
  18  *     <a href="{@url}">...</a>
  19  * with
  20  *     <a><xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>...</a>
  21  */
  22  class UninlineAttributes extends AbstractNormalization
  23  {
  24      /**
  25      * {@inheritdoc}
  26      */
  27      protected $queries = ['//*[namespace-uri() != $XSL][@*]'];
  28  
  29      /**
  30      * {@inheritdoc}
  31      */
  32  	protected function normalizeElement(DOMElement $element)
  33      {
  34          // Using a document fragment improves performance with multiple attributes
  35          $fragment = $element->ownerDocument->createDocumentFragment();
  36          while ($element->attributes->length > 0)
  37          {
  38              $fragment->appendChild($this->uninlineAttribute($element->attributes->item(0)));
  39          }
  40          $element->insertBefore($fragment, $element->firstChild);
  41      }
  42  
  43      /**
  44      * Remove an attribute from its parent element and return its content as an xsl:attribute
  45      *
  46      * @param  DOMAttr    $attribute Attribute node
  47      * @return DOMElement            xsl:attribute element
  48      */
  49  	protected function uninlineAttribute(DOMAttr $attribute)
  50      {
  51          $xslAttribute = (strpos($attribute->value, '{') === false)
  52                        ? $this->uninlineStaticAttribute($attribute)
  53                        : $this->uninlineDynamicAttribute($attribute);
  54  
  55          // Set the xsl:attribute element's name
  56          $xslAttribute->setAttribute('name', $attribute->nodeName);
  57  
  58          // Remove the attribute from its parent element
  59          $attribute->parentNode->removeAttributeNode($attribute);
  60  
  61          return $xslAttribute;
  62      }
  63  
  64      /**
  65      * Uninline an AVT-style attribute
  66      *
  67      * @param  DOMAttr    $attribute Attribute node
  68      * @return DOMElement            xsl:attribute element
  69      */
  70  	protected function uninlineDynamicAttribute(DOMAttr $attribute)
  71      {
  72          $xslAttribute = $this->createElement('xsl:attribute');
  73  
  74          // Build the content of the xsl:attribute element
  75          foreach (AVTHelper::parse($attribute->value) as list($type, $content))
  76          {
  77              if ($type === 'expression')
  78              {
  79                  $childNode = $this->createElement('xsl:value-of');
  80                  $childNode->setAttribute('select', $content);
  81              }
  82              else
  83              {
  84                  $childNode = $this->createText($content);
  85              }
  86  
  87              $xslAttribute->appendChild($childNode);
  88          }
  89  
  90          return $xslAttribute;
  91      }
  92  
  93      /**
  94      * Uninline an attribute with a static value
  95      *
  96      * @param  DOMAttr    $attribute Attribute node
  97      * @return DOMElement            xsl:attribute element
  98      */
  99  	protected function uninlineStaticAttribute(DOMAttr $attribute)
 100      {
 101          return $this->createElement('xsl:attribute', str_replace('}}', '}', $attribute->value));
 102      }
 103  }


Generated: Sun Jun 23 12:25:44 2024 Cross-referenced by PHPXref 0.7.1