From 642d2c7d58767cb75130a1c40a938d4da4dcff41 Mon Sep 17 00:00:00 2001 From: JakeQZ Date: Mon, 23 Sep 2024 17:46:12 +0100 Subject: [PATCH] [TASK] Add `AbstractHtmlProcessor::getHtmlElement()` (#1335) This is `protected` so that it can be used by subclasses, e.g. for iterating the DOM tree (which is likely for #1276). --- src/HtmlProcessor/AbstractHtmlProcessor.php | 23 +++++++++++++++---- .../AbstractHtmlProcessorTest.php | 12 ++++++++++ .../Fixtures/TestingHtmlProcessor.php | 8 ++++++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/HtmlProcessor/AbstractHtmlProcessor.php b/src/HtmlProcessor/AbstractHtmlProcessor.php index 71754851..9c6eb6f9 100644 --- a/src/HtmlProcessor/AbstractHtmlProcessor.php +++ b/src/HtmlProcessor/AbstractHtmlProcessor.php @@ -197,6 +197,23 @@ private function removeSelfClosingTagsClosingTags(string $html): string return (new Preg())->replace('%%', '', $html); } + /** + * Returns the HTML element. + * + * This method assumes that there always is an HTML element, throwing an exception otherwise. + * + * @throws \UnexpectedValueException + */ + protected function getHtmlElement(): \DOMElement + { + $htmlElement = $this->getDomDocument()->getElementsByTagName('html')->item(0); + if (!$htmlElement instanceof \DOMElement) { + throw new \UnexpectedValueException('There is no HTML element although there should be one.', 1569930853); + } + + return $htmlElement; + } + /** * Returns the BODY element. * @@ -456,10 +473,6 @@ private function ensureExistenceOfBodyElement(): void return; } - $htmlElement = $this->getDomDocument()->getElementsByTagName('html')->item(0); - if (!$htmlElement instanceof \DOMElement) { - throw new \UnexpectedValueException('There is no HTML element although there should be one.', 1569930853); - } - $htmlElement->appendChild($this->getDomDocument()->createElement('body')); + $this->getHtmlElement()->appendChild($this->getDomDocument()->createElement('body')); } } diff --git a/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php b/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php index 1c891ad5..5e2102ac 100644 --- a/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php +++ b/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php @@ -59,6 +59,18 @@ public function fromDomDocumentReturnsInstanceOfCalledClass(): void self::assertInstanceOf(TestingHtmlProcessor::class, $subject); } + /** + * @test + */ + public function getHtmlElementReturnsHtmlElement(): void + { + $subject = TestingHtmlProcessor::fromHtml('

'); + + $result = $subject->callGetHtmlElement(); + + self::assertSame('html', $result->tagName); + } + /** * @test */ diff --git a/tests/Unit/HtmlProcessor/Fixtures/TestingHtmlProcessor.php b/tests/Unit/HtmlProcessor/Fixtures/TestingHtmlProcessor.php index 3686cb81..f8f4351b 100644 --- a/tests/Unit/HtmlProcessor/Fixtures/TestingHtmlProcessor.php +++ b/tests/Unit/HtmlProcessor/Fixtures/TestingHtmlProcessor.php @@ -9,4 +9,10 @@ /** * Fixture class for AbstractHtmlProcessor. */ -final class TestingHtmlProcessor extends AbstractHtmlProcessor {} +final class TestingHtmlProcessor extends AbstractHtmlProcessor +{ + public function callGetHtmlElement(): \DOMElement + { + return $this->getHtmlElement(); + } +}