-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathPHP.php
325 lines (286 loc) · 6.65 KB
/
PHP.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* @package s9e\TextFormatter
* @copyright Copyright (c) The s9e authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Renderers;
use DOMNode;
use DOMXPath;
use RuntimeException;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Utils\XPath;
abstract class PHP extends Renderer
{
/**
* @var array[] Stack of dictionaries used by the Quick renderer [[attrName => attrValue]]
*/
protected $attributes;
/**
* @var array Dictionary of replacements used by the Quick renderer [id => [match, replace]]
*/
protected $dynamic;
/**
* @var bool Whether to enable the Quick renderer
*/
public $enableQuickRenderer = false;
/**
* @var string Renderer's output
*/
protected $out;
/**
* @var string Regexp that matches XML elements to be rendered by the quick renderer
*/
protected $quickRegexp = '((?!))';
/**
* @var string Regexp that matches nodes that SHOULD NOT be rendered by the quick renderer
*/
protected $quickRenderingTest = '((?<=<)[!?])';
/**
* @var array Dictionary of static replacements used by the Quick renderer [id => replacement]
*/
protected $static;
/**
* @var DOMXPath XPath object used to query the document being rendered
*/
protected $xpath;
/**
* Render given DOMNode
*
* @param DOMNode $node
* @return void
*/
abstract protected function renderNode(DOMNode $node);
public function __sleep()
{
return ['enableQuickRenderer', 'params'];
}
/**
* Render the content of given node
*
* Matches the behaviour of an xsl:apply-templates element
*
* @param DOMNode $root Context node
* @param string $query XPath query used to filter which child nodes to render
* @return void
*/
protected function at(DOMNode $root, $query = null)
{
if ($root->nodeType === XML_TEXT_NODE)
{
// Text nodes are outputted directly
$this->out .= htmlspecialchars($root->textContent, ENT_NOQUOTES);
}
else
{
$nodes = (isset($query)) ? $this->xpath->query($query, $root) : $root->childNodes;
foreach ($nodes as $node)
{
$this->renderNode($node);
}
}
}
/**
* Test whether given XML can be rendered with the Quick renderer
*
* @param string $xml
* @return bool
*/
protected function canQuickRender($xml)
{
return ($this->enableQuickRenderer && !preg_match($this->quickRenderingTest, $xml) && substr($xml, -4) === '</r>');
}
/**
* Ensure that a tag pair does not contain a start tag of itself
*
* Detects malformed matches such as <X><X></X>
*
* @param string $id
* @param string $xml
* @return void
*/
protected function checkTagPairContent($id, $xml)
{
if (strpos($xml, '<' . $id, 1) !== false)
{
throw new RuntimeException;
}
}
/**
* Return a parameter's value as an XPath expression
*
* @param string $paramName
* @return string
*/
protected function getParamAsXPath($paramName)
{
return (isset($this->params[$paramName])) ? XPath::export($this->params[$paramName]) : "''";
}
/**
* Extract the text content from given XML
*
* NOTE: numeric character entities are decoded beforehand, we don't need to decode them here
*
* @param string $xml Original XML
* @return string Text content, with special characters decoded
*/
protected function getQuickTextContent($xml)
{
return htmlspecialchars_decode(strip_tags($xml));
}
/**
* Test whether given array has any non-null values
*
* @param array $array
* @return bool
*/
protected function hasNonNullValues(array $array)
{
foreach ($array as $v)
{
if (isset($v))
{
return true;
}
}
return false;
}
/**
* Capture and return the attributes of an XML element
*
* NOTE: XML character entities are left as-is
*
* @param string $xml Element in XML form
* @return array Dictionary of [attrName => attrValue]
*/
protected function matchAttributes($xml)
{
if (strpos($xml, '="') === false)
{
return [];
}
// Match all name-value pairs until the first right bracket
preg_match_all('(([^ =]++)="([^"]*))S', substr($xml, 0, strpos($xml, '>')), $m);
return array_combine($m[1], $m[2]);
}
/**
* Render an intermediate representation using the Quick renderer
*
* @param string $xml Intermediate representation
* @return string
*/
protected function renderQuick($xml)
{
$this->attributes = [];
$xml = $this->decodeSMP($xml);
$html = preg_replace_callback(
$this->quickRegexp,
$this->renderQuickCallback(...),
substr($xml, 1 + strpos($xml, '>'), -4)
);
return str_replace('<br/>', '<br>', $html);
}
/**
* Render a string matched by the Quick renderer
*
* This stub should be overwritten by generated renderers
*
* @param string[] $m
* @return string
*/
protected function renderQuickCallback(array $m)
{
if (isset($m[3]))
{
return $this->renderQuickSelfClosingTag($m);
}
if (isset($m[2]))
{
// Single tag
$id = $m[2];
}
else
{
// Tag pair
$id = $m[1];
$this->checkTagPairContent($id, $m[0]);
}
if (isset($this->static[$id]))
{
return $this->static[$id];
}
if (isset($this->dynamic[$id]))
{
return preg_replace($this->dynamic[$id][0], $this->dynamic[$id][1], $m[0], 1);
}
return $this->renderQuickTemplate($id, $m[0]);
}
/**
* Render a self-closing tag using the Quick renderer
*
* @param string[] $m
* @return string
*/
protected function renderQuickSelfClosingTag(array $m)
{
unset($m[3]);
$m[0] = substr($m[0], 0, -2) . '>';
$html = $this->renderQuickCallback($m);
$m[0] = '</' . $m[2] . '>';
$m[2] = '/' . $m[2];
$html .= $this->renderQuickCallback($m);
return $html;
}
/**
* Render a string matched by the Quick renderer using a generated PHP template
*
* This stub should be overwritten by generated renderers
*
* @param integer $id Tag's ID (tag name optionally preceded by a slash)
* @param string $xml Tag's XML or tag pair's XML including their content
* @return string Rendered template
*/
protected function renderQuickTemplate($id, $xml)
{
throw new RuntimeException('Not implemented');
}
/**
* {@inheritdoc}
*/
protected function renderRichText($xml)
{
$this->setLocale();
try
{
if ($this->canQuickRender($xml))
{
$html = $this->renderQuick($xml);
$this->restoreLocale();
return $html;
}
}
catch (RuntimeException $e)
{
// Do nothing
}
$dom = $this->loadXML($xml);
$this->out = '';
$this->xpath = new DOMXPath($dom);
$this->at($dom->documentElement);
$html = $this->out;
$this->reset();
$this->restoreLocale();
return $html;
}
/**
* Reset object properties that are populated during rendering
*
* @return void
*/
protected function reset()
{
unset($this->attributes);
unset($this->out);
unset($this->xpath);
}
}