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
13 changes: 12 additions & 1 deletion src/Core/DOMParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,24 @@ private function setDocument(string $value): DOMParser
*/
$this->DOM->loadHTML(
$this->makeValidXMLDocument(
$this->minify($value)
$this->minify(
$this->wrapPlainText($value)
)
)
);

return $this;
}

private function wrapPlainText(string $value): string
{
if ($value !== '' && strip_tags($value) === $value) {
return '<p>' . $value . '</p>';
}

return $value;
}

private function minify(string $value): string
{
return (new Minify)->process($value);
Expand Down
66 changes: 66 additions & 0 deletions tests/DOMParser/PlainTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use Tiptap\Editor;

test('plain text is wrapped in a paragraph', function () {
$result = (new Editor)
->setContent('Hello world')
->getDocument();

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

test('plain text with special characters is wrapped in a paragraph', function () {
$result = (new Editor)
->setContent('Hello & goodbye < world')
->getDocument();

expect($result)->toEqual([
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [
[
'type' => 'text',
'text' => 'Hello & goodbye < world',
],
],
],
],
]);
});

test('html input with tags is not double-wrapped', function () {
$result = (new Editor)
->setContent('<p>Hello world</p>')
->getDocument();

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