@@ -322,6 +322,12 @@ public function startTag($name, $attributes = array(), $selfClosing = false)
322322 break ;
323323 }
324324
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+
325331 // Special case handling for SVG.
326332 if ($ this ->insertMode === static ::IM_IN_SVG ) {
327333 $ lname = Elements::normalizeSvgElement ($ lname );
@@ -548,21 +554,20 @@ public function comment($cdata)
548554
549555 public function text ($ data )
550556 {
551- // XXX: Hmmm.... should we really be this strict?
557+ // https://html.spec.whatwg.org/multipage/parsing.html#the-before-head-insertion-mode
552558 if ($ this ->insertMode < static ::IM_IN_HEAD ) {
553559 // 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 .
557561 $ 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 ;
561568 }
562-
563- return ;
564569 }
565- // fprintf(STDOUT, "Appending text %s.", $data);
570+
566571 $ node = $ this ->doc ->createTextNode ($ data );
567572 $ this ->current ->appendChild ($ node );
568573 }
0 commit comments