From e9e58b54e263eaebed51aa2c72483cbc05017dc1 Mon Sep 17 00:00:00 2001 From: Nadar Date: Tue, 5 Dec 2023 15:26:54 +0000 Subject: [PATCH] Refactor mark type "strikethrough" to "strike" in Parser class --- src/Mark.php | 5 ---- src/Parser.php | 5 ++-- tests/ParserTest.php | 24 ++++++++++++++++ tests/marks.json | 67 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 tests/marks.json diff --git a/src/Mark.php b/src/Mark.php index eaf88d2..6455d70 100644 --- a/src/Mark.php +++ b/src/Mark.php @@ -9,11 +9,6 @@ public function __construct(protected array $mark) } - public function getText(): string - { - return $this->mark['text'] ?? ''; - } - public function getType(): string { return $this->mark['type']; diff --git a/src/Parser.php b/src/Parser.php index 9196b7c..5d9bf51 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -37,14 +37,15 @@ public function getDefaultNodeRenderers(): array Types::text->name => static function (Node $node) { $text = $node->getText(); foreach ($node->getMarks() as $mark) { + /** @var Mark $mark */ if ($mark->getType() === 'bold') { $text = '' . $text . ''; } elseif ($mark->getType() === "italic") { $text = '' . $text . ''; } elseif ($mark->getType() === "underline") { $text = '' . $text . ''; - } elseif ($mark->getType() === "strikethrough") { - $text = '' . $text . ''; + } elseif ($mark->getType() === "strike") { + $text = '' . $text . ''; } elseif ($mark->getType() === "link") { $text = '' . $text . ''; } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index bab43f2..995612c 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -21,6 +21,18 @@ public function testCompile() $this->assertSame($html, $result); } + public function testMarks() + { + $path = __DIR__ . '/marks.json'; + $buff = file_get_contents($path); + $json = json_decode($buff, true); + + $wysiwyg = new Parser(); + $result = $wysiwyg->toHtml($json); + $html = '

This is a sample text with bold, italic, underline, and strikethrough formatting.

'; + $this->assertSame($html, $result); + } + public function testXss() { $json = <<toHtml(json_decode($json, true)); $this->assertSame('

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

', $result); + + // test without xss filter: + + $xss = <<alert('xss');" + } + EOT; + $node = new Node($wysiwyg, json_decode($xss, true)); + $xssResult = $node->getText(false); + $this->assertSame("", $xssResult); } public function testCustomNodeRenderer() diff --git a/tests/marks.json b/tests/marks.json new file mode 100644 index 0000000..4cd026b --- /dev/null +++ b/tests/marks.json @@ -0,0 +1,67 @@ +{ + "type": "doc", + "content": [ + { + "type": "paragraph", + "content": [ + { + "type": "text", + "text": "This is a sample text with " + }, + { + "type": "text", + "marks": [ + { + "type": "bold" + } + ], + "text": "bold" + }, + { + "type": "text", + "text": ", " + }, + { + "type": "text", + "marks": [ + { + "type": "italic" + } + ], + "text": "italic" + }, + { + "type": "text", + "text": ", " + }, + { + "type": "text", + "marks": [ + { + "type": "underline" + } + ], + "text": "underline" + }, + { + "type": "text", + "text": ", and " + }, + { + "type": "text", + "marks": [ + { + "type": "strike" + } + ], + "text": "strikethrough" + }, + { + "type": "text", + "text": " formatting." + } + ] + } + ] + } + \ No newline at end of file