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

Commit

Permalink
Merge pull request #31 from deanblackborough/v1.01.1
Browse files Browse the repository at this point in the history
V1.01.1
  • Loading branch information
deanblackborough authored Sep 11, 2017
2 parents 2fd6880 + 51e5449 commit dd3fcad
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

Full changelog for PHP Quill Renderer

## 1.01.1 - 2017-09-11

* If a list follows text the generated HTML is invalid. [Bugfix] (Credit: Carlos https://github.com/sald19
for finding bug)
* Switched to preg_split, code was looking for two or more newlines but ever only splitting on two.
* Moved newline replacement to last possible step.

## 1.01.0 - 2017-09-04

* Attribute incorrect for bullet list (Credit: Carlos https://github.com/sald19) [Bugfix]
Expand Down
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);
}
}
13 changes: 13 additions & 0 deletions tests/MultipleAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ 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":"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>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());
}
}

0 comments on commit dd3fcad

Please sign in to comment.