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 f4e987b..42f1c13 100644 --- a/tests/MultipleAttributesTest.php +++ b/tests/MultipleAttributesTest.php @@ -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() { @@ -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 = '
normal text here
This is a single line of text.