Skip to content

Commit

Permalink
smiles node
Browse files Browse the repository at this point in the history
  • Loading branch information
litlife committed Oct 4, 2021
1 parent bc263c1 commit 33ad41c
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "litlife/json-to-bbcode",
"description": "Json-to-bbcode is a library written in PHP for converting ProseMirror json to BBCode",
"version": "1.1.0",
"version": "1.2.0",
"license": "MIT",
"authors": [
{
Expand Down
19 changes: 19 additions & 0 deletions src/Nodes/Smile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Litlife\JsonToBBCode\Nodes;

class Smile extends Node
{
public function matching(): bool
{
return $this->proseMirrorJson['type'] == 'smile';
}

public function toBBCode($innerBBCode = null): string
{
if (!empty($this->proseMirrorJson['attrs']['simple_form']))
return $this->proseMirrorJson['attrs']['simple_form'];
else
return '';
}
}
9 changes: 6 additions & 3 deletions tests/Nodes/HardBreakTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ public function test()

$jsonArray = json_decode($jsonString, true);

$this->assertEquals($bb, (new Renderer())
$actual = (new Renderer())
->clearNodes()
->clearMarks()
->addNode(Blockquote::class)
->addNode(Paragraph::class)
->addNode(HardBreak::class)
->render($jsonArray)
);
->render($jsonArray);

$actual = str_replace( "\r\n", "\n", $actual);

$this->assertEquals($bb, $actual);
}
}
105 changes: 105 additions & 0 deletions tests/Nodes/SmileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

use Litlife\JsonToBBCode\Nodes\Smile;
use Litlife\JsonToBBCode\Renderer;
use PHPUnit\Framework\TestCase;

class SmileTest extends TestCase
{
public function testAddSmile()
{
$bb = 'Тест :died: :hoo:';

$jsonString = <<<EOF
{
"type": "doc",
"content": [
{
"type": "paragraph",
"attrs": {
"textAlign": "left"
},
"content": [
{
"type": "text",
"text": "Тест "
},
{
"type": "hardBreak"
},
{
"type": "smile",
"attrs": {
"src": "http://example.com/storage/smiles/80.gif",
"width": 43,
"height": 34,
"simple_form": ":died:"
}
},
{
"type": "text",
"text": " "
},
{
"type": "smile",
"attrs": {
"src": "http://example.com/storage/smiles/72.gif",
"width": 44,
"height": 46,
"simple_form": ":hoo:"
}
}
]
}
]
}
EOF;

$jsonArray = json_decode($jsonString, true);

$this->assertEquals($bb, (new Renderer())
->clearNodes()
->addNode(Smile::class)
->render($jsonArray));
}

public function testAddSmileWithoutSimpleForm()
{
$bb = 'Тест ';

$jsonString = <<<EOF
{
"type": "doc",
"content": [
{
"type": "paragraph",
"attrs": {
"textAlign": "left"
},
"content": [
{
"type": "text",
"text": "Тест "
},
{
"type": "smile",
"attrs": {
"src": "http://example.com/storage/smiles/72.gif",
"width": 44,
"height": 46
}
}
]
}
]
}
EOF;

$jsonArray = json_decode($jsonString, true);

$this->assertEquals($bb, (new Renderer())
->clearNodes()
->addNode(Smile::class)
->render($jsonArray));
}
}

0 comments on commit 33ad41c

Please sign in to comment.