Skip to content
Open
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
16 changes: 14 additions & 2 deletions src/Core/DOMParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private function processChildren($node): array

if ($child->hasChildNodes()) {
$item = array_merge($item, [
'content' => $this->processChildren($child),
'content' => $this->processChildren(isset($item['contentDom']) ? $item['contentDom'] : $child),
]);
}

Expand All @@ -94,6 +94,8 @@ private function processChildren($node): array
]);
}

unset($item['contentDom']);

array_push($nodes, $item);
} elseif ($class = $this->getMarkFor($child)) {
array_push($this->storedMarks, $this->parseAttributes($class, $child));
Expand Down Expand Up @@ -283,7 +285,7 @@ private function checkParseRule($parseRule, $DOMNode): bool
/**
* @return (array|mixed|string)[]|null
*
* @psalm-return array{type: mixed, text?: string, attrs?: array}|null
* @psalm-return array{type: mixed, text?: string, attrs?: array, contentDom?: mixed}|null
*/
private function parseAttributes($class, $DOMNode): ?array
{
Expand Down Expand Up @@ -314,6 +316,16 @@ private function parseAttributes($class, $DOMNode): ?array
continue;
}

if (isset($parseRule['contentElement'])) {
if (is_string($parseRule['contentElement'])) {
throw new \Exception("Tiptap for PHP does not support CSS selector in contentElement yet");
} else if (is_callable($parseRule['contentElement'])) {
$item['contentDom'] = $parseRule['contentElement']($DOMNode);
} else {
$item['contentDom'] = $parseRule['contentElement'];
}
}

$attributes = $parseRule['attrs'] ?? [];
if (count($attributes)) {
if (! isset($item['attrs'])) {
Expand Down
21 changes: 21 additions & 0 deletions tests/DOMParser/Nodes/ContentElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tiptap\Tests\DOMParser\Nodes;

use Tiptap\Core\Node;

class ContentElement extends Node
{
public static $name = 'contentElement';

public function parseHTML()
{
return [
[
'tag' => 'div',
'contentElement' => fn ($domNode) => new \DOMElement('div', $domNode->lastChild->textContent),
'getAttrs' => fn ($domNode) => $domNode->getAttribute('class') === 'contentElement',
],
];
}
}
28 changes: 28 additions & 0 deletions tests/DOMParser/Nodes/ContentElementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Tiptap\Editor;
use Tiptap\Extensions\StarterKit;
use Tiptap\Tests\DOMParser\Nodes\ContentElement;

test('node with contentElement gets rendered correctly', function () {
$html = '<div class="contentElement"><span>no content</span><span>content</span></div>';

$result = (new Editor([
'extensions' => [
new StarterKit,
new ContentElement,
],
]))->setContent($html)->getDocument();

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