@@ -322,6 +322,12 @@ public function startTag($name, $attributes = array(), $selfClosing = false)
322
322
break ;
323
323
}
324
324
325
+ // Case when no <body> exists, note section on 'Anything else' below.
326
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-after-head-insertion-mode
327
+ if ($ this ->insertMode === static ::IM_AFTER_HEAD && 'head ' !== $ name && 'body ' !== $ name ) {
328
+ $ this ->startTag ('body ' );
329
+ }
330
+
325
331
// Special case handling for SVG.
326
332
if ($ this ->insertMode === static ::IM_IN_SVG ) {
327
333
$ lname = Elements::normalizeSvgElement ($ lname );
@@ -548,21 +554,20 @@ public function comment($cdata)
548
554
549
555
public function text ($ data )
550
556
{
551
- // XXX: Hmmm.... should we really be this strict?
557
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-before-head-insertion-mode
552
558
if ($ this ->insertMode < static ::IM_IN_HEAD ) {
553
559
// Per '8.2.5.4.3 The "before head" insertion mode' the characters
554
- // " \t\n\r\f" should be ignored but no mention of a parse error. This is
555
- // practical as most documents contain these characters. Other text is not
556
- // expected here so recording a parse error is necessary.
560
+ // " \t\n\r\f" should be ignored .
557
561
$ dataTmp = trim ($ data , " \t\n\r\f" );
558
- if (!empty ($ dataTmp )) {
559
- // fprintf(STDOUT, "Unexpected insert mode: %d", $this->insertMode);
560
- $ this ->parseError ('Unexpected text. Ignoring: ' . $ dataTmp );
562
+ if (! empty ($ dataTmp )) {
563
+ $ this ->startTag ('head ' );
564
+ $ this ->endTag ('head ' );
565
+ $ this ->startTag ('body ' );
566
+ } else {
567
+ return ;
561
568
}
562
-
563
- return ;
564
569
}
565
- // fprintf(STDOUT, "Appending text %s.", $data);
570
+
566
571
$ node = $ this ->doc ->createTextNode ($ data );
567
572
$ this ->current ->appendChild ($ node );
568
573
}
0 commit comments