diff --git a/core/modules/image/src/Plugin/Filter/FilterImageStyle.php b/core/modules/image/src/Plugin/Filter/FilterImageStyle.php index 7926f41..1c03de5 100644 --- a/core/modules/image/src/Plugin/Filter/FilterImageStyle.php +++ b/core/modules/image/src/Plugin/Filter/FilterImageStyle.php @@ -97,11 +97,12 @@ public function process($text, $langcode) { $dom = HTML::load($text); $xpath = new \DOMXPath($dom); - // Process each img element DOM node found with the necessary attributes. - foreach ($xpath->query('//*[@data-entity-type="file" and @data-entity-uuid and @data-image-style]') as $dom_node) { + // Process each img element DOM element found with the necessary attributes. + /** @var \DOMElement $dom_element */ + foreach ($xpath->query('//*[@data-entity-type="file" and @data-entity-uuid and @data-image-style]') as $dom_element) { // Get the UUID and image style for the file. - $file_uuid = $dom_node->getAttribute('data-entity-uuid'); - $image_style_id = $dom_node->getAttribute('data-image-style'); + $file_uuid = $dom_element->getAttribute('data-entity-uuid'); + $image_style_id = $dom_element->getAttribute('data-image-style'); // If the image style is not a valid one, then don't transform the HTML. if (empty($file_uuid) || !in_array($image_style_id, $image_styles)) { @@ -109,10 +110,10 @@ public function process($text, $langcode) { } // Transform the HTML for the img element by applying an image style. - $altered_html = $this->getImageStyleHtml($file_uuid, $image_style_id, $dom_node); + $altered_img = $this->getImageStyleHtml($file_uuid, $image_style_id, $dom_element); // Load the altered HTML into a new DOMDocument and retrieve the element. - $updated_node = HTML::load($altered_html)->getElementsByTagName('body') + $updated_node = HTML::load($altered_img)->getElementsByTagName('body') ->item(0) ->childNodes ->item(0); @@ -122,7 +123,7 @@ public function process($text, $langcode) { $updated_node = $dom->importNode($updated_node, TRUE); // Finally, replace the original image node with the new image node. - $dom_node->parentNode->replaceChild($updated_node, $dom_node); + $dom_element->parentNode->replaceChild($updated_node, $dom_element); } // Process the filter with the newly updated DOM. @@ -176,22 +177,22 @@ protected function getImageInfo($file_uuid) { /** * Removes attributes that will be generated from image style theme function. * - * @param \DOMNode $dom_node - * The DOM node for the img element. + * @param \DOMElement $dom_element + * The DOM element for the img element. * * @return array * The attributes array. */ - protected function prepareImageAttributes($dom_node) { + protected function prepareImageAttributes(\DOMElement $dom_element) { // Make sure all non-regenerated attributes are retained. - $dom_node->removeAttribute('data-entity-uuid'); - $dom_node->removeAttribute('data-image-style'); - $dom_node->removeAttribute('width'); - $dom_node->removeAttribute('height'); - $dom_node->removeAttribute('src'); + $dom_element->removeAttribute('data-entity-uuid'); + $dom_element->removeAttribute('data-image-style'); + $dom_element->removeAttribute('width'); + $dom_element->removeAttribute('height'); + $dom_element->removeAttribute('src'); $attributes = array(); - for ($i = 0; $i < $dom_node->attributes->length; $i++) { - $attr = $dom_node->attributes->item($i); + for ($i = 0; $i < $dom_element->attributes->length; $i++) { + $attr = $dom_element->attributes->item($i); $attributes[$attr->name] = $attr->value; } @@ -205,20 +206,20 @@ protected function prepareImageAttributes($dom_node) { * The UUID for the file. * @param string $image_style_id * The ID for the image style. - * @param \DOMNode $dom_node - * The DOM node for the image element. + * @param \DOMElement $dom_element + * The DOM element for the image element. * * @return string * The img element with the image style applied. */ - protected function getImageStyleHtml($file_uuid, $image_style_id, \DOMNode $dom_node) { + protected function getImageStyleHtml($file_uuid, $image_style_id, \DOMElement $dom_element) { $image_info = $this->getImageInfo($file_uuid); $image_uri = $image_info['uri']; $image_width = $image_info['width']; $image_height = $image_info['height']; // Remove attributes that will be generated by the image style. - $attributes = $this->prepareImageAttributes($dom_node); + $attributes = $this->prepareImageAttributes($dom_element); // Re-render as an image style. $image = array( diff --git a/core/modules/image/tests/src/Unit/FilterImageStyleTest.php b/core/modules/image/tests/src/Unit/FilterImageStyleTest.php index d1f9e4f..3f853f4 100644 --- a/core/modules/image/tests/src/Unit/FilterImageStyleTest.php +++ b/core/modules/image/tests/src/Unit/FilterImageStyleTest.php @@ -51,12 +51,11 @@ protected function setUp() { $plugin_definition, $this->entityTypeManager->reveal(), $this->entityRepository->reveal(), - $this->imageFactory->reveal(),$this->renderer->reveal() + $this->imageFactory->reveal(), + $this->renderer->reveal() ]) ->setMethods([ 'loadImageStyles', - 'getImageInfo', - 'prepareImageAttributes', 'getImageStyleHtml' ]) ->getMock(); @@ -79,20 +78,14 @@ public function testProcessWithImage() { $original_alt = 'A wooly mammoth trumpets as a crevasse breaks open in the glacier.'; $original_img = '' . $original_alt .''; - $original_img_doc = new \DOMDocument($original_img); - $original_img_node = $original_img_doc->createElement('img'); - - $processed_img = '' . $original_alt .''; - $processed_img_doc = new \DOMDocument($processed_img); - $processed_img_node = $processed_img_doc->createElement('img'); + $original_text = '

' . $original_img . '

'; $generated_src = 'styles/medium/public/image.png'; $generated_width = '200'; $generated_height = '150'; $generated_img = '' . $original_alt .''; - $generated_img_doc = new \DOMDocument($generated_img); - $generated_img_node = $generated_img_doc->createElement('img'); + $generated_text = '

' . $generated_img . '

'; $this->filterImageStyle ->method('loadImageStyles') @@ -103,33 +96,15 @@ public function testProcessWithImage() { ]); $this->filterImageStyle - ->method('getImageInfo') - ->with( - $this->equalTo($original_uuid) - ) - ->willReturn([ - 'uri' => 'styles/medium/public/image.png', - 'width' => '200', - 'height'=> '150' - ]); - - $this->filterImageStyle - ->method('prepareImageAttributes') - ->with( - $this->equalTo($original_img_node) - ) - ->willReturn($processed_img_node); - - $this->filterImageStyle ->method('getImageStyleHtml') ->with( $this->equalTo($original_uuid), $this->equalTo($original_image_style), - $this->equalTo($original_img_node) + $this->anything() ) - ->willReturn($generated_img_node); + ->willReturn($generated_img); - $output = $this->filterImageStyle->process($original_img, 'en'); - $this->assertEquals($generated_img_node, $output); + $output = $this->filterImageStyle->process($original_text, 'en'); + $this->assertEquals($generated_text, $output); } }