From b572913c8d899fb0c7f44761cc803a305ff1f7d7 Mon Sep 17 00:00:00 2001 From: Wojciech Peisert Date: Wed, 1 Apr 2020 16:47:10 +0200 Subject: [PATCH] Fix loadHTML --- .../Service/DomService.php | 36 +++++++++++++++++++ .../Service/Wysiwyg/WysiwygService.php | 26 +++++++++----- 2 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/PimcoreDevkitBundle/Service/DomService.php diff --git a/src/PimcoreDevkitBundle/Service/DomService.php b/src/PimcoreDevkitBundle/Service/DomService.php new file mode 100644 index 0000000..f5b74bc --- /dev/null +++ b/src/PimcoreDevkitBundle/Service/DomService.php @@ -0,0 +1,36 @@ +loadHTML( + mb_convert_encoding("
$html
", 'HTML-ENTITIES', 'UTF-8') + ); + + $container = $doc->getElementsByTagName('div')->item(0); + $container = $container->parentNode->removeChild($container); + while ($doc->firstChild) { + $doc->removeChild($doc->firstChild); + } + + while ($container->firstChild ) { + $doc->appendChild($container->firstChild); + } + + return $doc; + } +} diff --git a/src/PimcoreDevkitBundle/Service/Wysiwyg/WysiwygService.php b/src/PimcoreDevkitBundle/Service/Wysiwyg/WysiwygService.php index be6a286..2be8819 100644 --- a/src/PimcoreDevkitBundle/Service/Wysiwyg/WysiwygService.php +++ b/src/PimcoreDevkitBundle/Service/Wysiwyg/WysiwygService.php @@ -6,6 +6,7 @@ use PimcoreDevkitBundle\Model\AssetSeoData; use Pimcore\Model\Asset; +use PimcoreDevkitBundle\Service\DomService; /** * Class WysiwygService @@ -24,14 +25,23 @@ class WysiwygService private $assetFinderService; /** - * Wysiwyg constructor. + * @var DomService + */ + private $domService; + + /** * @param AssetSeoService $assetSeoService * @param AssetFinderService $assetFinderService + * @param DomService $domService */ - public function __construct(AssetSeoService $assetSeoService, AssetFinderService $assetFinderService) - { + public function __construct( + AssetSeoService $assetSeoService, + AssetFinderService $assetFinderService, + DomService $domService + ) { $this->assetSeoService = $assetSeoService; $this->assetFinderService = $assetFinderService; + $this->domService = $domService; } /** @@ -40,11 +50,11 @@ public function __construct(AssetSeoService $assetSeoService, AssetFinderService */ public function addMetaToImages(string $html, string $lang = ''): string { - $doc = new \DOMDocument(); - $doc->loadHTML( - mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), - LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD - ); + if (!$html) { + return ''; + } + + $doc = $this->domService->loadHTML($html); /** @var \DOMElement $imgNode */ foreach ($doc->getElementsByTagName('img') as $imgNode) {