Skip to content

Commit fa4c605

Browse files
committed
Add noContent property to Node
1 parent b81ca0d commit fa4c605

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/Core/DOMParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private function processChildren($node): array
8282
continue;
8383
}
8484

85-
if ($child->hasChildNodes()) {
85+
if (!$class::$noContent && $child->hasChildNodes()) {
8686
$item = array_merge($item, [
8787
'content' => $this->processChildren($child),
8888
]);

src/Core/Node.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Node extends Extension
88

99
public static $topNode = false;
1010

11+
public static $noContent = false;
12+
1113
public static $marks = '_';
1214

1315
public function addAttributes()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Tiptap\Tests\DOMParser\Nodes;
4+
5+
use Tiptap\Core\Node;
6+
7+
class BlockquoteCite extends Node
8+
{
9+
public static $name = 'blockquoteCite';
10+
11+
public static $noContent = true;
12+
13+
public function addAttributes(): array
14+
{
15+
return [
16+
'quote' => [
17+
'parseHTML' => fn ($domNode) => $domNode->childNodes[0]->childNodes[0]->textContent ?? '',
18+
],
19+
'author' => [
20+
'parseHTML' => fn ($domNode) => $domNode->childNodes[1]->textContent ?? '',
21+
],
22+
];
23+
}
24+
25+
public function parseHTML()
26+
{
27+
return [
28+
[
29+
'tag' => 'blockquote',
30+
'getAttrs' => fn ($domNode) => isset($domNode->childNodes[1]) && $domNode->childNodes[1]->tagName === 'cite',
31+
],
32+
];
33+
}
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Tiptap\Editor;
4+
use Tiptap\Extensions\StarterKit;
5+
use Tiptap\Tests\DOMParser\Nodes\BlockquoteCite;
6+
7+
test('no content node gets rendered correctly', function () {
8+
$html = '<blockquote><p>Quote</p><cite>Author</cite></blockquote>';
9+
10+
$result = (new Editor([
11+
'extensions' => [
12+
new StarterKit,
13+
new BlockquoteCite,
14+
],
15+
]))->setContent($html)->getDocument();
16+
17+
expect($result)->toEqual([
18+
'type' => 'doc',
19+
'content' => [
20+
[
21+
'type' => 'blockquoteCite',
22+
'attrs' => [
23+
'quote' => 'Quote',
24+
'author' => 'Author',
25+
],
26+
],
27+
],
28+
]);
29+
});

0 commit comments

Comments
 (0)