1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) 2010-2023 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 DOMComment; |
12
|
|
|
use DOMNode; |
13
|
|
|
use s9e\SweetDOM\Document; |
14
|
|
|
use s9e\SweetDOM\Element; |
15
|
|
|
|
16
|
|
|
abstract class AbstractNormalization |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* XSL namespace |
20
|
|
|
*/ |
21
|
|
|
const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ?Document Document that holds the template being normalized |
25
|
|
|
*/ |
26
|
|
|
protected ?Document $ownerDocument = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string[] XPath queries used to retrieve nodes of interest |
30
|
|
|
*/ |
31
|
|
|
protected array $queries = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Apply this normalization rule to given template |
35
|
|
|
* |
36
|
|
|
* @param Element $template <xsl:template/> node |
37
|
|
|
*/ |
38
|
|
|
public function normalize(Element $template): void |
39
|
|
|
{ |
40
|
|
|
$this->ownerDocument = $template->ownerDocument; |
41
|
|
|
foreach ($this->getNodes() as $node) |
42
|
|
|
{ |
43
|
|
|
$this->normalizeNode($node); |
44
|
|
|
} |
45
|
|
|
$this->reset(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create an xsl:text element or a text node in current template |
50
|
|
|
* |
51
|
|
|
* @param string $content |
52
|
|
|
* @return DOMNode |
53
|
|
|
*/ |
54
|
|
|
protected function createText(string $content): DOMNode |
55
|
|
|
{ |
56
|
|
|
return (trim($content) === '') |
57
|
|
|
? $this->ownerDocument->createXslText($content) |
58
|
|
|
: $this->ownerDocument->createTextNode($content); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create a text node in current template |
63
|
|
|
* |
64
|
|
|
* @param string $content |
65
|
|
|
* @return DOMText |
66
|
|
|
*/ |
67
|
|
|
protected function createTextNode($content): DOMText |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
return $this->ownerDocument->createTextNode($content); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Query and return a list of nodes of interest |
74
|
|
|
* |
75
|
|
|
* @return DOMNode[] |
76
|
|
|
*/ |
77
|
|
|
protected function getNodes(): array |
78
|
|
|
{ |
79
|
|
|
$query = implode(' | ', $this->queries); |
80
|
|
|
|
81
|
|
|
return ($query === '') ? [] : iterator_to_array($this->ownerDocument->query($query)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Test whether given node is an XSL element |
86
|
|
|
* |
87
|
|
|
* @param DOMNode $node |
88
|
|
|
* @param string $localName |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
protected function isXsl(DOMNode $node, $localName = null) |
92
|
|
|
{ |
93
|
|
|
return ($node->namespaceURI === self::XMLNS_XSL && (!isset($localName) || $localName === $node->localName)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Make an ASCII string lowercase |
98
|
|
|
* |
99
|
|
|
* @param string $str Original string |
100
|
|
|
* @return string Lowercased string |
101
|
|
|
*/ |
102
|
|
|
protected function lowercase($str) |
103
|
|
|
{ |
104
|
|
|
return strtr($str, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Normalize given attribute |
109
|
|
|
* |
110
|
|
|
* @param DOMAttr $attribute |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
protected function normalizeAttribute(DOMAttr $attribute) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Normalize given element |
119
|
|
|
*/ |
120
|
|
|
protected function normalizeElement(Element $element): void |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Normalize given node |
126
|
|
|
* |
127
|
|
|
* @param DOMNode $node |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
protected function normalizeNode(DOMNode $node) |
131
|
|
|
{ |
132
|
|
|
if (!$node->parentNode) |
133
|
|
|
{ |
134
|
|
|
// Ignore nodes that have been removed from the document |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
if ($node instanceof Element) |
138
|
|
|
{ |
139
|
|
|
$this->normalizeElement($node); |
140
|
|
|
} |
141
|
|
|
elseif ($node instanceof DOMAttr) |
142
|
|
|
{ |
143
|
|
|
$this->normalizeAttribute($node); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Reset this instance's properties after usage |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
protected function reset() |
153
|
|
|
{ |
154
|
|
|
$this->ownerDocument = null; |
155
|
|
|
} |
156
|
|
|
} |