@@ -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 );
@@ -556,21 +562,20 @@ public function comment($cdata)
556
562
557
563
public function text ($ data )
558
564
{
559
- // XXX: Hmmm.... should we really be this strict?
565
+ // https://html.spec.whatwg.org/multipage/parsing.html#the-before-head-insertion-mode
560
566
if ($ this ->insertMode < static ::IM_IN_HEAD ) {
561
567
// Per '8.2.5.4.3 The "before head" insertion mode' the characters
562
- // " \t\n\r\f" should be ignored but no mention of a parse error. This is
563
- // practical as most documents contain these characters. Other text is not
564
- // expected here so recording a parse error is necessary.
568
+ // " \t\n\r\f" should be ignored .
565
569
$ dataTmp = trim ($ data , " \t\n\r\f" );
566
- if (!empty ($ dataTmp )) {
567
- // fprintf(STDOUT, "Unexpected insert mode: %d", $this->insertMode);
568
- $ this ->parseError ('Unexpected text. Ignoring: ' . $ dataTmp );
570
+ if (! empty ($ dataTmp )) {
571
+ $ this ->startTag ('head ' );
572
+ $ this ->endTag ('head ' );
573
+ $ this ->startTag ('body ' );
574
+ } else {
575
+ return ;
569
576
}
570
-
571
- return ;
572
577
}
573
- // fprintf(STDOUT, "Appending text %s.", $data);
578
+
574
579
$ node = $ this ->doc ->createTextNode ($ data );
575
580
$ this ->current ->appendChild ($ node );
576
581
}
0 commit comments