Chris@0
|
1 <?php
|
Chris@0
|
2 namespace Masterminds\HTML5\Parser;
|
Chris@0
|
3
|
Chris@0
|
4 use Masterminds\HTML5\Elements;
|
Chris@0
|
5
|
Chris@0
|
6 /**
|
Chris@0
|
7 * Create an HTML5 DOM tree from events.
|
Chris@0
|
8 *
|
Chris@0
|
9 * This attempts to create a DOM from events emitted by a parser. This
|
Chris@0
|
10 * attempts (but does not guarantee) to up-convert older HTML documents
|
Chris@0
|
11 * to HTML5. It does this by applying HTML5's rules, but it will not
|
Chris@0
|
12 * change the architecture of the document itself.
|
Chris@0
|
13 *
|
Chris@0
|
14 * Many of the error correction and quirks features suggested in the specification
|
Chris@0
|
15 * are implemented herein; however, not all of them are. Since we do not
|
Chris@0
|
16 * assume a graphical user agent, no presentation-specific logic is conducted
|
Chris@0
|
17 * during tree building.
|
Chris@0
|
18 *
|
Chris@0
|
19 * FIXME: The present tree builder does not exactly follow the state machine rules
|
Chris@0
|
20 * for insert modes as outlined in the HTML5 spec. The processor needs to be
|
Chris@0
|
21 * re-written to accomodate this. See, for example, the Go language HTML5
|
Chris@0
|
22 * parser.
|
Chris@0
|
23 */
|
Chris@0
|
24 class DOMTreeBuilder implements EventHandler
|
Chris@0
|
25 {
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Defined in http://www.w3.org/TR/html51/infrastructure.html#html-namespace-0
|
Chris@0
|
28 */
|
Chris@0
|
29 const NAMESPACE_HTML = 'http://www.w3.org/1999/xhtml';
|
Chris@0
|
30
|
Chris@0
|
31 const NAMESPACE_MATHML = 'http://www.w3.org/1998/Math/MathML';
|
Chris@0
|
32
|
Chris@0
|
33 const NAMESPACE_SVG = 'http://www.w3.org/2000/svg';
|
Chris@0
|
34
|
Chris@0
|
35 const NAMESPACE_XLINK = 'http://www.w3.org/1999/xlink';
|
Chris@0
|
36
|
Chris@0
|
37 const NAMESPACE_XML = 'http://www.w3.org/XML/1998/namespace';
|
Chris@0
|
38
|
Chris@0
|
39 const NAMESPACE_XMLNS = 'http://www.w3.org/2000/xmlns/';
|
Chris@0
|
40
|
Chris@0
|
41 const OPT_DISABLE_HTML_NS = 'disable_html_ns';
|
Chris@0
|
42
|
Chris@0
|
43 const OPT_TARGET_DOC = 'target_document';
|
Chris@0
|
44
|
Chris@0
|
45 const OPT_IMPLICIT_NS = 'implicit_namespaces';
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Holds the HTML5 element names that causes a namespace switch
|
Chris@0
|
49 *
|
Chris@0
|
50 * @var array
|
Chris@0
|
51 */
|
Chris@0
|
52 protected $nsRoots = array(
|
Chris@0
|
53 'html' => self::NAMESPACE_HTML,
|
Chris@0
|
54 'svg' => self::NAMESPACE_SVG,
|
Chris@0
|
55 'math' => self::NAMESPACE_MATHML
|
Chris@0
|
56 );
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Holds the always available namespaces (which does not require the XMLNS declaration).
|
Chris@0
|
60 *
|
Chris@0
|
61 * @var array
|
Chris@0
|
62 */
|
Chris@0
|
63 protected $implicitNamespaces = array(
|
Chris@0
|
64 'xml' => self::NAMESPACE_XML,
|
Chris@0
|
65 'xmlns' => self::NAMESPACE_XMLNS,
|
Chris@0
|
66 'xlink' => self::NAMESPACE_XLINK
|
Chris@0
|
67 );
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * Holds a stack of currently active namespaces.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @var array
|
Chris@0
|
73 */
|
Chris@0
|
74 protected $nsStack = array();
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Holds the number of namespaces declared by a node.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @var array
|
Chris@0
|
80 */
|
Chris@0
|
81 protected $pushes = array();
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * Defined in 8.2.5.
|
Chris@0
|
85 */
|
Chris@0
|
86 const IM_INITIAL = 0;
|
Chris@0
|
87
|
Chris@0
|
88 const IM_BEFORE_HTML = 1;
|
Chris@0
|
89
|
Chris@0
|
90 const IM_BEFORE_HEAD = 2;
|
Chris@0
|
91
|
Chris@0
|
92 const IM_IN_HEAD = 3;
|
Chris@0
|
93
|
Chris@0
|
94 const IM_IN_HEAD_NOSCRIPT = 4;
|
Chris@0
|
95
|
Chris@0
|
96 const IM_AFTER_HEAD = 5;
|
Chris@0
|
97
|
Chris@0
|
98 const IM_IN_BODY = 6;
|
Chris@0
|
99
|
Chris@0
|
100 const IM_TEXT = 7;
|
Chris@0
|
101
|
Chris@0
|
102 const IM_IN_TABLE = 8;
|
Chris@0
|
103
|
Chris@0
|
104 const IM_IN_TABLE_TEXT = 9;
|
Chris@0
|
105
|
Chris@0
|
106 const IM_IN_CAPTION = 10;
|
Chris@0
|
107
|
Chris@0
|
108 const IM_IN_COLUMN_GROUP = 11;
|
Chris@0
|
109
|
Chris@0
|
110 const IM_IN_TABLE_BODY = 12;
|
Chris@0
|
111
|
Chris@0
|
112 const IM_IN_ROW = 13;
|
Chris@0
|
113
|
Chris@0
|
114 const IM_IN_CELL = 14;
|
Chris@0
|
115
|
Chris@0
|
116 const IM_IN_SELECT = 15;
|
Chris@0
|
117
|
Chris@0
|
118 const IM_IN_SELECT_IN_TABLE = 16;
|
Chris@0
|
119
|
Chris@0
|
120 const IM_AFTER_BODY = 17;
|
Chris@0
|
121
|
Chris@0
|
122 const IM_IN_FRAMESET = 18;
|
Chris@0
|
123
|
Chris@0
|
124 const IM_AFTER_FRAMESET = 19;
|
Chris@0
|
125
|
Chris@0
|
126 const IM_AFTER_AFTER_BODY = 20;
|
Chris@0
|
127
|
Chris@0
|
128 const IM_AFTER_AFTER_FRAMESET = 21;
|
Chris@0
|
129
|
Chris@0
|
130 const IM_IN_SVG = 22;
|
Chris@0
|
131
|
Chris@0
|
132 const IM_IN_MATHML = 23;
|
Chris@0
|
133
|
Chris@0
|
134 protected $options = array();
|
Chris@0
|
135
|
Chris@0
|
136 protected $stack = array();
|
Chris@0
|
137
|
Chris@0
|
138 protected $current; // Pointer in the tag hierarchy.
|
Chris@0
|
139 protected $doc;
|
Chris@0
|
140
|
Chris@0
|
141 protected $frag;
|
Chris@0
|
142
|
Chris@0
|
143 protected $processor;
|
Chris@0
|
144
|
Chris@0
|
145 protected $insertMode = 0;
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * Track if we are in an element that allows only inline child nodes
|
Chris@0
|
149 * @var string|null
|
Chris@0
|
150 */
|
Chris@0
|
151 protected $onlyInline;
|
Chris@0
|
152
|
Chris@0
|
153 /**
|
Chris@0
|
154 * Quirks mode is enabled by default.
|
Chris@0
|
155 * Any document that is missing the
|
Chris@0
|
156 * DT will be considered to be in quirks mode.
|
Chris@0
|
157 */
|
Chris@0
|
158 protected $quirks = true;
|
Chris@0
|
159
|
Chris@0
|
160 protected $errors = array();
|
Chris@0
|
161
|
Chris@0
|
162 public function __construct($isFragment = false, array $options = array())
|
Chris@0
|
163 {
|
Chris@0
|
164 $this->options = $options;
|
Chris@0
|
165
|
Chris@0
|
166 if (isset($options[self::OPT_TARGET_DOC])) {
|
Chris@0
|
167 $this->doc = $options[self::OPT_TARGET_DOC];
|
Chris@0
|
168 } else {
|
Chris@0
|
169 $impl = new \DOMImplementation();
|
Chris@0
|
170 // XXX:
|
Chris@0
|
171 // Create the doctype. For now, we are always creating HTML5
|
Chris@0
|
172 // documents, and attempting to up-convert any older DTDs to HTML5.
|
Chris@0
|
173 $dt = $impl->createDocumentType('html');
|
Chris@0
|
174 // $this->doc = \DOMImplementation::createDocument(NULL, 'html', $dt);
|
Chris@0
|
175 $this->doc = $impl->createDocument(null, null, $dt);
|
Chris@0
|
176 }
|
Chris@0
|
177 $this->errors = array();
|
Chris@0
|
178
|
Chris@0
|
179 $this->current = $this->doc; // ->documentElement;
|
Chris@0
|
180
|
Chris@0
|
181 // Create a rules engine for tags.
|
Chris@0
|
182 $this->rules = new TreeBuildingRules($this->doc);
|
Chris@0
|
183
|
Chris@0
|
184 $implicitNS = array();
|
Chris@0
|
185 if (isset($this->options[self::OPT_IMPLICIT_NS])) {
|
Chris@0
|
186 $implicitNS = $this->options[self::OPT_IMPLICIT_NS];
|
Chris@0
|
187 } elseif (isset($this->options["implicitNamespaces"])) {
|
Chris@0
|
188 $implicitNS = $this->options["implicitNamespaces"];
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 // Fill $nsStack with the defalut HTML5 namespaces, plus the "implicitNamespaces" array taken form $options
|
Chris@0
|
192 array_unshift($this->nsStack, $implicitNS + array(
|
Chris@0
|
193 '' => self::NAMESPACE_HTML
|
Chris@0
|
194 ) + $this->implicitNamespaces);
|
Chris@0
|
195
|
Chris@0
|
196 if ($isFragment) {
|
Chris@0
|
197 $this->insertMode = static::IM_IN_BODY;
|
Chris@0
|
198 $this->frag = $this->doc->createDocumentFragment();
|
Chris@0
|
199 $this->current = $this->frag;
|
Chris@0
|
200 }
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 /**
|
Chris@0
|
204 * Get the document.
|
Chris@0
|
205 */
|
Chris@0
|
206 public function document()
|
Chris@0
|
207 {
|
Chris@0
|
208 return $this->doc;
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 /**
|
Chris@0
|
212 * Get the DOM fragment for the body.
|
Chris@0
|
213 *
|
Chris@0
|
214 * This returns a DOMNodeList because a fragment may have zero or more
|
Chris@0
|
215 * DOMNodes at its root.
|
Chris@0
|
216 *
|
Chris@0
|
217 * @see http://www.w3.org/TR/2012/CR-html5-20121217/syntax.html#concept-frag-parse-context
|
Chris@0
|
218 *
|
Chris@0
|
219 * @return \DOMFragmentDocumentFragment
|
Chris@0
|
220 */
|
Chris@0
|
221 public function fragment()
|
Chris@0
|
222 {
|
Chris@0
|
223 return $this->frag;
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 /**
|
Chris@0
|
227 * Provide an instruction processor.
|
Chris@0
|
228 *
|
Chris@0
|
229 * This is used for handling Processor Instructions as they are
|
Chris@0
|
230 * inserted. If omitted, PI's are inserted directly into the DOM tree.
|
Chris@0
|
231 */
|
Chris@0
|
232 public function setInstructionProcessor(\Masterminds\HTML5\InstructionProcessor $proc)
|
Chris@0
|
233 {
|
Chris@0
|
234 $this->processor = $proc;
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 public function doctype($name, $idType = 0, $id = null, $quirks = false)
|
Chris@0
|
238 {
|
Chris@0
|
239 // This is used solely for setting quirks mode. Currently we don't
|
Chris@0
|
240 // try to preserve the inbound DT. We convert it to HTML5.
|
Chris@0
|
241 $this->quirks = $quirks;
|
Chris@0
|
242
|
Chris@0
|
243 if ($this->insertMode > static::IM_INITIAL) {
|
Chris@0
|
244 $this->parseError("Illegal placement of DOCTYPE tag. Ignoring: " . $name);
|
Chris@0
|
245
|
Chris@0
|
246 return;
|
Chris@0
|
247 }
|
Chris@0
|
248
|
Chris@0
|
249 $this->insertMode = static::IM_BEFORE_HTML;
|
Chris@0
|
250 }
|
Chris@0
|
251
|
Chris@0
|
252 /**
|
Chris@0
|
253 * Process the start tag.
|
Chris@0
|
254 *
|
Chris@0
|
255 * @todo - XMLNS namespace handling (we need to parse, even if it's not valid)
|
Chris@0
|
256 * - XLink, MathML and SVG namespace handling
|
Chris@0
|
257 * - Omission rules: 8.1.2.4 Optional tags
|
Chris@0
|
258 */
|
Chris@0
|
259 public function startTag($name, $attributes = array(), $selfClosing = false)
|
Chris@0
|
260 {
|
Chris@0
|
261 // fprintf(STDOUT, $name);
|
Chris@0
|
262 $lname = $this->normalizeTagName($name);
|
Chris@0
|
263
|
Chris@0
|
264 // Make sure we have an html element.
|
Chris@0
|
265 if (! $this->doc->documentElement && $name !== 'html' && ! $this->frag) {
|
Chris@0
|
266 $this->startTag('html');
|
Chris@0
|
267 }
|
Chris@0
|
268
|
Chris@0
|
269 // Set quirks mode if we're at IM_INITIAL with no doctype.
|
Chris@0
|
270 if ($this->insertMode == static::IM_INITIAL) {
|
Chris@0
|
271 $this->quirks = true;
|
Chris@0
|
272 $this->parseError("No DOCTYPE specified.");
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 // SPECIAL TAG HANDLING:
|
Chris@0
|
276 // Spec says do this, and "don't ask."
|
Chris@0
|
277 // find the spec where this is defined... looks problematic
|
Chris@0
|
278 if ($name == 'image' && !($this->insertMode === static::IM_IN_SVG || $this->insertMode === static::IM_IN_MATHML)) {
|
Chris@0
|
279 $name = 'img';
|
Chris@0
|
280 }
|
Chris@0
|
281
|
Chris@0
|
282 // Autoclose p tags where appropriate.
|
Chris@0
|
283 if ($this->insertMode >= static::IM_IN_BODY && Elements::isA($name, Elements::AUTOCLOSE_P)) {
|
Chris@0
|
284 $this->autoclose('p');
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 // Set insert mode:
|
Chris@0
|
288 switch ($name) {
|
Chris@0
|
289 case 'html':
|
Chris@0
|
290 $this->insertMode = static::IM_BEFORE_HEAD;
|
Chris@0
|
291 break;
|
Chris@0
|
292 case 'head':
|
Chris@0
|
293 if ($this->insertMode > static::IM_BEFORE_HEAD) {
|
Chris@0
|
294 $this->parseError("Unexpected head tag outside of head context.");
|
Chris@0
|
295 } else {
|
Chris@0
|
296 $this->insertMode = static::IM_IN_HEAD;
|
Chris@0
|
297 }
|
Chris@0
|
298 break;
|
Chris@0
|
299 case 'body':
|
Chris@0
|
300 $this->insertMode = static::IM_IN_BODY;
|
Chris@0
|
301 break;
|
Chris@0
|
302 case 'svg':
|
Chris@0
|
303 $this->insertMode = static::IM_IN_SVG;
|
Chris@0
|
304 break;
|
Chris@0
|
305 case 'math':
|
Chris@0
|
306 $this->insertMode = static::IM_IN_MATHML;
|
Chris@0
|
307 break;
|
Chris@0
|
308 case 'noscript':
|
Chris@0
|
309 if ($this->insertMode == static::IM_IN_HEAD) {
|
Chris@0
|
310 $this->insertMode = static::IM_IN_HEAD_NOSCRIPT;
|
Chris@0
|
311 }
|
Chris@0
|
312 break;
|
Chris@0
|
313 }
|
Chris@0
|
314
|
Chris@0
|
315 // Special case handling for SVG.
|
Chris@0
|
316 if ($this->insertMode == static::IM_IN_SVG) {
|
Chris@0
|
317 $lname = Elements::normalizeSvgElement($lname);
|
Chris@0
|
318 }
|
Chris@0
|
319
|
Chris@0
|
320 $pushes = 0;
|
Chris@0
|
321 // when we found a tag thats appears inside $nsRoots, we have to switch the defalut namespace
|
Chris@0
|
322 if (isset($this->nsRoots[$lname]) && $this->nsStack[0][''] !== $this->nsRoots[$lname]) {
|
Chris@0
|
323 array_unshift($this->nsStack, array(
|
Chris@0
|
324 '' => $this->nsRoots[$lname]
|
Chris@0
|
325 ) + $this->nsStack[0]);
|
Chris@0
|
326 $pushes ++;
|
Chris@0
|
327 }
|
Chris@0
|
328 $needsWorkaround = false;
|
Chris@0
|
329 if (isset($this->options["xmlNamespaces"]) && $this->options["xmlNamespaces"]) {
|
Chris@0
|
330 // when xmlNamespaces is true a and we found a 'xmlns' or 'xmlns:*' attribute, we should add a new item to the $nsStack
|
Chris@0
|
331 foreach ($attributes as $aName => $aVal) {
|
Chris@0
|
332 if ($aName === 'xmlns') {
|
Chris@0
|
333 $needsWorkaround = $aVal;
|
Chris@0
|
334 array_unshift($this->nsStack, array(
|
Chris@0
|
335 '' => $aVal
|
Chris@0
|
336 ) + $this->nsStack[0]);
|
Chris@0
|
337 $pushes ++;
|
Chris@0
|
338 } elseif ((($pos = strpos($aName, ':')) ? substr($aName, 0, $pos) : '') === 'xmlns') {
|
Chris@0
|
339 array_unshift($this->nsStack, array(
|
Chris@0
|
340 substr($aName, $pos + 1) => $aVal
|
Chris@0
|
341 ) + $this->nsStack[0]);
|
Chris@0
|
342 $pushes ++;
|
Chris@0
|
343 }
|
Chris@0
|
344 }
|
Chris@0
|
345 }
|
Chris@0
|
346
|
Chris@0
|
347 if ($this->onlyInline && Elements::isA($lname, Elements::BLOCK_TAG)) {
|
Chris@0
|
348 $this->autoclose($this->onlyInline);
|
Chris@0
|
349 $this->onlyInline = null;
|
Chris@0
|
350 }
|
Chris@0
|
351
|
Chris@0
|
352 try {
|
Chris@0
|
353 $prefix = ($pos = strpos($lname, ':')) ? substr($lname, 0, $pos) : '';
|
Chris@0
|
354
|
Chris@0
|
355
|
Chris@0
|
356 if ($needsWorkaround!==false) {
|
Chris@0
|
357
|
Chris@0
|
358 $xml = "<$lname xmlns=\"$needsWorkaround\" ".(strlen($prefix) && isset($this->nsStack[0][$prefix])?("xmlns:$prefix=\"".$this->nsStack[0][$prefix]."\""):"")."/>";
|
Chris@0
|
359
|
Chris@0
|
360 $frag = new \DOMDocument('1.0', 'UTF-8');
|
Chris@0
|
361 $frag->loadXML($xml);
|
Chris@0
|
362
|
Chris@0
|
363 $ele = $this->doc->importNode($frag->documentElement, true);
|
Chris@0
|
364
|
Chris@0
|
365 } else {
|
Chris@0
|
366 if (!isset($this->nsStack[0][$prefix]) || ($prefix === "" && isset($this->options[self::OPT_DISABLE_HTML_NS]) && $this->options[self::OPT_DISABLE_HTML_NS])) {
|
Chris@0
|
367 $ele = $this->doc->createElement($lname);
|
Chris@0
|
368 } else {
|
Chris@0
|
369 $ele = $this->doc->createElementNS($this->nsStack[0][$prefix], $lname);
|
Chris@0
|
370 }
|
Chris@0
|
371 }
|
Chris@0
|
372
|
Chris@0
|
373 } catch (\DOMException $e) {
|
Chris@0
|
374 $this->parseError("Illegal tag name: <$lname>. Replaced with <invalid>.");
|
Chris@0
|
375 $ele = $this->doc->createElement('invalid');
|
Chris@0
|
376 }
|
Chris@0
|
377
|
Chris@0
|
378 if (Elements::isA($lname, Elements::BLOCK_ONLY_INLINE)) {
|
Chris@0
|
379 $this->onlyInline = $lname;
|
Chris@0
|
380 }
|
Chris@0
|
381
|
Chris@0
|
382 // When we add some namespacess, we have to track them. Later, when "endElement" is invoked, we have to remove them.
|
Chris@0
|
383 // When we are on a void tag, we do not need to care about namesapce nesting.
|
Chris@0
|
384 if ($pushes > 0 && !Elements::isA($name, Elements::VOID_TAG)) {
|
Chris@0
|
385 // PHP tends to free the memory used by DOM,
|
Chris@0
|
386 // to avoid spl_object_hash collisions whe have to avoid garbage collection of $ele storing it into $pushes
|
Chris@0
|
387 // see https://bugs.php.net/bug.php?id=67459
|
Chris@0
|
388 $this->pushes[spl_object_hash($ele)] = array($pushes, $ele);
|
Chris@0
|
389
|
Chris@0
|
390 // SEE https://github.com/facebook/hhvm/issues/2962
|
Chris@0
|
391 if (defined('HHVM_VERSION')) {
|
Chris@0
|
392 $ele->setAttribute('html5-php-fake-id-attribute', spl_object_hash($ele));
|
Chris@0
|
393 }
|
Chris@0
|
394 }
|
Chris@0
|
395
|
Chris@0
|
396 foreach ($attributes as $aName => $aVal) {
|
Chris@0
|
397 // xmlns attributes can't be set
|
Chris@0
|
398 if ($aName === 'xmlns') {
|
Chris@0
|
399 continue;
|
Chris@0
|
400 }
|
Chris@0
|
401
|
Chris@0
|
402 if ($this->insertMode == static::IM_IN_SVG) {
|
Chris@0
|
403 $aName = Elements::normalizeSvgAttribute($aName);
|
Chris@0
|
404 } elseif ($this->insertMode == static::IM_IN_MATHML) {
|
Chris@0
|
405 $aName = Elements::normalizeMathMlAttribute($aName);
|
Chris@0
|
406 }
|
Chris@0
|
407
|
Chris@0
|
408 try {
|
Chris@0
|
409 $prefix = ($pos = strpos($aName, ':')) ? substr($aName, 0, $pos) : false;
|
Chris@0
|
410
|
Chris@0
|
411 if ($prefix==='xmlns') {
|
Chris@0
|
412 $ele->setAttributeNs(self::NAMESPACE_XMLNS, $aName, $aVal);
|
Chris@0
|
413 } elseif ($prefix!==false && isset($this->nsStack[0][$prefix])) {
|
Chris@0
|
414 $ele->setAttributeNs($this->nsStack[0][$prefix], $aName, $aVal);
|
Chris@0
|
415 } else {
|
Chris@0
|
416 $ele->setAttribute($aName, $aVal);
|
Chris@0
|
417 }
|
Chris@0
|
418 } catch (\DOMException $e) {
|
Chris@0
|
419 $this->parseError("Illegal attribute name for tag $name. Ignoring: $aName");
|
Chris@0
|
420 continue;
|
Chris@0
|
421 }
|
Chris@0
|
422
|
Chris@0
|
423 // This is necessary on a non-DTD schema, like HTML5.
|
Chris@0
|
424 if ($aName == 'id') {
|
Chris@0
|
425 $ele->setIdAttribute('id', true);
|
Chris@0
|
426 }
|
Chris@0
|
427 }
|
Chris@0
|
428
|
Chris@0
|
429 // Some elements have special processing rules. Handle those separately.
|
Chris@0
|
430 if ($this->rules->hasRules($name) && $this->frag !== $this->current) {
|
Chris@0
|
431 $this->current = $this->rules->evaluate($ele, $this->current);
|
Chris@0
|
432 } // Otherwise, it's a standard element.
|
Chris@0
|
433 else {
|
Chris@0
|
434 $this->current->appendChild($ele);
|
Chris@0
|
435
|
Chris@0
|
436 // XXX: Need to handle self-closing tags and unary tags.
|
Chris@0
|
437 if (! Elements::isA($name, Elements::VOID_TAG)) {
|
Chris@0
|
438 $this->current = $ele;
|
Chris@0
|
439 }
|
Chris@0
|
440 }
|
Chris@0
|
441
|
Chris@0
|
442 // This is sort of a last-ditch attempt to correct for cases where no head/body
|
Chris@0
|
443 // elements are provided.
|
Chris@0
|
444 if ($this->insertMode <= static::IM_BEFORE_HEAD && $name != 'head' && $name != 'html') {
|
Chris@0
|
445 $this->insertMode = static::IM_IN_BODY;
|
Chris@0
|
446 }
|
Chris@0
|
447
|
Chris@0
|
448 // When we are on a void tag, we do not need to care about namesapce nesting,
|
Chris@0
|
449 // but we have to remove the namespaces pushed to $nsStack.
|
Chris@0
|
450 if ($pushes > 0 && Elements::isA($name, Elements::VOID_TAG)) {
|
Chris@0
|
451 // remove the namespaced definded by current node
|
Chris@0
|
452 for ($i = 0; $i < $pushes; $i ++) {
|
Chris@0
|
453 array_shift($this->nsStack);
|
Chris@0
|
454 }
|
Chris@0
|
455 }
|
Chris@0
|
456 // Return the element mask, which the tokenizer can then use to set
|
Chris@0
|
457 // various processing rules.
|
Chris@0
|
458 return Elements::element($name);
|
Chris@0
|
459 }
|
Chris@0
|
460
|
Chris@0
|
461 public function endTag($name)
|
Chris@0
|
462 {
|
Chris@0
|
463 $lname = $this->normalizeTagName($name);
|
Chris@0
|
464
|
Chris@0
|
465 // Ignore closing tags for unary elements.
|
Chris@0
|
466 if (Elements::isA($name, Elements::VOID_TAG)) {
|
Chris@0
|
467 return;
|
Chris@0
|
468 }
|
Chris@0
|
469
|
Chris@0
|
470 if ($this->insertMode <= static::IM_BEFORE_HTML) {
|
Chris@0
|
471 // 8.2.5.4.2
|
Chris@0
|
472 if (in_array($name, array(
|
Chris@0
|
473 'html',
|
Chris@0
|
474 'br',
|
Chris@0
|
475 'head',
|
Chris@0
|
476 'title'
|
Chris@0
|
477 ))) {
|
Chris@0
|
478 $this->startTag('html');
|
Chris@0
|
479 $this->endTag($name);
|
Chris@0
|
480 $this->insertMode = static::IM_BEFORE_HEAD;
|
Chris@0
|
481
|
Chris@0
|
482 return;
|
Chris@0
|
483 }
|
Chris@0
|
484
|
Chris@0
|
485 // Ignore the tag.
|
Chris@0
|
486 $this->parseError("Illegal closing tag at global scope.");
|
Chris@0
|
487
|
Chris@0
|
488 return;
|
Chris@0
|
489 }
|
Chris@0
|
490
|
Chris@0
|
491 // Special case handling for SVG.
|
Chris@0
|
492 if ($this->insertMode == static::IM_IN_SVG) {
|
Chris@0
|
493 $lname = Elements::normalizeSvgElement($lname);
|
Chris@0
|
494 }
|
Chris@0
|
495
|
Chris@0
|
496 // See https://github.com/facebook/hhvm/issues/2962
|
Chris@0
|
497 if (defined('HHVM_VERSION') && ($cid = $this->current->getAttribute('html5-php-fake-id-attribute'))) {
|
Chris@0
|
498 $this->current->removeAttribute('html5-php-fake-id-attribute');
|
Chris@0
|
499 } else {
|
Chris@0
|
500 $cid = spl_object_hash($this->current);
|
Chris@0
|
501 }
|
Chris@0
|
502
|
Chris@0
|
503 // XXX: Not sure whether we need this anymore.
|
Chris@0
|
504 // if ($name != $lname) {
|
Chris@0
|
505 // return $this->quirksTreeResolver($lname);
|
Chris@0
|
506 // }
|
Chris@0
|
507
|
Chris@0
|
508 // XXX: HTML has no parent. What do we do, though,
|
Chris@0
|
509 // if this element appears in the wrong place?
|
Chris@0
|
510 if ($lname == 'html') {
|
Chris@0
|
511 return;
|
Chris@0
|
512 }
|
Chris@0
|
513
|
Chris@0
|
514 // remove the namespaced definded by current node
|
Chris@0
|
515 if (isset($this->pushes[$cid])) {
|
Chris@0
|
516 for ($i = 0; $i < $this->pushes[$cid][0]; $i ++) {
|
Chris@0
|
517 array_shift($this->nsStack);
|
Chris@0
|
518 }
|
Chris@0
|
519 unset($this->pushes[$cid]);
|
Chris@0
|
520 }
|
Chris@0
|
521
|
Chris@0
|
522 if (! $this->autoclose($lname)) {
|
Chris@0
|
523 $this->parseError('Could not find closing tag for ' . $lname);
|
Chris@0
|
524 }
|
Chris@0
|
525
|
Chris@0
|
526 // switch ($this->insertMode) {
|
Chris@0
|
527 switch ($lname) {
|
Chris@0
|
528 case "head":
|
Chris@0
|
529 $this->insertMode = static::IM_AFTER_HEAD;
|
Chris@0
|
530 break;
|
Chris@0
|
531 case "body":
|
Chris@0
|
532 $this->insertMode = static::IM_AFTER_BODY;
|
Chris@0
|
533 break;
|
Chris@0
|
534 case "svg":
|
Chris@0
|
535 case "mathml":
|
Chris@0
|
536 $this->insertMode = static::IM_IN_BODY;
|
Chris@0
|
537 break;
|
Chris@0
|
538 }
|
Chris@0
|
539 }
|
Chris@0
|
540
|
Chris@0
|
541 public function comment($cdata)
|
Chris@0
|
542 {
|
Chris@0
|
543 // TODO: Need to handle case where comment appears outside of the HTML tag.
|
Chris@0
|
544 $node = $this->doc->createComment($cdata);
|
Chris@0
|
545 $this->current->appendChild($node);
|
Chris@0
|
546 }
|
Chris@0
|
547
|
Chris@0
|
548 public function text($data)
|
Chris@0
|
549 {
|
Chris@0
|
550 // XXX: Hmmm.... should we really be this strict?
|
Chris@0
|
551 if ($this->insertMode < static::IM_IN_HEAD) {
|
Chris@0
|
552 // Per '8.2.5.4.3 The "before head" insertion mode' the characters
|
Chris@0
|
553 // " \t\n\r\f" should be ignored but no mention of a parse error. This is
|
Chris@0
|
554 // practical as most documents contain these characters. Other text is not
|
Chris@0
|
555 // expected here so recording a parse error is necessary.
|
Chris@0
|
556 $dataTmp = trim($data, " \t\n\r\f");
|
Chris@0
|
557 if (! empty($dataTmp)) {
|
Chris@0
|
558 // fprintf(STDOUT, "Unexpected insert mode: %d", $this->insertMode);
|
Chris@0
|
559 $this->parseError("Unexpected text. Ignoring: " . $dataTmp);
|
Chris@0
|
560 }
|
Chris@0
|
561
|
Chris@0
|
562 return;
|
Chris@0
|
563 }
|
Chris@0
|
564 // fprintf(STDOUT, "Appending text %s.", $data);
|
Chris@0
|
565 $node = $this->doc->createTextNode($data);
|
Chris@0
|
566 $this->current->appendChild($node);
|
Chris@0
|
567 }
|
Chris@0
|
568
|
Chris@0
|
569 public function eof()
|
Chris@0
|
570 {
|
Chris@0
|
571 // If the $current isn't the $root, do we need to do anything?
|
Chris@0
|
572 }
|
Chris@0
|
573
|
Chris@0
|
574 public function parseError($msg, $line = 0, $col = 0)
|
Chris@0
|
575 {
|
Chris@0
|
576 $this->errors[] = sprintf("Line %d, Col %d: %s", $line, $col, $msg);
|
Chris@0
|
577 }
|
Chris@0
|
578
|
Chris@0
|
579 public function getErrors()
|
Chris@0
|
580 {
|
Chris@0
|
581 return $this->errors;
|
Chris@0
|
582 }
|
Chris@0
|
583
|
Chris@0
|
584 public function cdata($data)
|
Chris@0
|
585 {
|
Chris@0
|
586 $node = $this->doc->createCDATASection($data);
|
Chris@0
|
587 $this->current->appendChild($node);
|
Chris@0
|
588 }
|
Chris@0
|
589
|
Chris@0
|
590 public function processingInstruction($name, $data = null)
|
Chris@0
|
591 {
|
Chris@0
|
592 // XXX: Ignore initial XML declaration, per the spec.
|
Chris@0
|
593 if ($this->insertMode == static::IM_INITIAL && 'xml' == strtolower($name)) {
|
Chris@0
|
594 return;
|
Chris@0
|
595 }
|
Chris@0
|
596
|
Chris@0
|
597 // Important: The processor may modify the current DOM tree however
|
Chris@0
|
598 // it sees fit.
|
Chris@0
|
599 if (isset($this->processor)) {
|
Chris@0
|
600 $res = $this->processor->process($this->current, $name, $data);
|
Chris@0
|
601 if (! empty($res)) {
|
Chris@0
|
602 $this->current = $res;
|
Chris@0
|
603 }
|
Chris@0
|
604
|
Chris@0
|
605 return;
|
Chris@0
|
606 }
|
Chris@0
|
607
|
Chris@0
|
608 // Otherwise, this is just a dumb PI element.
|
Chris@0
|
609 $node = $this->doc->createProcessingInstruction($name, $data);
|
Chris@0
|
610
|
Chris@0
|
611 $this->current->appendChild($node);
|
Chris@0
|
612 }
|
Chris@0
|
613
|
Chris@0
|
614 // ==========================================================================
|
Chris@0
|
615 // UTILITIES
|
Chris@0
|
616 // ==========================================================================
|
Chris@0
|
617
|
Chris@0
|
618 /**
|
Chris@0
|
619 * Apply normalization rules to a tag name.
|
Chris@0
|
620 *
|
Chris@0
|
621 * See sections 2.9 and 8.1.2.
|
Chris@0
|
622 *
|
Chris@0
|
623 * @param string $name
|
Chris@0
|
624 * The tag name.
|
Chris@0
|
625 * @return string The normalized tag name.
|
Chris@0
|
626 */
|
Chris@0
|
627 protected function normalizeTagName($name)
|
Chris@0
|
628 {
|
Chris@0
|
629 /*
|
Chris@0
|
630 * Section 2.9 suggests that we should not do this. if (strpos($name, ':') !== false) { // We know from the grammar that there must be at least one other // char besides :, since : is not a legal tag start. $parts = explode(':', $name); return array_pop($parts); }
|
Chris@0
|
631 */
|
Chris@0
|
632 return $name;
|
Chris@0
|
633 }
|
Chris@0
|
634
|
Chris@0
|
635 protected function quirksTreeResolver($name)
|
Chris@0
|
636 {
|
Chris@0
|
637 throw new \Exception("Not implemented.");
|
Chris@0
|
638 }
|
Chris@0
|
639
|
Chris@0
|
640 /**
|
Chris@0
|
641 * Automatically climb the tree and close the closest node with the matching $tag.
|
Chris@0
|
642 */
|
Chris@0
|
643 protected function autoclose($tag)
|
Chris@0
|
644 {
|
Chris@0
|
645 $working = $this->current;
|
Chris@0
|
646 do {
|
Chris@0
|
647 if ($working->nodeType != XML_ELEMENT_NODE) {
|
Chris@0
|
648 return false;
|
Chris@0
|
649 }
|
Chris@0
|
650 if ($working->tagName == $tag) {
|
Chris@0
|
651 $this->current = $working->parentNode;
|
Chris@0
|
652
|
Chris@0
|
653 return true;
|
Chris@0
|
654 }
|
Chris@0
|
655 } while ($working = $working->parentNode);
|
Chris@0
|
656 return false;
|
Chris@0
|
657 }
|
Chris@0
|
658
|
Chris@0
|
659 /**
|
Chris@0
|
660 * Checks if the given tagname is an ancestor of the present candidate.
|
Chris@0
|
661 *
|
Chris@0
|
662 * If $this->current or anything above $this->current matches the given tag
|
Chris@0
|
663 * name, this returns true.
|
Chris@0
|
664 */
|
Chris@0
|
665 protected function isAncestor($tagname)
|
Chris@0
|
666 {
|
Chris@0
|
667 $candidate = $this->current;
|
Chris@0
|
668 while ($candidate->nodeType === XML_ELEMENT_NODE) {
|
Chris@0
|
669 if ($candidate->tagName == $tagname) {
|
Chris@0
|
670 return true;
|
Chris@0
|
671 }
|
Chris@0
|
672 $candidate = $candidate->parentNode;
|
Chris@0
|
673 }
|
Chris@0
|
674
|
Chris@0
|
675 return false;
|
Chris@0
|
676 }
|
Chris@0
|
677
|
Chris@0
|
678 /**
|
Chris@0
|
679 * Returns true if the immediate parent element is of the given tagname.
|
Chris@0
|
680 */
|
Chris@0
|
681 protected function isParent($tagname)
|
Chris@0
|
682 {
|
Chris@0
|
683 return $this->current->tagName == $tagname;
|
Chris@0
|
684 }
|
Chris@0
|
685 }
|