Skip to content

Commit

Permalink
OXDEV-7182 Move HtmlFilter classes into the module
Browse files Browse the repository at this point in the history
  • Loading branch information
tkcreateit committed Aug 29, 2024
1 parent 6785431 commit 9a4a03e
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 17 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [4.0.1] - unreleased
## [4.1.0] - unreleased

### Fixed
- Pre-filter CMS content before it is passed to Summernote editor

### Added
- `HtmlTagRemover` class
- `HtmlFilter` and `HtmlTagRemover` class

## [4.0.0] - 2024-03-12

Expand Down
1 change: 1 addition & 0 deletions services.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports:
- { resource: src/Service/services.yaml }
- { resource: src/HtmlFilter/services.yaml }

services:
_defaults:
Expand Down
43 changes: 43 additions & 0 deletions src/HtmlFilter/HtmlFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\WysiwygModule\HtmlFilter;

use DOMDocument;
use DOMXPath;

class HtmlFilter implements HtmlFilterInterface
{
public function __construct(private readonly HtmlRemoverInterface $htmlRemover)
{
}

public function filter(string $html): string
{
$doc = new DOMDocument();
$doc->loadHTML("<div>$html</div>", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$xpath = new DOMXPath($doc);
foreach ($xpath->query('//script') as $node) {
$this->htmlRemover->remove($node);
}

return $this->getInnerHtml($doc);
}

private function getInnerHtml(DOMDocument $doc): string
{
$html = '';
foreach ($doc->documentElement->childNodes as $node) {
$html .= $doc->saveHTML($node);
}

return $html;
}
}
15 changes: 15 additions & 0 deletions src/HtmlFilter/HtmlFilterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\WysiwygModule\HtmlFilter;

interface HtmlFilterInterface
{
public function filter(string $html): string;
}
17 changes: 17 additions & 0 deletions src/HtmlFilter/HtmlRemoverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\WysiwygModule\HtmlFilter;

use DOMNode;

interface HtmlRemoverInterface
{
public function remove(DOMNode $node): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

declare(strict_types=1);

namespace OxidEsales\WysiwygModule\Service;
namespace OxidEsales\WysiwygModule\HtmlFilter;

use DOMNode;
use InvalidArgumentException;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\HtmlFilter\HtmlRemoverInterface;

class HtmlTagRemover implements HtmlRemoverInterface
{
Expand Down
12 changes: 12 additions & 0 deletions src/HtmlFilter/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
_defaults:
public: false
autowire: true

OxidEsales\WysiwygModule\HtmlFilter\HtmlRemoverInterface:
class: OxidEsales\WysiwygModule\HtmlFilter\HtmlTagRemover
public: true

OxidEsales\WysiwygModule\HtmlFilter\HtmlFilterInterface:
class: OxidEsales\WysiwygModule\HtmlFilter\HtmlFilter
public: true
2 changes: 1 addition & 1 deletion src/Service/EditorRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace OxidEsales\WysiwygModule\Service;

use OxidEsales\EshopCommunity\Internal\Framework\Templating\HtmlFilter\HtmlFilterInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererInterface;
use OxidEsales\WysiwygModule\HtmlFilter\HtmlFilterInterface;

class EditorRenderer implements EditorRendererInterface
{
Expand Down
10 changes: 0 additions & 10 deletions src/Service/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,3 @@ services:
OxidEsales\WysiwygModule\Service\EditorRendererInterface:
class: OxidEsales\WysiwygModule\Service\EditorRenderer
public: true

OxidEsales\EshopCommunity\Internal\Framework\Templating\HtmlFilter\HtmlRemoverInterface:
class: OxidEsales\WysiwygModule\Service\HtmlTagRemover
public: true

OxidEsales\EshopCommunity\Internal\Framework\Templating\HtmlFilter\HtmlFilterInterface:
class: OxidEsales\EshopCommunity\Internal\Framework\Templating\HtmlFilter\HtmlFilter
public: true
arguments:
$htmlRemover: '@OxidEsales\EshopCommunity\Internal\Framework\Templating\HtmlFilter\HtmlRemoverInterface'
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

declare(strict_types=1);

namespace OxidEsales\WysiwygModule\Tests\Unit\Service;
namespace OxidEsales\WysiwygModule\Tests\Unit\HtmlFilter;

use DOMDocument;
use InvalidArgumentException;
use OxidEsales\WysiwygModule\Service\HtmlTagRemover;
use OxidEsales\WysiwygModule\HtmlFilter\HtmlTagRemover;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
Expand Down

0 comments on commit 9a4a03e

Please sign in to comment.