Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
InlineInferredValues.php
001 <?php
002
003 /**
004 * @package s9e\TextFormatter
005 * @copyright Copyright (c) 2010-2022 The s9e authors
006 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
007 */
008 namespace s9e\TextFormatter\Configurator\TemplateNormalizations;
009
010 use DOMAttr;
011 use DOMElement;
012 use DOMNode;
013 use s9e\TextFormatter\Configurator\Helpers\AVTHelper;
014 use s9e\TextFormatter\Configurator\Helpers\XPathHelper;
015
016 /**
017 * Inline the text content of a node or the value of an attribute where it's known
018 *
019 * Will replace
020 * <xsl:if test="@foo='Foo'"><xsl:value-of select="@foo"/></xsl:if>
021 * with
022 * <xsl:if test="@foo='Foo'">Foo</xsl:if>
023 *
024 * This should be applied after control structures have been optimized
025 */
026 class InlineInferredValues extends AbstractNormalization
027 {
028 /**
029 * {@inheritdoc}
030 */
031 protected $queries = ['//xsl:if', '//xsl:when'];
032
033 /**
034 * {@inheritdoc}
035 */
036 protected function normalizeElement(DOMElement $element)
037 {
038 // Test whether the map has exactly one key and one value
039 $map = XPathHelper::parseEqualityExpr($element->getAttribute('test'));
040 if ($map === false || count($map) !== 1 || count($map[key($map)]) !== 1)
041 {
042 return;
043 }
044
045 $expr = key($map);
046 $value = end($map[$expr]);
047 $this->inlineInferredValue($element, $expr, $value);
048 }
049
050 /**
051 * Replace the inferred value in given node and its descendants
052 *
053 * @param DOMNode $node Context node
054 * @param string $expr XPath expression
055 * @param string $value Inferred value
056 * @return void
057 */
058 protected function inlineInferredValue(DOMNode $node, $expr, $value)
059 {
060 // Get xsl:value-of descendants that match the condition
061 $query = './/xsl:value-of[@select="' . $expr . '"]';
062 foreach ($this->xpath($query, $node) as $valueOf)
063 {
064 $this->replaceValueOf($valueOf, $value);
065 }
066
067 // Get all attributes from non-XSL elements that *could* match the condition
068 $query = './/*[namespace-uri() != $XSL]/@*[contains(., "{' . $expr . '}")]';
069 foreach ($this->xpath($query, $node) as $attribute)
070 {
071 $this->replaceAttribute($attribute, $expr, $value);
072 }
073 }
074
075 /**
076 * Replace an expression with a literal value in given attribute
077 *
078 * @param DOMAttr $attribute
079 * @param string $expr
080 * @param string $value
081 * @return void
082 */
083 protected function replaceAttribute(DOMAttr $attribute, $expr, $value)
084 {
085 AVTHelper::replace(
086 $attribute,
087 function ($token) use ($expr, $value)
088 {
089 // Test whether this expression is the one we're looking for
090 if ($token[0] === 'expression' && $token[1] === $expr)
091 {
092 // Replace the expression with the value (as a literal)
093 $token = ['literal', $value];
094 }
095
096 return $token;
097 }
098 );
099 }
100
101 /**
102 * Replace an xsl:value-of element with a literal value
103 *
104 * @param DOMElement $valueOf
105 * @param string $value
106 * @return void
107 */
108 protected function replaceValueOf(DOMElement $valueOf, $value)
109 {
110 $valueOf->parentNode->replaceChild($this->createText($value), $valueOf);
111 }
112 }