From b8bfe5927a23efe0ae11b3e7a57bf387b7cbc274 Mon Sep 17 00:00:00 2001 From: Nadar Date: Tue, 5 Dec 2023 19:10:55 +0000 Subject: [PATCH] added codeblock, hrs and tables --- .php-cs-fixer.dist.php | 1 - src/Parser.php | 10 ++++++ src/Types.php | 5 +++ tests/AdditionalsTest.php | 25 ++++++++++++++ tests/ParserTest.php | 12 ++++--- tests/options.json | 69 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 tests/AdditionalsTest.php create mode 100644 tests/options.json diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index e850cd2..17d118e 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -1,7 +1,6 @@ exclude('tests') ->in(__DIR__) ; diff --git a/src/Parser.php b/src/Parser.php index 997d461..b4bed81 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -45,6 +45,16 @@ public function getDefaultNodeRenderers(): array Types::listItem->name => static fn (Node $node) => $node->renderContent(), + Types::codeBlock->name => static fn (Node $node) => '
' . $node->renderContent() . '
', + + Types::horizontalRule->name => static fn () => '
', + + Types::table->name => static fn (Node $node) => "{$node->renderContent()}
", + + Types::tableRow->name => static fn (Node $node) => "{$node->renderContent()}", + + Types::tableCell->name => static fn (Node $node) => "{$node->renderContent()}", + Types::text->name => static function (Node $node) { $text = $node->getText(); foreach ($node->getMarks() as $mark) { diff --git a/src/Types.php b/src/Types.php index bb9c802..1d33733 100644 --- a/src/Types.php +++ b/src/Types.php @@ -21,4 +21,9 @@ enum Types case orderedList; // Represents an ordered list type. case listItem; // Represents a list item type. case text; // Represents a text type. + case codeBlock; + case horizontalRule; + case tableRow; + case tableCell; + case table; } diff --git a/tests/AdditionalsTest.php b/tests/AdditionalsTest.php new file mode 100644 index 0000000..576593f --- /dev/null +++ b/tests/AdditionalsTest.php @@ -0,0 +1,25 @@ +toHtml($json); + $html = '
function hello() {
+    console.log('Hello, World!');
+}

Cell 1Cell 2
Cell 3Cell 4
'; + $this->assertSame($html, $result); + } +} diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 995612c..e3dfd75 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -1,4 +1,6 @@ -assertSame('

<script>alert('xss');</script>

', $result); // test without xss filter: - + $xss = <<replaceNode(Types::paragraph, function(Node $node) { + $wysiwyg->replaceNode(Types::paragraph, function (Node $node) { return '

Custom Paragraph

'; }); - $wysiwyg->addNode('barfoo', fn(Node $node) => '
BarFoo: '.$node->renderContent().'
'); + $wysiwyg->addNode('barfoo', fn (Node $node) => '
BarFoo: '.$node->renderContent().'
'); $result = $wysiwyg->toHtml(json_decode($json, true)); $this->assertSame('
BarFoo: Hello World
', $result); } -} \ No newline at end of file +} diff --git a/tests/options.json b/tests/options.json new file mode 100644 index 0000000..b761495 --- /dev/null +++ b/tests/options.json @@ -0,0 +1,69 @@ +{ + "type": "doc", + "content": [ + { + "type": "codeBlock", + "content": [ + { + "type": "text", + "text": "function hello() {\n console.log('Hello, World!');\n}" + } + ] + }, + { + "type": "horizontalRule" + }, + { + "type": "table", + "content": [ + { + "type": "tableRow", + "content": [ + { + "type": "tableCell", + "content": [ + { + "type": "text", + "text": "Cell 1" + } + ] + }, + { + "type": "tableCell", + "content": [ + { + "type": "text", + "text": "Cell 2" + } + ] + } + ] + }, + { + "type": "tableRow", + "content": [ + { + "type": "tableCell", + "content": [ + { + "type": "text", + "text": "Cell 3" + } + ] + }, + { + "type": "tableCell", + "content": [ + { + "type": "text", + "text": "Cell 4" + } + ] + } + ] + } + ] + } + ] + } + \ No newline at end of file