Skip to content

Commit

Permalink
Merge pull request #31 from DivanteLtd/fix_load_html
Browse files Browse the repository at this point in the history
Fix loadHTML
  • Loading branch information
wpeisert authored Apr 1, 2020
2 parents adf5e71 + b572913 commit af1d081
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
36 changes: 36 additions & 0 deletions src/PimcoreDevkitBundle/Service/DomService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace PimcoreDevkitBundle\Service;

/**
* Class DomService
* @package PimcoreDevkitBundle\Service
*/
class DomService
{
/**
* @param string $html
* @return \DOMDocument
*/
public function loadHTML(string $html): \DOMDocument
{
$doc = new \DOMDocument();
$doc->loadHTML(
mb_convert_encoding("<div>$html</div>", '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;
}
}
26 changes: 18 additions & 8 deletions src/PimcoreDevkitBundle/Service/Wysiwyg/WysiwygService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PimcoreDevkitBundle\Model\AssetSeoData;
use Pimcore\Model\Asset;
use PimcoreDevkitBundle\Service\DomService;

/**
* Class WysiwygService
Expand All @@ -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;
}

/**
Expand All @@ -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) {
Expand Down

0 comments on commit af1d081

Please sign in to comment.