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
|
--TEST--
Bug #76738 Wrong handling of output buffer
--SKIPIF--
<?php
require_once('skipif.inc');
if (!extension_loaded('mbstring')) die('skip mbstring extension not available');
?>
--FILE--
<?php declare(strict_types=1);
$test_string = base64_decode('PGRpdiBjbGFzcz0idGlueW1jZS1nZW5lcmF0ZWQtcm9vdC1ibG9jayIgc3R5bGU9Im1hcmdpbjogMHB4OyBwYWRkaW5nOiAwcHg7Ij4KPGRpdiBzdHlsZT0iZmxvYXQ6IHJpZ2h0OyI+PGltZyBhbHQ9IkZvdG8gUmVpbmRsIEhhcmFsZCIgc3JjPSIvcnRlL3VwbG9hZC9mb3RvLmpwZyIgc3R5bGU9IndpZHRoOiAyNTBweDsgaGVpZ2h0OiAyNDlweDsiPjwvZGl2Pgo8ZGl2IHN0eWxlPSJ3aGl0ZS1zcGFjZTogbm93cmFwOyI+CjxwIHN0eWxlPSJtYXJnaW46IDBweDsiPlJlaW5kbCBIYXJhbGQmdXVtbDs8YnI+IFNpZWdmcmllZGdhc3NlIDIyLTI0LzIvNDxicj4gMTIxMCBXaWVuPG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k+CjxsaT5TdGFyZ2F0ZSwgQW5kcm9tZWRhLCBIaWdod2F5IFRvIEhlbGwsIFJlbmVnYWRlLCBOaWtpdGEsIFByb2ZpbGVyLCBDU0ksIERhcmsgQW5nZWwsIEJ1ZmZ5LCBDaGFybWVkPC9saT4KPC91bD4KPC9kaXY+CjwvZGl2Pgo=');
$after_load = (new rh_rte_helper_debug)->on_load($test_string);
var_dump($after_load === $test_string);
final class rh_rte_helper_debug
{
public $errors = [];
public function on_load(string $content): string
{
$content = utf8_encode($content);
$content = $this->add_outer_html($content);
$content = $this->remove_garbage($content);
$has_root_block = $this->has_root_block($content);
if(!$has_root_block)
{
$content = $this->add_root_block($content);
}
$content = $this->remove_outer_html($content);
$content = mb_convert_encoding($content, 'html-entities', 'UTF-8');
$content = utf8_decode($content);
return $content;
}
private function has_root_block(string $content): bool
{
$return = $content;
$doc = $this->get_dom($content);
if($doc != FALSE)
{
$xpath = ($doc != FALSE) ? new DomXpath($doc) : FALSE;
if($xpath != FALSE)
{
$path = '//*[contains(concat(" ", normalize-space(@class), " "), "tinymce-generated-root-block")]';
$nodes = $xpath->query($path);
$nodes_idx = ($nodes != FALSE) ? $nodes->length : 0;
if($nodes_idx > 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
private function add_root_block(string $content): string
{
$return = $content;
$doc = $this->get_dom($content);
if($doc != FALSE)
{
$xpath = ($doc != FALSE) ? new DomXpath($doc) : FALSE;
if($xpath != FALSE)
{
$path = '//*[contains(concat(" ", normalize-space(@class), " "), "tinymce-generated-root-block")]';
$nodes = $xpath->query($path);
$nodes_idx = ($nodes != FALSE) ? $nodes->length : 0;
if($nodes_idx == 0)
{
$root_div = $doc->createElement('div');
$root_div->setAttribute('class', 'tinymce-generated-root-block');
$root_div->setAttribute('style', 'margin: 0px; padding: 0px;');
$body = $doc->getElementsByTagName('body')->item(0);
if($body !== NULL)
{
while($body->childNodes->length > 0)
{
$root_div->appendChild($body->childNodes->item(0));
}
$body->appendChild($root_div);
$return = $doc->saveHTML();
}
}
}
}
return $return;
}
private function add_outer_html(string $content): string
{
return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Tidy</title></head><body>' . $content . '</body></html>';
}
private function remove_root_block(string $content): string
{
return $this->remove_block($content, 'tinymce-generated-root-block');
}
private function remove_garbage(string $content): string
{
return $this->remove_block($content, 'tinymce-garbage-root-block');
}
private function get_dom(string $html, bool $add_outer_html=TRUE): DOMDocument
{
$use_internal_errors = libxml_use_internal_errors(TRUE);
$dom = new DOMDocument;
$dom->resolveExternals = FALSE;
$dom->preserveWhiteSpace = TRUE;
$dom->strictErrorChecking = FALSE;
$dom->formatOutput = TRUE;
$dom->recover = TRUE;
$dom->validateOnParse = TRUE;
$dom->substituteEntities = FALSE;
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
$options = 0;
if(!$add_outer_html)
{
$options = LIBXML_HTML_NOIMPLIED;
}
$options = $options | LIBXML_HTML_NODEFDTD;
$loaded = @$dom->loadHTML('<?xml encoding="UTF-8">' . $html, $options);
if(!$loaded)
{
$dom = FALSE;
$this->errors = libxml_get_errors();
}
else
{
foreach($dom->childNodes as $item)
{
if($item->nodeType == XML_PI_NODE)
{
$dom->removeChild($item);
}
}
$dom->encoding = 'UTF-8';
}
libxml_clear_errors();
libxml_use_internal_errors($use_internal_errors);
return $dom;
}
private function remove_outer_html(string $content): string
{
$return = $content;
$doc = $this->get_dom($content);
if($doc !== FALSE)
{
if($doc->doctype !== NULL)
{
$doc->doctype->parentNode->removeChild($doc->doctype);
}
$html = $doc->getElementsByTagName('html')->item(0);
if($html !== NULL)
{
$fragment = $doc->createDocumentFragment();
while($html->childNodes->length > 0)
{
$childNode = $html->childNodes->item(0);
$fragment->appendChild($childNode);
}
$html->parentNode->replaceChild($fragment, $html);
}
$body = $doc->getElementsByTagName('body')->item(0);
if($body !== NULL)
{
$return = '';
$fragment = $doc->createDocumentFragment();
while($body->childNodes->length > 0)
{
$childNode = $body->childNodes->item(0);
$fragment->appendChild($childNode);
$return .= $doc->saveHTML($childNode);
}
$body->parentNode->replaceChild($fragment, $body);
}
else
{
$return = $doc->saveHTML();
}
}
return $return;
}
private function remove_block(string $content, string $class='tinymce-generated-root-block'): string
{
$return = $content;
$doc = $this->get_dom($content);
if($doc != FALSE)
{
$xpath = ($doc != FALSE) ? new DomXpath($doc) : FALSE;
if($xpath != FALSE)
{
$path = '//*[contains(concat(" ", normalize-space(@class), " "), "'.$class.'")]';
$nodes = $xpath->query($path);
$nodes_idx = ($nodes != FALSE) ? $nodes->length : 0;
if($nodes_idx > 0)
{
foreach($nodes as $node)
{
$fragment = $doc->createDocumentFragment();
while($node->childNodes->length > 0)
{
$childNode = $node->childNodes->item(0);
if($childNode->nodeType == XML_TEXT_NODE)
{
$fragment->appendChild($doc->createTextNode($childNode->nodeValue));
$childNode->parentNode->removeChild($childNode);
}
else
{
$fragment->appendChild($childNode);
}
}
$node->parentNode->replaceChild($fragment, $node);
}
$return = $doc->saveHTML();
}
}
}
return $return;
}
}
--EXPECT--
bool(true)
|