Skip to content

Commit

Permalink
Merge pull request #39 from godismyjudge95/patch-1
Browse files Browse the repository at this point in the history
Fix multidimensional array check
  • Loading branch information
nperez0111 committed Aug 12, 2024
2 parents e4e2cfe + 441711c commit d617b6c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Core/DOMParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ private function processChildren($node): array
return $this->mergeSimilarNodes($nodes);
}

private function isMultidimensionalArray($array)
{
foreach ($array as $value) {
if (is_array($value)) {
return true;
}
}

return false;
}

private function mergeSimilarNodes($nodes)
{
$result = [];
Expand All @@ -131,10 +142,7 @@ private function mergeSimilarNodes($nodes)
*/
array_reduce($nodes, function ($carry, $node) use (&$result) {
// Ignore multidimensional arrays
if (
count($node) !== count($node, COUNT_RECURSIVE)
|| count($carry) !== count($carry, COUNT_RECURSIVE)
) {
if ($this->isMultidimensionalArray($node) || $this->isMultidimensionalArray($carry)) {
$result[] = $node;

return $node;
Expand Down
25 changes: 25 additions & 0 deletions tests/DOMParser/EmptyNodesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Tiptap\Editor;

test('parsing must not fail on empty nodes', function () {
$html = '<p><img /></p><p><img /></p>';

$result = (new Editor)
->setContent($html)
->getDocument();

expect($result)->toEqual([
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [],
],
[
'type' => 'paragraph',
'content' => [],
],
],
]);
});

0 comments on commit d617b6c

Please sign in to comment.