1 | <?php |
||
49 | class HtmlDocumentFactory implements DocumentFactoryInterface |
||
50 | { |
||
51 | /** |
||
52 | * HTML5 elements |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected static $html5 = [ |
||
57 | 'a', |
||
58 | 'abbr', |
||
59 | 'acronym', |
||
60 | 'address', |
||
61 | 'applet', |
||
62 | 'area', |
||
63 | 'article', |
||
64 | 'aside', |
||
65 | 'audio', |
||
66 | 'b', |
||
67 | 'base', |
||
68 | 'basefont', |
||
69 | 'bdi', |
||
70 | 'bdo', |
||
71 | 'bgsound', |
||
72 | 'big', |
||
73 | 'blink', |
||
74 | 'blockquote', |
||
75 | 'body', |
||
76 | 'br', |
||
77 | 'button', |
||
78 | 'canvas', |
||
79 | 'caption', |
||
80 | 'center', |
||
81 | 'cite', |
||
82 | 'code', |
||
83 | 'col', |
||
84 | 'colgroup', |
||
85 | 'content', |
||
86 | 'data', |
||
87 | 'datalist', |
||
88 | 'dd', |
||
89 | 'decorator', |
||
90 | 'del', |
||
91 | 'details', |
||
92 | 'dfn', |
||
93 | 'dir', |
||
94 | 'div', |
||
95 | 'dl', |
||
96 | 'dt', |
||
97 | 'element', |
||
98 | 'em', |
||
99 | 'embed', |
||
100 | 'fieldset', |
||
101 | 'figcaption', |
||
102 | 'figure', |
||
103 | 'font', |
||
104 | 'footer', |
||
105 | 'form', |
||
106 | 'frame', |
||
107 | 'frameset', |
||
108 | 'h1', |
||
109 | 'h2', |
||
110 | 'h3', |
||
111 | 'h4', |
||
112 | 'h5', |
||
113 | 'h6', |
||
114 | 'head', |
||
115 | 'header', |
||
116 | 'hgroup', |
||
117 | 'hr', |
||
118 | 'html', |
||
119 | 'i', |
||
120 | 'iframe', |
||
121 | 'img', |
||
122 | 'input', |
||
123 | 'ins', |
||
124 | 'isindex', |
||
125 | 'kbd', |
||
126 | 'keygen', |
||
127 | 'label', |
||
128 | 'legend', |
||
129 | 'li', |
||
130 | 'link', |
||
131 | 'listing', |
||
132 | 'main', |
||
133 | 'map', |
||
134 | 'mark', |
||
135 | 'marquee', |
||
136 | 'menu', |
||
137 | 'menuitem', |
||
138 | 'meta', |
||
139 | 'meter', |
||
140 | 'nav', |
||
141 | 'nobr', |
||
142 | 'noframes', |
||
143 | 'noscript', |
||
144 | 'object', |
||
145 | 'ol', |
||
146 | 'optgroup', |
||
147 | 'option', |
||
148 | 'output', |
||
149 | 'p', |
||
150 | 'param', |
||
151 | 'plaintext', |
||
152 | 'pre', |
||
153 | 'progress', |
||
154 | 'q', |
||
155 | 'rp', |
||
156 | 'rt', |
||
157 | 'ruby', |
||
158 | 's', |
||
159 | 'samp', |
||
160 | 'script', |
||
161 | 'section', |
||
162 | 'select', |
||
163 | 'shadow', |
||
164 | 'small', |
||
165 | 'source', |
||
166 | 'spacer', |
||
167 | 'span', |
||
168 | 'strike', |
||
169 | 'strong', |
||
170 | 'style', |
||
171 | 'sub', |
||
172 | 'summary', |
||
173 | 'sup', |
||
174 | 'table', |
||
175 | 'tbody', |
||
176 | 'td', |
||
177 | 'template', |
||
178 | 'textarea', |
||
179 | 'tfoot', |
||
180 | 'th', |
||
181 | 'thead', |
||
182 | 'time', |
||
183 | 'title', |
||
184 | 'tr', |
||
185 | 'track', |
||
186 | 'tt', |
||
187 | 'u', |
||
188 | 'ul', |
||
189 | 'var', |
||
190 | 'video', |
||
191 | 'wbr', |
||
192 | 'xmp' |
||
193 | ]; |
||
194 | /** |
||
195 | * Custom HTML parsing error handler |
||
196 | * |
||
197 | * @var callable|null |
||
198 | */ |
||
199 | protected $errorHandler; |
||
200 | |||
201 | /** |
||
202 | * Constructor |
||
203 | * |
||
204 | * @param callable|null $errorHandler Custom HTML parsing error handler |
||
205 | */ |
||
206 | 18 | public function __construct(callable $errorHandler = null) |
|
210 | |||
211 | /** |
||
212 | * Create a DOM document from a source |
||
213 | * |
||
214 | * @param mixed $source Source |
||
215 | * @return \DOMDocument DOM document |
||
216 | */ |
||
217 | 18 | public function createDocumentFromSource($source) |
|
227 | |||
228 | /** |
||
229 | * Process parsing errors |
||
230 | * |
||
231 | * @param \LibXMLError[] $errors Parsing errors |
||
232 | * @throws InvalidArgumentException If it's not a HTML5 error |
||
233 | */ |
||
234 | 18 | protected function processParsingErrors(array $errors) |
|
243 | |||
244 | /** |
||
245 | * Test whether a parsing error is not because of an "invalid" HTML5 tag |
||
246 | * |
||
247 | * @param \LibXMLError $error Parsing error |
||
248 | * @return bool Error is not because of an "invalid" HTML5 tag |
||
249 | */ |
||
250 | 8 | protected function isNotInvalidHtml5TagError(\LibXMLError $error) |
|
258 | |||
259 | /** |
||
260 | * Test whether a parsing error is allowed per custom HTML parser error handler |
||
261 | * |
||
262 | * @param \LibXMLError $error Parsing error |
||
263 | * @return bool Error is not allowed |
||
264 | */ |
||
265 | 3 | protected function isNotAllowedError(\LibXMLError $error) |
|
269 | } |
||
270 |