Skip to content

Commit

Permalink
Merge pull request #5 from hyvor/promote-removed
Browse files Browse the repository at this point in the history
fixes the position bug when promoting
  • Loading branch information
supun-io authored Mar 28, 2024
2 parents 234ad5a + 96f93ea commit 97af2b6
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 91 deletions.
24 changes: 17 additions & 7 deletions src/Content/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,38 @@ private function sanitizeNode(
removedAddableNodes: $removedAddableNodes
);

$removedNodes = [];

foreach ($content as $child) {
foreach ($content as $index => $child) {
if ($child->content->count() > 0) {
$removedNodes = [];
$this->sanitizeNode($child, $removedNodes);

if (count($removedNodes)) {
$this->tryAddRemovedNodesToParent($node, $index, $removedNodes);
}
}
}

$this->tryAddRemovedNodesToParent($node, $removedNodes);

}

/**
* @param Node[] $removedNodes
*/
private function tryAddRemovedNodesToParent(Node $parent, array $removedNodes) : void
private function tryAddRemovedNodesToParent(Node $parent, int $insertAfter, array $removedNodes) : void
{

$clone = DeepCopy::copy($parent);
foreach ($removedNodes as $removedNode) {
$clone->content->addNode($removedNode);
}

$contentNodes = $clone->content->all();
array_splice(
$contentNodes,
$insertAfter + 1,
0,
array_reverse($removedNodes)
);
$clone->content->setNodes($contentNodes);

if ($this->matchChildren($clone)) {
$parent->content->setNodes($clone->content->all());
}
Expand Down
226 changes: 142 additions & 84 deletions tests/Unit/Content/SanitizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,108 +585,166 @@ class SanitizerText extends NodeType

});

#bug
describe('promotes to parent if removed', function() {

it('success', function() {

$schema = ($this->getSchema)();

$doc = Document::fromJson($schema, [
'type' => 'doc',
'content' => [
[
'type' => 'figure',
'content' => [
[
'type' => 'blockquote',
'content' => [
[
'type' => 'paragraph',
'content' => [
['type' => 'text', 'text' => 'hello']
]
it('success', function() {

$schema = ($this->getSchema)();

$doc = Document::fromJson($schema, [
'type' => 'doc',
'content' => [
[
'type' => 'figure',
'content' => [
[
'type' => 'blockquote',
'content' => [
[
'type' => 'paragraph',
'content' => [
['type' => 'text', 'text' => 'hello']
]
]
]
]
],
]
]);

$sanitized = Sanitizer::sanitize($schema, $doc);

expect($sanitized->toArray())->toBe([
'type' => 'doc',
'content' => [
[
'type' => 'figure',
],
[
'type' => 'blockquote',
'content' => [
[
'type' => 'paragraph',
'content' => [
['type' => 'text', 'text' => 'hello']
]
]
],
]
]);

$sanitized = Sanitizer::sanitize($schema, $doc);

expect($sanitized->toArray())->toBe([
'type' => 'doc',
'content' => [
[
'type' => 'figure',
],
[
'type' => 'blockquote',
'content' => [
[
'type' => 'paragraph',
'content' => [
['type' => 'text', 'text' => 'hello']
]
]
]
]
]);
]
]);

});


it('ignores when the parent does not accept it', function() {

$schema = ($this->getSchema)(new class extends NodeType {
public string $name = 'doc';
public ?string $content = 'paragraph figure';
});

it('ignores when the parent does not accept it', function() {

$schema = ($this->getSchema)(new class extends NodeType {
public string $name = 'doc';
public ?string $content = 'paragraph figure';
});

$doc = Document::fromJson($schema, [
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [['type' => 'text', 'text' => 'hello']]
],
[
'type' => 'figure',
'content' => [
[
'type' => 'blockquote',
'content' => [
[
'type' => 'paragraph',
'content' => [
['type' => 'text', 'text' => 'hello']
]
$doc = Document::fromJson($schema, [
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [['type' => 'text', 'text' => 'hello']]
],
[
'type' => 'figure',
'content' => [
[
'type' => 'blockquote',
'content' => [
[
'type' => 'paragraph',
'content' => [
['type' => 'text', 'text' => 'hello']
]
]
]
]
],
]
],
]
]);

$sanitized = Sanitizer::sanitize($schema, $doc);

expect($sanitized->toArray())->toBe([
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [['type' => 'text', 'text' => 'hello']]
],
[
'type' => 'figure',
]
]);

$sanitized = Sanitizer::sanitize($schema, $doc);

expect($sanitized->toArray())->toBe([
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [['type' => 'text', 'text' => 'hello']]
],
[
'type' => 'figure',
]
]);

});


it('promotes to parent if removed - when there are other elements after', function() {

$schema = ($this->getSchema)();

$doc = Document::fromJson($schema, [
'type' => 'doc',
'content' => [
[
'type' => 'figure',
'content' => [
[
'type' => 'blockquote',
'content' => [
[
'type' => 'paragraph',
'content' => [
['type' => 'text', 'text' => 'hello']
]
]
]
]
]
],
[
'type' => 'paragraph',
'content' => [
['type' => 'text', 'text' => 'world']
]
]
]);
]
]);

});
$sanitized = Sanitizer::sanitize($schema, $doc);

});
expect($sanitized->toArray())->toBe([
'type' => 'doc',
'content' => [
[
'type' => 'figure',
],
[
'type' => 'blockquote',
'content' => [
[
'type' => 'paragraph',
'content' => [
['type' => 'text', 'text' => 'hello']
]
]
]
],
[
'type' => 'paragraph',
'content' => [
['type' => 'text', 'text' => 'world']
]
]
]
]);

});

0 comments on commit 97af2b6

Please sign in to comment.