Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Correctly import nodes with different owner document #114

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ protected function replaceNode(DOMNode $source, ?DOMNode $target): ?DOMNode
if ($target === null) {
$source->parentNode->removeChild($source);
} elseif ($source !== $target) {
if ($source->ownerDocument !== $target->ownerDocument) {
$source->ownerDocument->importNode($target);
if ($source->ownerDocument !== $target->ownerDocument
&& $source->ownerDocument !== null
&& $target->ownerDocument !== null
) {
$target = $source->ownerDocument->importNode($target, true);
}
$source->parentNode->replaceChild($target, $source);
}
Expand Down
9 changes: 6 additions & 3 deletions src/Visitor/CommonVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public function leaveNode(DOMNode $domNode): ?DOMNode
if (!$domNode instanceof DOMElement) {
return $domNode;
}
$tag = $this->behavior->getTag($domNode->nodeName);
if ($tag === null) {
$node = $this->behavior->getNode($domNode->nodeName);
if ($node === null) {
// pass custom elements, in case it has been declared
if ($this->behavior->shallAllowCustomElements() && $this->isCustomElement($domNode)) {
return $domNode;
Expand All @@ -124,7 +124,10 @@ public function leaveNode(DOMNode $domNode): ?DOMNode
return null;
}
// completely remove node, in case it is expected to exist with children only
if (!$this->hasNonEmptyChildren($domNode) && $tag->shallPurgeWithoutChildren()) {
if ($node instanceof Behavior\Tag
&& $node->shallPurgeWithoutChildren()
&& !$this->hasNonEmptyChildren($domNode)
) {
return null;
}
return $domNode;
Expand Down
36 changes: 36 additions & 0 deletions tests/ScenarioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,42 @@ public function tagIsHandled(Behavior\NodeHandler $nodeHandler, string $payload,
self::assertSame($expectation, $sanitizer->sanitize($payload));
}

/**
* @test
*/
public function tagHandlingIsDelegated(): void
{
$behavior = (new Behavior())
->withFlags(Behavior::REMOVE_UNEXPECTED_CHILDREN)
->withName('scenario-test')
->withTags(new Behavior\Tag('div', Behavior\Tag::ALLOW_CHILDREN))
->withNodes(
new Behavior\NodeHandler(
new Behavior\Tag('my-placeholder'),
new Behavior\Handler\ClosureHandler(
static function (NodeInterface $node, ?DOMNode $domNode): ?\DOMNode {
if ($domNode === null) {
return null;
}
$newDocument = new \DOMDocument();
$text = $newDocument->createTextNode($domNode->textContent);
$span = $newDocument->createElement('div');
$span->setAttribute('class', 'delegated');
$span->appendChild($text);
return $span;
}
)
)
);
$sanitizer = new Sanitizer(
$behavior,
new CommonVisitor($behavior)
);
$payload = '<div><my-placeholder><span class="inner">value</span></my-placeholder></div>';
$expectation = '<div><div class="delegated">value</div></div>';
self::assertSame($expectation, $sanitizer->sanitize($payload));
}

public static function commentsAreHandledDataProvider(): array
{
return [
Expand Down