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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
<?php
/**
* Performs transformations of HTML by wrapping around libxml2 and working
* around its countless bugs.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
class HtmlFormatter {
/**
* @var DOMDocument
*/
private $doc;
private $html;
private $itemsToRemove = array();
private $elementsToFlatten = array();
protected $removeMedia = false;
/**
* Constructor
*
* @param string $html: Text to process
*/
public function __construct( $html ) {
$this->html = $html;
}
/**
* Turns a chunk of HTML into a proper document
* @param string $html
* @return string
*/
public static function wrapHTML( $html ) {
return '<!doctype html><html><head></head><body>' . $html . '</body></html>';
}
/**
* Override this in descendant class to modify HTML after it has been converted from DOM tree
* @param string $html: HTML to process
* @return string: Processed HTML
*/
protected function onHtmlReady( $html ) {
return $html;
}
/**
* @return DOMDocument: DOM to manipulate
*/
public function getDoc() {
if ( !$this->doc ) {
$html = mb_convert_encoding( $this->html, 'HTML-ENTITIES', 'UTF-8' );
// Workaround for bug that caused spaces before references
// to disappear during processing:
// https://bugzilla.wikimedia.org/show_bug.cgi?id=53086
//
// Please replace with a better fix if one can be found.
$html = str_replace( ' <', ' <', $html );
libxml_use_internal_errors( true );
$loader = libxml_disable_entity_loader();
$this->doc = new DOMDocument();
$this->doc->strictErrorChecking = false;
$this->doc->loadHTML( $html );
libxml_disable_entity_loader( $loader );
libxml_use_internal_errors( false );
$this->doc->encoding = 'UTF-8';
}
return $this->doc;
}
/**
* Sets whether images/videos/sounds should be removed from output
* @param bool $flag
*/
public function setRemoveMedia( $flag = true ) {
$this->removeMedia = $flag;
}
/**
* Adds one or more selector of content to remove. A subset of CSS selector
* syntax is supported:
*
* <tag>
* <tag>.class
* .<class>
* #<id>
*
* @param Array|string $selectors: Selector(s) of stuff to remove
*/
public function remove( $selectors ) {
$this->itemsToRemove = array_merge( $this->itemsToRemove, (array)$selectors );
}
/**
* Adds one or more element name to the list to flatten (remove tag, but not its content)
* Can accept undelimited regexes
*
* Note this interface may fail in surprising unexpected ways due to usage of regexes,
* so should not be relied on for HTML markup security measures.
*
* @param Array|string $elements: Name(s) of tag(s) to flatten
*/
public function flatten( $elements ) {
$this->elementsToFlatten = array_merge( $this->elementsToFlatten, (array)$elements );
}
/**
* Instructs the formatter to flatten all tags
*/
public function flattenAllTags() {
$this->flatten( '[?!]?[a-z0-9]+' );
}
/**
* Removes content we've chosen to remove
*/
public function filterContent() {
wfProfileIn( __METHOD__ );
$removals = $this->parseItemsToRemove();
if ( !$removals ) {
wfProfileOut( __METHOD__ );
return;
}
$doc = $this->getDoc();
// Remove tags
// You can't remove DOMNodes from a DOMNodeList as you're iterating
// over them in a foreach loop. It will seemingly leave the internal
// iterator on the foreach out of wack and results will be quite
// strange. Though, making a queue of items to remove seems to work.
$domElemsToRemove = array();
foreach ( $removals['TAG'] as $tagToRemove ) {
$tagToRemoveNodes = $doc->getElementsByTagName( $tagToRemove );
foreach ( $tagToRemoveNodes as $tagToRemoveNode ) {
if ( $tagToRemoveNode ) {
$domElemsToRemove[] = $tagToRemoveNode;
}
}
}
$this->removeElements( $domElemsToRemove );
// Elements with named IDs
$domElemsToRemove = array();
foreach ( $removals['ID'] as $itemToRemove ) {
$itemToRemoveNode = $doc->getElementById( $itemToRemove );
if ( $itemToRemoveNode ) {
$domElemsToRemove[] = $itemToRemoveNode;
}
}
$this->removeElements( $domElemsToRemove );
// CSS Classes
$domElemsToRemove = array();
$xpath = new DOMXpath( $doc );
foreach ( $removals['CLASS'] as $classToRemove ) {
$elements = $xpath->query( '//*[contains(@class, "' . $classToRemove . '")]' );
/** @var $element DOMElement */
foreach ( $elements as $element ) {
$classes = $element->getAttribute( 'class' );
if ( preg_match( "/\b$classToRemove\b/", $classes ) && $element->parentNode ) {
$domElemsToRemove[] = $element;
}
}
}
$this->removeElements( $domElemsToRemove );
// Tags with CSS Classes
foreach ( $removals['TAG_CLASS'] as $classToRemove ) {
$parts = explode( '.', $classToRemove );
$elements = $xpath->query(
'//' . $parts[0] . '[@class="' . $parts[1] . '"]'
);
$this->removeElements( $elements );
}
wfProfileOut( __METHOD__ );
}
/**
* Removes a list of elelments from DOMDocument
* @param array|DOMNodeList $elements
*/
private function removeElements( $elements ) {
$list = $elements;
if ( $elements instanceof DOMNodeList ) {
$list = array();
foreach ( $elements as $element ) {
$list[] = $element;
}
}
/** @var $element DOMElement */
foreach ( $list as $element ) {
if ( $element->parentNode ) {
$element->parentNode->removeChild( $element );
}
}
}
/**
* libxml in its usual pointlessness converts many chars to entities - this function
* perfoms a reverse conversion
* @param string $html
* @return string
*/
private function fixLibXML( $html ) {
wfProfileIn( __METHOD__ );
static $replacements;
if ( ! $replacements ) {
// We don't include rules like '"' => '&quot;' because entities had already been
// normalized by libxml. Using this function with input not sanitized by libxml is UNSAFE!
$replacements = new ReplacementArray( array(
'"' => '&quot;',
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;',
) );
}
$html = $replacements->replace( $html );
$html = mb_convert_encoding( $html, 'UTF-8', 'HTML-ENTITIES' );
wfProfileOut( __METHOD__ );
return $html;
}
/**
* Performs final transformations and returns resulting HTML
*
* @param DOMElement|string|null $element: ID of element to get HTML from or false to get it from the whole tree
* @return string: Processed HTML
*/
public function getText( $element = null ) {
wfProfileIn( __METHOD__ );
if ( $this->doc ) {
if ( $element !== null && !( $element instanceof DOMElement ) ) {
$element = $this->doc->getElementById( $element );
}
if ( $element ) {
$body = $this->doc->getElementsByTagName( 'body' )->item( 0 );
$nodesArray = array();
foreach ( $body->childNodes as $node ) {
$nodesArray[] = $node;
}
foreach ( $nodesArray as $nodeArray ) {
$body->removeChild( $nodeArray );
}
$body->appendChild( $element );
}
$html = $this->doc->saveHTML();
$html = $this->fixLibXml( $html );
} else {
$html = $this->html;
}
if ( wfIsWindows() ) {
// Appears to be cleanup for CRLF misprocessing of unknown origin
// when running server on Windows platform.
//
// If this error continues in the future, please track it down in the
// XML code paths if possible and fix there.
$html = str_replace( ' ', '', $html );
}
$html = preg_replace( '/<!--.*?-->|^.*?<body>|<\/body>.*$/s', '', $html );
$html = $this->onHtmlReady( $html );
if ( $this->elementsToFlatten ) {
$elements = implode( '|', $this->elementsToFlatten );
$html = preg_replace( "#</?($elements)\\b[^>]*>#is", '', $html );
}
wfProfileOut( __METHOD__ );
return $html;
}
/**
* @param $selector: CSS selector to parse
* @param $type
* @param $rawName
* @return bool: Whether the selector was successfully recognised
*/
protected function parseSelector( $selector, &$type, &$rawName ) {
if ( strpos( $selector, '.' ) === 0 ) {
$type = 'CLASS';
$rawName = substr( $selector, 1 );
} elseif ( strpos( $selector, '#' ) === 0 ) {
$type = 'ID';
$rawName = substr( $selector, 1 );
} elseif ( strpos( $selector, '.' ) !== 0 &&
strpos( $selector, '.' ) !== false )
{
$type = 'TAG_CLASS';
$rawName = $selector;
} elseif ( strpos( $selector, '[' ) === false
&& strpos( $selector, ']' ) === false )
{
$type = 'TAG';
$rawName = $selector;
} else {
throw new MWException( __METHOD__ . "(): unrecognized selector '$selector'" );
}
return true;
}
/**
* Transforms CSS selectors into an internal representation suitable for processing
* @return array
*/
protected function parseItemsToRemove() {
wfProfileIn( __METHOD__ );
$removals = array(
'ID' => array(),
'TAG' => array(),
'CLASS' => array(),
'TAG_CLASS' => array(),
);
foreach ( $this->itemsToRemove as $itemToRemove ) {
$type = '';
$rawName = '';
if ( $this->parseSelector( $itemToRemove, $type, $rawName ) ) {
$removals[$type][] = $rawName;
}
}
if ( $this->removeMedia ) {
$removals['TAG'][] = 'img';
$removals['TAG'][] = 'audio';
$removals['TAG'][] = 'video';
}
wfProfileOut( __METHOD__ );
return $removals;
}
}
|