Skip to content

Commit

Permalink
Refactor mark type "strikethrough" to "strike" in
Browse files Browse the repository at this point in the history
Parser class
  • Loading branch information
nadar committed Dec 5, 2023
1 parent 278b96c commit e9e58b5
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 7 deletions.
5 changes: 0 additions & 5 deletions src/Mark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
5 changes: 3 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<strong>' . $text . '</strong>';
} elseif ($mark->getType() === "italic") {
$text = '<em>' . $text . '</em>';
} elseif ($mark->getType() === "underline") {
$text = '<u>' . $text . '</u>';
} elseif ($mark->getType() === "strikethrough") {
$text = '<del>' . $text . '</del>';
} elseif ($mark->getType() === "strike") {
$text = '<s>' . $text . '</s>';
} elseif ($mark->getType() === "link") {
$text = '<a href="' . $mark->getAttr('href') . '" target="' . $mark->getAttr('target') . '">' . $text . '</a>';
}
Expand Down
24 changes: 24 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<p>This is a sample text with <strong>bold</strong>, <em>italic</em>, <u>underline</u>, and <s>strikethrough</s> formatting.</p>';
$this->assertSame($html, $result);
}

public function testXss()
{
$json = <<<EOT
Expand All @@ -44,6 +56,18 @@ public function testXss()
$result = $wysiwyg->toHtml(json_decode($json, true));

$this->assertSame('<p>&lt;script&gt;alert(&apos;xss&apos;);&lt;/script&gt;</p>', $result);

// test without xss filter:

$xss = <<<EOT
{
"type": "text",
"text": "<script>alert('xss');</script>"
}
EOT;
$node = new Node($wysiwyg, json_decode($xss, true));
$xssResult = $node->getText(false);
$this->assertSame("<script>alert('xss');</script>", $xssResult);
}

public function testCustomNodeRenderer()
Expand Down
67 changes: 67 additions & 0 deletions tests/marks.json
Original file line number Diff line number Diff line change
@@ -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."
}
]
}
]
}

0 comments on commit e9e58b5

Please sign in to comment.