Skip to content

Commit

Permalink
WIP Add content filter
Browse files Browse the repository at this point in the history
  • Loading branch information
tkcreateit committed Jun 25, 2024
1 parent e90743b commit 1503393
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Service/HtmlTagRemover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

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

declare(strict_types=1);

namespace OxidEsales\WysiwygModule\Service;

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

class HtmlTagRemover implements HtmlRemoverInterface
{
public function remove(DOMNode $node): void
{
$parent = $node->parentNode;
while ($node->hasChildNodes()) {
$parent->insertBefore($node->lastChild, $node->nextSibling);
}
$parent->removeChild($node);
}
}
54 changes: 54 additions & 0 deletions tests/Unit/Service/HtmlTagRemoverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

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

declare(strict_types=1);

namespace OxidEsales\WysiwygModule\Tests\Unit\Service;

use DOMDocument;
use OxidEsales\WysiwygModule\Service\HtmlTagRemover;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class HtmlTagRemoverTest extends TestCase
{
#[Test]
#[DataProvider('htmlProvider')]
public function remove(string $node, string $html, string $expectedHtml): void
{
$doc = new DOMDocument();
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$node = $doc->getElementsByTagName($node)->item(0);

$remover = new HtmlTagRemover();
$remover->remove($node);

$this->assertEquals($expectedHtml, rtrim($doc->saveHTML()));
}

public static function htmlProvider(): array
{
return [
[
'node' => 'span',
'html' => '<div><span>content</span></div>',
'expectedHtml' => '<div>content</div>',
],
[
'node' => 'script',
'html' => '<div><script>//content</script></div>',
'expectedHtml' => '<div>//content</div>',
],
[
'node' => 'script',
'html' => '<div><script src="app.js"/></div>',
'expectedHtml' => '<div></div>',
],
];
}
}

0 comments on commit 1503393

Please sign in to comment.