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 |
AbstractNormalization.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 DOMComment;
012 use DOMElement;
013 use DOMNode;
014 use DOMXPath;
015
016 abstract class AbstractNormalization
017 {
018 /**
019 * XSL namespace
020 */
021 const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform';
022
023 /**
024 * @var DOMDocument Document that holds the template being normalized
025 */
026 protected $ownerDocument;
027
028 /**
029 * @var string[] XPath queries used to retrieve nodes of interest
030 */
031 protected $queries = [];
032
033 /**
034 * @var DOMXPath
035 */
036 protected $xpath;
037
038 /**
039 * Apply this normalization rule to given template
040 *
041 * @param DOMElement $template <xsl:template/> node
042 * @return void
043 */
044 public function normalize(DOMElement $template)
045 {
046 $this->ownerDocument = $template->ownerDocument;
047 $this->xpath = new DOMXPath($this->ownerDocument);
048 $this->xpath->registerNamespace('xsl', self::XMLNS_XSL);
049 foreach ($this->getNodes() as $node)
050 {
051 $this->normalizeNode($node);
052 }
053 $this->reset();
054 }
055
056 /**
057 * Create an element in current template
058 *
059 * @param string $nodeName
060 * @param string $textContent
061 * @return DOMElement
062 */
063 protected function createElement($nodeName, $textContent = '')
064 {
065 $methodName = 'createElement';
066 $args = [$nodeName];
067
068 // Add the text content for the new element
069 if ($textContent !== '')
070 {
071 $args[] = htmlspecialchars($textContent, ENT_NOQUOTES, 'UTF-8');
072 }
073
074 // Handle namespaced elements
075 $prefix = strstr($nodeName, ':', true);
076 if ($prefix > '')
077 {
078 $methodName .= 'NS';
079 array_unshift($args, $this->ownerDocument->lookupNamespaceURI($prefix));
080 }
081
082 return call_user_func_array([$this->ownerDocument, $methodName], $args);
083 }
084
085 /**
086 * Create an xsl:text element or a text node in current template
087 *
088 * @param string $content
089 * @return DOMNode
090 */
091 protected function createText($content)
092 {
093 return (trim($content) === '')
094 ? $this->createElement('xsl:text', $content)
095 : $this->ownerDocument->createTextNode($content);
096 }
097
098 /**
099 * Create a text node in current template
100 *
101 * @param string $content
102 * @return DOMText
103 */
104 protected function createTextNode($content)
105 {
106 return $this->ownerDocument->createTextNode($content);
107 }
108
109 /**
110 * Query and return a list of nodes of interest
111 *
112 * @return DOMNode[]
113 */
114 protected function getNodes()
115 {
116 $query = implode(' | ', $this->queries);
117
118 return ($query === '') ? [] : $this->xpath($query);
119 }
120
121 /**
122 * Test whether given node is an XSL element
123 *
124 * @param DOMNode $node
125 * @param string $localName
126 * @return bool
127 */
128 protected function isXsl(DOMNode $node, $localName = null)
129 {
130 return ($node->namespaceURI === self::XMLNS_XSL && (!isset($localName) || $localName === $node->localName));
131 }
132
133 /**
134 * Make an ASCII string lowercase
135 *
136 * @param string $str Original string
137 * @return string Lowercased string
138 */
139 protected function lowercase($str)
140 {
141 return strtr($str, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
142 }
143
144 /**
145 * Normalize given attribute
146 *
147 * @param DOMAttr $attribute
148 * @return void
149 */
150 protected function normalizeAttribute(DOMAttr $attribute)
151 {
152 }
153
154 /**
155 * Normalize given element
156 *
157 * @param DOMElement $element
158 * @return void
159 */
160 protected function normalizeElement(DOMElement $element)
161 {
162 }
163
164 /**
165 * Normalize given node
166 *
167 * @param DOMNode $node
168 * @return void
169 */
170 protected function normalizeNode(DOMNode $node)
171 {
172 if (!$node->parentNode)
173 {
174 // Ignore nodes that have been removed from the document
175 return;
176 }
177 if ($node instanceof DOMElement)
178 {
179 $this->normalizeElement($node);
180 }
181 elseif ($node instanceof DOMAttr)
182 {
183 $this->normalizeAttribute($node);
184 }
185 }
186
187 /**
188 * Reset this instance's properties after usage
189 *
190 * @return void
191 */
192 protected function reset()
193 {
194 $this->ownerDocument = null;
195 $this->xpath = null;
196 }
197
198 /**
199 * Evaluate given XPath expression
200 *
201 * For convenience, $XSL is replaced with the XSL namespace URI as a string
202 *
203 * @param string $query XPath query
204 * @param DOMNode $node Context node
205 * @return DOMNode[]
206 */
207 protected function xpath($query, DOMNode $node = null)
208 {
209 $query = str_replace('$XSL', '"' . self::XMLNS_XSL . '"', $query);
210
211 return iterator_to_array($this->xpath->query($query, $node));
212 }
213 }