Skip to content

Commit 1b1bfbf

Browse files
committed
Add contentElement support to Node
1 parent b81ca0d commit 1b1bfbf

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

src/Core/DOMParser.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private function processChildren($node): array
8484

8585
if ($child->hasChildNodes()) {
8686
$item = array_merge($item, [
87-
'content' => $this->processChildren($child),
87+
'content' => $this->processChildren(isset($item['contentDom']) ? $item['contentDom'] : $child),
8888
]);
8989
}
9090

@@ -94,6 +94,8 @@ private function processChildren($node): array
9494
]);
9595
}
9696

97+
unset($item['contentDom']);
98+
9799
array_push($nodes, $item);
98100
} elseif ($class = $this->getMarkFor($child)) {
99101
array_push($this->storedMarks, $this->parseAttributes($class, $child));
@@ -283,7 +285,7 @@ private function checkParseRule($parseRule, $DOMNode): bool
283285
/**
284286
* @return (array|mixed|string)[]|null
285287
*
286-
* @psalm-return array{type: mixed, text?: string, attrs?: array}|null
288+
* @psalm-return array{type: mixed, text?: string, attrs?: array, contentDom?: \DOMNode}|null
287289
*/
288290
private function parseAttributes($class, $DOMNode): ?array
289291
{
@@ -314,6 +316,16 @@ private function parseAttributes($class, $DOMNode): ?array
314316
continue;
315317
}
316318

319+
if (isset($parseRule['contentElement'])) {
320+
if (is_string($parseRule['contentElement'])) {
321+
throw new \Exception("Tiptap for PHP does not support CSS selector in contentElement yet");
322+
} else if (is_callable($parseRule['contentElement'])) {
323+
$item['contentDom'] = $parseRule['contentElement']($DOMNode);
324+
} else {
325+
$item['contentDom'] = $parseRule['contentElement'];
326+
}
327+
}
328+
317329
$attributes = $parseRule['attrs'] ?? [];
318330
if (count($attributes)) {
319331
if (! isset($item['attrs'])) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tiptap\Tests\DOMParser\Nodes;
4+
5+
use Tiptap\Core\Node;
6+
7+
class ContentElement extends Node
8+
{
9+
public static $name = 'contentElement';
10+
11+
public function parseHTML()
12+
{
13+
return [
14+
[
15+
'tag' => 'div',
16+
'contentElement' => fn ($domNode) => new \DOMElement('div', $domNode->lastChild->textContent),
17+
'getAttrs' => fn ($domNode) => $domNode->getAttribute('class') === 'contentElement',
18+
],
19+
];
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Tiptap\Editor;
4+
use Tiptap\Extensions\StarterKit;
5+
use Tiptap\Tests\DOMParser\Nodes\ContentElement;
6+
7+
test('node with contentElement gets rendered correctly', function () {
8+
$html = '<div class="contentElement"><span>no content</span><span>content</span></div>';
9+
10+
$result = (new Editor([
11+
'extensions' => [
12+
new StarterKit,
13+
new ContentElement,
14+
],
15+
]))->setContent($html)->getDocument();
16+
17+
expect($result)->toEqual([
18+
'type' => 'doc',
19+
'content' => [
20+
[
21+
'type' => 'contentElement',
22+
'content' => [
23+
['type' => 'text', 'text' => 'content'],
24+
],
25+
],
26+
],
27+
]);
28+
});

0 commit comments

Comments
 (0)