Verified Commit f101ff7f authored by Alex Pott's avatar Alex Pott
Browse files

Issue #418620 by mfb, smustgrave, longwave, dsnopek, greenbeans, borisson_,...

Issue #418620 by mfb, smustgrave, longwave, dsnopek, greenbeans, borisson_, alexpott, olli, larowlan: Drupal\Component\Utility\Html::normalize() leaves messy </body></html> in certain situations
parent bbb91304
Loading
Loading
Loading
Loading
Loading
+5 −10
Original line number Diff line number Diff line
@@ -275,18 +275,13 @@ public static function normalize($html) {
   *   A \DOMDocument that represents the loaded HTML snippet.
   */
  public static function load($html) {
    $document = <<<EOD
<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>$html</body>
</html>
EOD;

    // Instantiate the HTML5 parser, but without the HTML5 namespace being
    // added to the DOM document.
    $html5 = new HTML5(['disable_html_ns' => TRUE]);
    return $html5->loadHTML($document);
    $html5 = new HTML5(['disable_html_ns' => TRUE, 'encoding' => 'UTF-8']);

    // Attach the provided HTML inside the body. Rely on the HTML5 parser to
    // close the body tag.
    return $html5->loadHTML('<body>' . $html);
  }

  /**
+3 −0
Original line number Diff line number Diff line
@@ -1008,6 +1008,9 @@ public function testHtmlCorrectorFilter() {
    $f = Html::normalize('<p>text');
    $this->assertEquals('<p>text</p>', $f, 'HTML corrector -- tag closing at the end of input.');

    $f = Html::normalize('<p>text <img alt="ao');
    $this->assertEquals('<p>text <img alt="ao"></p>', $f, 'HTML corrector -- tag closing at the end of input + broken attribute.');

    $f = Html::normalize('<p>text<p><p>text');
    $this->assertEquals('<p>text</p><p></p><p>text</p>', $f, 'HTML corrector -- tag closing.');