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