diff --git a/CHANGELOG.md b/CHANGELOG.md index 96e9aa4..3d6f456 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/src/Parser/Html.php b/src/Parser/Html.php index 576476b..6a96fce 100644 --- a/src/Parser/Html.php +++ b/src/Parser/Html.php @@ -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 @@ -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; } } @@ -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(); diff --git a/src/Renderer/Html.php b/src/Renderer/Html.php index abf9d2b..6ee462e 100644 --- a/src/Renderer/Html.php +++ b/src/Renderer/Html.php @@ -67,6 +67,6 @@ public function render() } } - return $this->html; + return str_replace("\n", '', $this->html); } } diff --git a/tests/MultipleAttributesTest.php b/tests/MultipleAttributesTest.php index 5b98b1f..42f1c13 100644 --- a/tests/MultipleAttributesTest.php +++ b/tests/MultipleAttributesTest.php @@ -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 = '
This is a single line of text.