Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
List bug
Browse files Browse the repository at this point in the history
* Switch to preg split, finding 2 or more new lines, only splitting on
2.
* Hack to fix bug #30
* Moved \n replacement to last step.
  • Loading branch information
deanblackborough committed Sep 11, 2017
1 parent 498c582 commit c16abb1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
42 changes: 36 additions & 6 deletions src/Parser/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,15 @@ private function lastItemClosed()
*/
private function splitDeltas()
{
$deltas = $this->deltas['ops'];
$deltas = $this->deltas;
$this->deltas = array();

foreach ($deltas as $delta) {
foreach ($deltas as $k => $delta) {
if (array_key_exists('insert', $delta) === true &&
//array_key_exists('attributes', $delta) === false && @todo Why did I add this?
is_array($delta['insert']) === false &&
preg_match("/[\n]{2,}/", $delta['insert']) !== 0) {

foreach (explode("\n\n", $delta['insert']) as $k => $match) {
foreach (preg_split("/[\n]{2,}/", $delta['insert']) as $match) {
$new_delta = [
'insert' => str_replace("\n", '', $match),
'break' => true
Expand All @@ -227,9 +226,38 @@ private function splitDeltas()
$this->deltas[] = $new_delta;
}
} else {
if (array_key_exists('insert', $delta) === true) {
$delta['insert'] = str_replace("\n", '', $delta['insert']);
$this->deltas[] = $delta;
}
}
}

/**
* List hack
*
* Looks to see if the next item is the start of a list, if this item contains a new line we need
* to split it.
*/
private function listHack()
{
$deltas = $this->deltas['ops'];
$this->deltas = array();

foreach ($deltas as $k => $delta) {
if (array_key_exists('insert', $delta) === true &&
is_array($delta['insert']) === false &&
preg_match("/[\n]{1}/", $delta['insert']) !== 0 &&
array_key_exists(($k+1), $deltas) === true &&
array_key_exists('attributes', $deltas[($k+1)]) === true &&
array_key_exists('list', $deltas[($k+1)]['attributes']) === true) {

foreach (preg_split("/[\n]{1}/", $delta['insert']) as $match) {
$new_delta = [
'insert' => $match
];

$this->deltas[] = $new_delta;
}
} else {
$this->deltas[] = $delta;
}
}
Expand Down Expand Up @@ -390,6 +418,8 @@ public function parse()
{
if ($this->json_valid === true && array_key_exists('ops', $this->deltas) === true) {

$this->listHack();

$this->splitDeltas();

$this->assignTags();
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ public function render()
}
}

return $this->html;
return str_replace("\n", '', $this->html);
}
}
31 changes: 4 additions & 27 deletions tests/MultipleAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
final class MultipleAttributesTest extends \PHPUnit\Framework\TestCase
{
/*private $deltas_multiple_attributes = '{"ops":[{"insert":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed efficitur nibh tempor augue lobortis, nec eleifend velit venenatis. Nullam fringilla dui eget lectus mattis tincidunt. \nDonec sollicitudin, lacus sed luctus ultricies, "},{"attributes":{"strike":true,"italic":true},"insert":"quam sapien "},{"attributes":{"strike":true},"insert":"sollicitudin"},{"insert":" quam, nec auctor eros felis elementum quam. Fusce vel mollis enim. "},{"attributes":{"bold":true},"insert":"Sed ac augue tincidunt,"},{"insert":" cursus urna a, tempus ipsum. Donec pretium fermentum erat a "},{"attributes":{"underline":true},"insert":"elementum"},{"insert":". In est odio, mattis sed dignissim sed, porta ac nisl. Nunc et tellus imperdiet turpis placerat tristique nec quis justo. Aenean nisi libero, auctor a laoreet sed, fermentum vel massa. Etiam ultricies leo eget purus tempor dapibus. Integer ac sapien eros. Suspendisse convallis ex."}]}';
private $deltas_multiple_attributes = '{"ops":[{"insert":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed efficitur nibh tempor augue lobortis, nec eleifend velit venenatis. Nullam fringilla dui eget lectus mattis tincidunt. \nDonec sollicitudin, lacus sed luctus ultricies, "},{"attributes":{"strike":true,"italic":true},"insert":"quam sapien "},{"attributes":{"strike":true},"insert":"sollicitudin"},{"insert":" quam, nec auctor eros felis elementum quam. Fusce vel mollis enim. "},{"attributes":{"bold":true},"insert":"Sed ac augue tincidunt,"},{"insert":" cursus urna a, tempus ipsum. Donec pretium fermentum erat a "},{"attributes":{"underline":true},"insert":"elementum"},{"insert":". In est odio, mattis sed dignissim sed, porta ac nisl. Nunc et tellus imperdiet turpis placerat tristique nec quis justo. Aenean nisi libero, auctor a laoreet sed, fermentum vel massa. Etiam ultricies leo eget purus tempor dapibus. Integer ac sapien eros. Suspendisse convallis ex."}]}';

public function testValidDeltasMultipleAttributes()
{
Expand All @@ -29,39 +29,16 @@ public function testOutputMultipleAttributes()

$quill = new \DBlackborough\Quill\Render($this->deltas_multiple_attributes);
$this->assertEquals($expected, $quill->render());
}*/
}

/**
* Test for bug report #30
*/
public function testParagraphThenList()
{
$deltas = '{
"ops": [{
"insert": "normal text here\nbullet 1"
}, {
"attributes": {
"list": "bullet"
},
"insert": "\n"
}, {
"insert": "bullet 2"
}, {
"attributes": {
"list": "bullet"
},
"insert": "\n"
}, {
"insert": "bullet3"
}, {
"attributes": {
"list": "bullet"
},
"insert": "\n"
}]
}';
$deltas = '{"ops":[{"insert":"This is a single line of text.\nBullet 1"},{"attributes":{"list":"bullet"},"insert":"\n"},{"insert":"Bullet 2"},{"attributes":{"list":"bullet"},"insert":"\n"},{"insert":"Bullet 3"},{"attributes":{"list":"bullet"},"insert":"\n"}]}';

$expected = '<p>normal text here</p><ul><li>bullet 1</li><li>bullet 2</li><li>bullet3</li></ul>';
$expected = '<p>This is a single line of text.</p><ul><li>Bullet 1</li><li>Bullet 2</li><li>Bullet 3</li></ul>';

$quill = new \DBlackborough\Quill\Render($deltas);
$this->assertEquals($expected, $quill->render());
Expand Down

0 comments on commit c16abb1

Please sign in to comment.