reverted: --- core/modules/help/tests/src/Functional/HelpTopicsSyntaxTest.php +++ core/modules/help/tests/src/Functional/HelpTopicsSyntaxTest.php @@ -4,11 +4,9 @@ use Drupal\Core\Extension\ExtensionLifecycle; use Drupal\Component\FrontMatter\FrontMatter; -use Drupal\Component\Utility\Html; use Drupal\Tests\BrowserTestBase; use Drupal\help\HelpTopicDiscovery; use Drupal\help_topics_twig_tester\HelpTestTwigNodeVisitor; -use Masterminds\HTML5; use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\AssertionFailedError; @@ -189,19 +187,18 @@ * ID of help topic (for error messages). */ protected function validateHtml(string $body, string $id) { + $doc = new \DOMDocument(); + $doc->strictErrorChecking = TRUE; + $doc->validateOnParse = FALSE; + libxml_use_internal_errors(TRUE); + if (!$doc->loadXML('
' . $body . '')) { + foreach (libxml_get_errors() as $error) { + $this->fail('Topic ' . $id . ' fails HTML validation: ' . $error->message); - // Inspired on \Drupal\Component\Utility\Html::load(), but keeping the - // HTML5 object to be able to use it later, and use the fragment related - // method. - $html5 = new HTML5(['disable_html_ns' => TRUE, 'encoding' => 'UTF-8']); - $doc = $html5->loadHTMLFragment($body); - if ($html5->hasErrors()) { - foreach ($html5->getErrors() as $error) { - $this->fail('Topic ' . $id . ' fails HTML validation: ' . $error); } + + libxml_clear_errors(); } - // Load it again, as full document, to walk over it. - $doc = Html::load($body); // Check for headings hierarchy. $levels = [1, 2, 3, 4, 5, 6]; foreach ($levels as $level) { @@ -248,7 +245,7 @@ case 'bad_html': case 'bad_html2': case 'bad_html3': + $this->assertStringContainsString('Opening and ending tag mismatch', $message); - $this->assertStringContainsString('Could not find closing tag for', $message); break; case 'top_level': reverted: --- core/modules/layout_discovery/tests/src/Kernel/LayoutTest.php +++ core/modules/layout_discovery/tests/src/Kernel/LayoutTest.php @@ -71,7 +71,7 @@ $build_id_input = $this->cssSelect('input[name="form_build_id"]')[0]->asXML(); $form_id_input = ''; $html[] = 'Test suffix'; + $html[] = $build_id_input . $form_id_input . ''; - $html[] = $build_id_input . "\n" . $form_id_input . "\n" . ''; // Match the HTML to the full form element. $this->assertSame(implode("\n", $html), $this->cssSelect('#the-form-id')[0]->asXML()); reverted: --- core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php +++ core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php @@ -480,7 +480,8 @@ // it. $render_array = $base_array; $html = \Drupal::service('renderer')->renderRoot($render_array); + $dom = new \DOMDocument(); + $dom->loadHTML($html); - $dom = Html::load($html); $this->assertEquals(1, $dom->getElementsByTagName('ul')->length, 'One "ul" tag found in the rendered HTML.'); $list_elements = $dom->getElementsByTagName('li'); $this->assertEquals(3, $list_elements->length, 'Three "li" tags found in the rendered HTML.'); @@ -497,14 +498,16 @@ $child_html = \Drupal::service('renderer')->renderRoot($render_array['first_child']); $parent_html = \Drupal::service('renderer')->renderRoot($render_array); // First check the child HTML. + $dom = new \DOMDocument(); + $dom->loadHTML($child_html); - $dom = Html::load($child_html); $this->assertEquals(1, $dom->getElementsByTagName('ul')->length, 'One "ul" tag found in the rendered child HTML.'); $list_elements = $dom->getElementsByTagName('li'); $this->assertEquals(2, $list_elements->length, 'Two "li" tags found in the rendered child HTML.'); $this->assertEquals('Parent link copy', $list_elements->item(0)->nodeValue, 'First expected link found.'); $this->assertEquals('First child link', $list_elements->item(1)->nodeValue, 'Second expected link found.'); // Then check the parent HTML. + $dom = new \DOMDocument(); + $dom->loadHTML($parent_html); - $dom = Html::load($parent_html); $this->assertEquals(1, $dom->getElementsByTagName('ul')->length, 'One "ul" tag found in the rendered parent HTML.'); $list_elements = $dom->getElementsByTagName('li'); $this->assertEquals(2, $list_elements->length, 'Two "li" tags found in the rendered parent HTML.'); reverted: --- core/modules/views/tests/src/Functional/Handler/FieldWebTest.php +++ core/modules/views/tests/src/Functional/Handler/FieldWebTest.php @@ -177,7 +177,8 @@ * An array containing simplexml objects. */ protected function parseContent($content) { + $htmlDom = new \DOMDocument(); + @$htmlDom->loadHTML('' . $content); - $htmlDom = Html::load($content); $elements = simplexml_import_dom($htmlDom); return $elements; reverted: --- core/modules/views/tests/src/Kernel/Plugin/StyleTest.php +++ core/modules/views/tests/src/Kernel/Plugin/StyleTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\views\Kernel\Plugin; -use Drupal\Component\Utility\Html; use Drupal\Tests\views\Kernel\ViewsKernelTestBase; use Drupal\views\Views; use Drupal\views_test_data\Plugin\views\row\RowTest; @@ -310,7 +309,8 @@ * The HTML DOM. */ protected function getHtmlDom($output) { + $html_dom = new \DOMDocument(); + @$html_dom->loadHTML($output); - $html_dom = Html::load($output); if ($html_dom) { // It's much easier to work with simplexml than DOM, luckily enough // we can just simply import our DOM tree. reverted: --- core/modules/views/tests/src/Kernel/Plugin/StyleTestBase.php +++ core/modules/views/tests/src/Kernel/Plugin/StyleTestBase.php @@ -2,8 +2,8 @@ namespace Drupal\Tests\views\Kernel\Plugin; -use Drupal\Component\Utility\Html; use Drupal\Tests\views\Kernel\ViewsKernelTestBase; +use Masterminds\HTML5; /** * Tests some general style plugin related functionality. @@ -21,7 +21,8 @@ * Stores a view output in the elements. */ public function storeViewPreview($output) { + $html5 = new HTML5(); + $htmlDom = $html5->loadHTML('' . $output . ''); - $htmlDom = Html::load($output); if ($htmlDom) { // It's much easier to work with simplexml than DOM, luckily enough // we can just simply import our DOM tree. reverted: --- core/tests/Drupal/KernelTests/AssertContentTrait.php +++ core/tests/Drupal/KernelTests/AssertContentTrait.php @@ -122,21 +122,14 @@ */ protected function parse() { if (!isset($this->elements)) { + // DOM can load HTML soup. But, HTML soup can throw warnings, suppress + // them. + $html_dom = new \DOMDocument(); + @$html_dom->loadHTML('' . $this->getRawContent(), LIBXML_NOBLANKS); + if ($html_dom) { - $content = $this->getRawContent(); - - // This is an XML document. - if (stripos(ltrim($content), 'loadXML($content); - } - else { - $dom = Html::load($content); - } - - if ($dom) { // It's much easier to work with simplexml than DOM, luckily enough // we can just simply import our DOM tree. + $this->elements = simplexml_import_dom($html_dom); - $this->elements = @simplexml_import_dom($dom); } } $this->assertNotFalse($this->elements, 'The current HTML page should be available for DOM navigation.'); reverted: --- core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\Core\Template; -use Drupal\Component\Render\MarkupInterface; use Drupal\Component\Utility\Html; use Drupal\Core\Render\Markup; use Drupal\Core\Template\Attribute; @@ -10,6 +9,7 @@ use Drupal\Core\Template\AttributeString; use Drupal\Core\Template\Loader\StringLoader; use Drupal\Tests\UnitTestCase; +use Drupal\Component\Render\MarkupInterface; use Twig\Environment; /** @@ -447,7 +447,8 @@ * The number of results that are found. */ protected function getXPathResultCount($query, $html) { + $document = new \DOMDocument(); + $document->loadHTML($html); - $document = Html::load($html); $xpath = new \DOMXPath($document); return $xpath->query($query)->length; reverted: --- core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php +++ core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php @@ -3,8 +3,6 @@ namespace Drupal\Tests\Core\Utility; use Drupal\Component\Render\MarkupInterface; -use Drupal\Component\Utility\Html; -use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\GeneratedButton; use Drupal\Core\GeneratedNoLink; use Drupal\Core\GeneratedUrl; @@ -14,6 +12,7 @@ use Drupal\Core\Url; use Drupal\Core\Utility\LinkGenerator; use Drupal\Tests\UnitTestCase; +use Drupal\Core\DependencyInjection\ContainerBuilder; /** * @coversDefaultClass \Drupal\Core\Utility\LinkGenerator @@ -677,7 +676,8 @@ } // Execute the query. + $document = new \DOMDocument(); + $document->loadHTML($html); - $document = Html::load($html); $xpath = new \DOMXPath($document); self::assertEquals($count, $xpath->query($query)->length); @@ -694,7 +694,8 @@ * @internal */ protected function assertNoXPathResults(string $query, string $html): void { + $document = new \DOMDocument(); + $document->loadHTML($html); - $document = Html::load($html); $xpath = new \DOMXPath($document); self::assertFalse((bool) $xpath->query($query)->length); only in patch2: unchanged: --- a/core/modules/help/tests/src/Functional/HelpTopicsSyntaxTest.php +++ b/core/modules/help/tests/src/Functional/HelpTopicsSyntaxTest.php @@ -4,9 +4,11 @@ use Drupal\Core\Extension\ExtensionLifecycle; use Drupal\Component\FrontMatter\FrontMatter; +use Drupal\Component\Utility\Html; use Drupal\Tests\BrowserTestBase; use Drupal\help\HelpTopicDiscovery; use Drupal\help_topics_twig_tester\HelpTestTwigNodeVisitor; +use Masterminds\HTML5; use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\AssertionFailedError; @@ -187,18 +189,19 @@ protected function verifyTopic($id, $definitions, $response = 200) { * ID of help topic (for error messages). */ protected function validateHtml(string $body, string $id) { - $doc = new \DOMDocument(); - $doc->strictErrorChecking = TRUE; - $doc->validateOnParse = FALSE; - libxml_use_internal_errors(TRUE); - if (!$doc->loadXML('' . $body . '')) { - foreach (libxml_get_errors() as $error) { - $this->fail('Topic ' . $id . ' fails HTML validation: ' . $error->message); + // Inspired on \Drupal\Component\Utility\Html::load(), but keeping the + // HTML5 object to be able to use it later, and use the fragment related + // method. + $html5 = new HTML5(['disable_html_ns' => TRUE, 'encoding' => 'UTF-8']); + $doc = $html5->loadHTMLFragment($body); + if ($html5->hasErrors()) { + foreach ($html5->getErrors() as $error) { + $this->fail('Topic ' . $id . ' fails HTML validation: ' . $error); } - - libxml_clear_errors(); } + // Load it again, as full document, to walk over it. + $doc = Html::load($body); // Check for headings hierarchy. $levels = [1, 2, 3, 4, 5, 6]; foreach ($levels as $level) { @@ -245,7 +248,7 @@ protected function verifyBadTopic($id, $definitions) { case 'bad_html': case 'bad_html2': case 'bad_html3': - $this->assertStringContainsString('Opening and ending tag mismatch', $message); + $this->assertStringContainsString('Could not find closing tag for', $message); break; case 'top_level': only in patch2: unchanged: --- a/core/modules/layout_discovery/tests/src/Kernel/LayoutTest.php +++ b/core/modules/layout_discovery/tests/src/Kernel/LayoutTest.php @@ -71,7 +71,7 @@ public function testRenderLayout($layout_id, $config, $regions, array $html) { $build_id_input = $this->cssSelect('input[name="form_build_id"]')[0]->asXML(); $form_id_input = ''; $html[] = 'Test suffix'; - $html[] = $build_id_input . $form_id_input . ''; + $html[] = $build_id_input . "\n" . $form_id_input . "\n" . ''; // Match the HTML to the full form element. $this->assertSame(implode("\n", $html), $this->cssSelect('#the-form-id')[0]->asXML()); only in patch2: unchanged: --- a/core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php +++ b/core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php @@ -480,8 +480,7 @@ public function testDrupalPreRenderLinks() { // it. $render_array = $base_array; $html = \Drupal::service('renderer')->renderRoot($render_array); - $dom = new \DOMDocument(); - $dom->loadHTML($html); + $dom = Html::load($html); $this->assertEquals(1, $dom->getElementsByTagName('ul')->length, 'One "ul" tag found in the rendered HTML.'); $list_elements = $dom->getElementsByTagName('li'); $this->assertEquals(3, $list_elements->length, 'Three "li" tags found in the rendered HTML.'); @@ -498,16 +497,14 @@ public function testDrupalPreRenderLinks() { $child_html = \Drupal::service('renderer')->renderRoot($render_array['first_child']); $parent_html = \Drupal::service('renderer')->renderRoot($render_array); // First check the child HTML. - $dom = new \DOMDocument(); - $dom->loadHTML($child_html); + $dom = Html::load($child_html); $this->assertEquals(1, $dom->getElementsByTagName('ul')->length, 'One "ul" tag found in the rendered child HTML.'); $list_elements = $dom->getElementsByTagName('li'); $this->assertEquals(2, $list_elements->length, 'Two "li" tags found in the rendered child HTML.'); $this->assertEquals('Parent link copy', $list_elements->item(0)->nodeValue, 'First expected link found.'); $this->assertEquals('First child link', $list_elements->item(1)->nodeValue, 'Second expected link found.'); // Then check the parent HTML. - $dom = new \DOMDocument(); - $dom->loadHTML($parent_html); + $dom = Html::load($parent_html); $this->assertEquals(1, $dom->getElementsByTagName('ul')->length, 'One "ul" tag found in the rendered parent HTML.'); $list_elements = $dom->getElementsByTagName('li'); $this->assertEquals(2, $list_elements->length, 'Two "li" tags found in the rendered parent HTML.'); only in patch2: unchanged: --- a/core/modules/views/tests/src/Functional/Handler/FieldWebTest.php +++ b/core/modules/views/tests/src/Functional/Handler/FieldWebTest.php @@ -177,8 +177,7 @@ protected function assertNotSubString(string $haystack, string $needle, string $ * An array containing simplexml objects. */ protected function parseContent($content) { - $htmlDom = new \DOMDocument(); - @$htmlDom->loadHTML('' . $content); + $htmlDom = Html::load($content); $elements = simplexml_import_dom($htmlDom); return $elements; only in patch2: unchanged: --- a/core/modules/views/tests/src/Kernel/Plugin/StyleTest.php +++ b/core/modules/views/tests/src/Kernel/Plugin/StyleTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\views\Kernel\Plugin; +use Drupal\Component\Utility\Html; use Drupal\Tests\views\Kernel\ViewsKernelTestBase; use Drupal\views\Views; use Drupal\views_test_data\Plugin\views\row\RowTest; @@ -309,8 +310,7 @@ public function testCustomRowClasses() { * The HTML DOM. */ protected function getHtmlDom($output) { - $html_dom = new \DOMDocument(); - @$html_dom->loadHTML($output); + $html_dom = Html::load($output); if ($html_dom) { // It's much easier to work with simplexml than DOM, luckily enough // we can just simply import our DOM tree. only in patch2: unchanged: --- a/core/modules/views/tests/src/Kernel/Plugin/StyleTestBase.php +++ b/core/modules/views/tests/src/Kernel/Plugin/StyleTestBase.php @@ -2,8 +2,8 @@ namespace Drupal\Tests\views\Kernel\Plugin; +use Drupal\Component\Utility\Html; use Drupal\Tests\views\Kernel\ViewsKernelTestBase; -use Masterminds\HTML5; /** * Tests some general style plugin related functionality. @@ -21,8 +21,7 @@ * Stores a view output in the elements. */ public function storeViewPreview($output) { - $html5 = new HTML5(); - $htmlDom = $html5->loadHTML('' . $output . ''); + $htmlDom = Html::load($output); if ($htmlDom) { // It's much easier to work with simplexml than DOM, luckily enough // we can just simply import our DOM tree. only in patch2: unchanged: --- a/core/tests/Drupal/KernelTests/AssertContentTrait.php +++ b/core/tests/Drupal/KernelTests/AssertContentTrait.php @@ -122,14 +122,21 @@ protected function setDrupalSettings($settings) { */ protected function parse() { if (!isset($this->elements)) { - // DOM can load HTML soup. But, HTML soup can throw warnings, suppress - // them. - $html_dom = new \DOMDocument(); - @$html_dom->loadHTML('' . $this->getRawContent(), LIBXML_NOBLANKS); - if ($html_dom) { + $content = $this->getRawContent(); + + // This is an XML document. + if (stripos(ltrim($content), 'loadXML($content); + } + else { + $dom = Html::load($content); + } + + if ($dom) { // It's much easier to work with simplexml than DOM, luckily enough // we can just simply import our DOM tree. - $this->elements = simplexml_import_dom($html_dom); + $this->elements = @simplexml_import_dom($dom); } } $this->assertNotFalse($this->elements, 'The current HTML page should be available for DOM navigation.'); only in patch2: unchanged: --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\Core\Template; +use Drupal\Component\Render\MarkupInterface; use Drupal\Component\Utility\Html; use Drupal\Core\Render\Markup; use Drupal\Core\Template\Attribute; @@ -9,7 +10,6 @@ use Drupal\Core\Template\AttributeString; use Drupal\Core\Template\Loader\StringLoader; use Drupal\Tests\UnitTestCase; -use Drupal\Component\Render\MarkupInterface; use Twig\Environment; /** @@ -447,8 +447,7 @@ protected function assertNoID(string $id, string $html): void { * The number of results that are found. */ protected function getXPathResultCount($query, $html) { - $document = new \DOMDocument(); - $document->loadHTML($html); + $document = Html::load($html); $xpath = new \DOMXPath($document); return $xpath->query($query)->length; only in patch2: unchanged: --- a/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php +++ b/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php @@ -3,6 +3,8 @@ namespace Drupal\Tests\Core\Utility; use Drupal\Component\Render\MarkupInterface; +use Drupal\Component\Utility\Html; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\GeneratedButton; use Drupal\Core\GeneratedNoLink; use Drupal\Core\GeneratedUrl; @@ -12,7 +14,6 @@ use Drupal\Core\Url; use Drupal\Core\Utility\LinkGenerator; use Drupal\Tests\UnitTestCase; -use Drupal\Core\DependencyInjection\ContainerBuilder; /** * @coversDefaultClass \Drupal\Core\Utility\LinkGenerator @@ -676,8 +677,7 @@ public static function assertLink(array $properties, MarkupInterface $html, int } // Execute the query. - $document = new \DOMDocument(); - $document->loadHTML($html); + $document = Html::load($html); $xpath = new \DOMXPath($document); self::assertEquals($count, $xpath->query($query)->length); @@ -694,8 +694,7 @@ public static function assertLink(array $properties, MarkupInterface $html, int * @internal */ protected function assertNoXPathResults(string $query, string $html): void { - $document = new \DOMDocument(); - $document->loadHTML($html); + $document = Html::load($html); $xpath = new \DOMXPath($document); self::assertFalse((bool) $xpath->query($query)->length);