diff --git a/.travis.yml b/.travis.yml index a6b0b4b..90308d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,7 @@ language: php php: - - '5.6' - '7.0' - '7.1' - - hhvm notifications: email: diff --git a/CHANGELOG.md b/CHANGELOG.md index a4a077e..5fbc8a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,14 @@ Full changelog for PHP Quill Renderer +## v0.80.0 + +* I'm now only testing against PHP 7+. [Tests] +* Added tests for setting attributes. [Tests] +* Switched to PSR4 +* Minor change to API if using Quill (Render) class, after the PSR4 change I didn't like Quill/Quill. +* Basic support for images (outputting the base64 directly via src="") + ## v0.70.0 - 2017-04-19 * Added the ability to set the HTML tag for the following Quill attributes, bold, italic, script, strike and underline. [Feature] diff --git a/README.md b/README.md index b3d713a..93392e9 100644 --- a/README.md +++ b/README.md @@ -18,17 +18,17 @@ Created for use in [Dlayer](https://github.com/Dlayer/dlayer) but works as a sta The easiest way to use the renderer is with composer. ```composer require deanblackborough/php-quill-renderer```, alternatively include the classes in src/ in your library. -## Usage, using Quill API +## Usage ``` try { - $quill = new \DBlackborough\Quill($deltas, 'HTML'); + $quill = new \DBlackborough\Quill\Render($deltas, 'HTML'); echo $quill->render(); } catch (\Exception $e) { echo $e->getMessage(); } ``` -## Usage, direct +## Usage, direct, parse and then render ``` $parser = new \DBlackborough\Quill\Parser\Html(); $parser->load($deltas); @@ -59,13 +59,13 @@ Script:Sub | `` Script:Super | `` Underline | `` Header | `` +Image | `` -## Planned features in possible order +## Planned features +* Markdown support * Lists (Bullets and Ordered) -* Formatting +* Formatting options (justification etc.) * Improved newline and paragraph support -* External images * Remaining toolbar options * Missing tests (options) -* Markdown support diff --git a/composer.json b/composer.json index c3c1ab1..b9c2365 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { "name": "deanblackborough/php-quill-renderer", - "description": "Render quill insert deltas to HTML", + "description": "Render quill insert deltas to HTML and soon markdown", "autoload": { - "psr-0" : { - "DBlackborough\\Quill" : "src" + "psr-4" : { + "DBlackborough\\Quill\\" : "src/DBlackborough/Quill" } }, "homepage": "https://github.com/deanblackborough/php-quill-renderer", diff --git a/example/index.php b/example/index.php index 34bbdbc..7adc6f7 100644 --- a/example/index.php +++ b/example/index.php @@ -1,15 +1,15 @@ render(); } catch (\Exception $e) { echo $e->getMessage(); diff --git a/src/DBlackborough/Quill/Parser/Html.php b/src/Parser/Html.php similarity index 93% rename from src/DBlackborough/Quill/Parser/Html.php rename to src/Parser/Html.php index d712482..eea7333 100644 --- a/src/DBlackborough/Quill/Parser/Html.php +++ b/src/Parser/Html.php @@ -2,8 +2,7 @@ namespace DBlackborough\Quill\Parser; -use DBlackborough\Quill\Parser; -use PHPUnit\Runner\Exception; +use \Exception; /** * Parser for HTML, parses the deltas to generate a content array for deltas into a html redy array @@ -12,7 +11,7 @@ * @copyright Dean Blackborough * @license https://github.com/deanblackborough/php-quill-renderer/blob/master/LICENSE */ -class Html extends Parser +class Html extends Parse { /** * Renderer constructor. @@ -199,7 +198,9 @@ private function splitDeltas() $this->deltas = array(); foreach ($deltas as $delta) { - if (array_key_exists('insert', $delta) === true && array_key_exists('attributes', $delta) === false && + if (array_key_exists('insert', $delta) === true && + array_key_exists('attributes', $delta) === false && + is_array($delta['insert']) === false && preg_match("/[\n]{2}/", $delta['insert']) !== 0) { foreach (explode("\n\n", $delta['insert']) as $match) { @@ -281,8 +282,15 @@ private function assignTags() } } - if (array_key_exists('insert', $insert) === true && strlen(trim($insert['insert'])) > 0) { - $this->content[$i]['content'] = $insert['insert']; + if (array_key_exists('insert', $insert) === true) { + if (is_array($insert['insert']) === false && strlen(trim($insert['insert'])) > 0) { + $this->content[$i]['content'] = $insert['insert']; + } else { + if (is_array($insert['insert']) === true && + array_key_exists('image', $insert['insert']) === true) { + $this->content[$i]['content'] = [ 'image' => $insert['insert']['image'] ]; + } + } } $i++; @@ -357,7 +365,7 @@ private function removeEmptyElements() $existing_content = $this->content; $this->content = array(); foreach ($existing_content as $content) { - if (strlen($content['content']) !== 0) { + if (is_array($content['content']) === true || strlen($content['content']) !== 0) { $this->content[] = $content; } } @@ -461,7 +469,7 @@ private function validateAndSetAttributeOption($option, $value) * @param mixed $value New Attribute option value * * @return boolean - * @throws Exception + * @throws \Exception */ public function setAttributeOption($option, $value) { diff --git a/src/DBlackborough/Quill/Parser.php b/src/Parser/Parse.php similarity index 98% rename from src/DBlackborough/Quill/Parser.php rename to src/Parser/Parse.php index adb275f..5e4dc84 100644 --- a/src/DBlackborough/Quill/Parser.php +++ b/src/Parser/Parse.php @@ -1,6 +1,6 @@ parser, '\DBlackborough\Quill\Parser') === true) { + if (is_a($this->parser, '\DBlackborough\Quill\Parser\Parse') === true) { return $this->parser->setAttributeOption($option, $value); } else { throw new \Exception('Parser not instantiated, can only set options after instantiating object'); diff --git a/src/DBlackborough/Quill/Renderer/Html.php b/src/Renderer/Html.php similarity index 84% rename from src/DBlackborough/Quill/Renderer/Html.php rename to src/Renderer/Html.php index 1215246..f33f93e 100644 --- a/src/DBlackborough/Quill/Renderer/Html.php +++ b/src/Renderer/Html.php @@ -2,8 +2,6 @@ namespace DBlackborough\Quill\Renderer; -use DBlackborough\Quill\Renderer; - /** * Quill renderer, converts quill delta inserts into HTML * @@ -11,7 +9,7 @@ * @copyright Dean Blackborough * @license https://github.com/deanblackborough/php-quill-renderer/blob/master/LICENSE */ -class Html extends Renderer +class Html extends Render { /** * The generated HTML, string generated by the render method from the content array @@ -46,7 +44,11 @@ public function render() } } - $this->html .= $content['content']; + if (is_array($content['content']) === false) { + $this->html .= $content['content']; + } else { + $this->html .= ''; + } foreach (array_reverse($content['tags']) as $tag) { if (array_key_exists('close', $tag) === true && $tag['close'] !== null) { diff --git a/src/DBlackborough/Quill/Renderer.php b/src/Renderer/Render.php similarity index 91% rename from src/DBlackborough/Quill/Renderer.php rename to src/Renderer/Render.php index 81cf3c6..1278bd4 100644 --- a/src/DBlackborough/Quill/Renderer.php +++ b/src/Renderer/Render.php @@ -1,6 +1,6 @@ deltas_simple_string, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_simple_string, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -24,7 +24,7 @@ public function testValidDeltasSimpleString() public function testInvalidDeltasCaught() { try { - $quill = new \DBlackborough\Quill($this->deltas_missing_quote, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_missing_quote, 'HTML'); $this->fail(__METHOD__ . ' failure'); } catch (\Exception $e) { $this->assertTrue(true); @@ -35,7 +35,7 @@ public function testOutputSimpleString() { $expected = '

Lorem ipsum dolor sit amet

'; - $quill = new \DBlackborough\Quill($this->deltas_simple_string); + $quill = new \DBlackborough\Quill\Render($this->deltas_simple_string); $this->assertEquals($expected, $quill->render()); } } diff --git a/tests/HeadingAttributeTest.php b/tests/HeadingAttributeTest.php index f92ac8e..1da4f56 100644 --- a/tests/HeadingAttributeTest.php +++ b/tests/HeadingAttributeTest.php @@ -1,10 +1,10 @@ deltas_h1, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_h1, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -26,7 +26,7 @@ public function testValidDeltasHeading1() public function testValidDeltasHeading2() { try { - $quill = new \DBlackborough\Quill($this->deltas_h2, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_h2, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -36,7 +36,7 @@ public function testValidDeltasHeading2() public function testValidDeltasHeadingThenText() { try { - $quill = new \DBlackborough\Quill($this->deltas_heading_then_text, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_heading_then_text, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -46,7 +46,7 @@ public function testValidDeltasHeadingThenText() public function testValidDeltasHeadingTestThenHeading() { try { - $quill = new \DBlackborough\Quill($this->deltas_heading_text_then_heading, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_heading_text_then_heading, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -57,7 +57,7 @@ public function testOutputHeader1ToH1() { $expected = "

Heading 1

"; - $quill = new \DBlackborough\Quill($this->deltas_h1); + $quill = new \DBlackborough\Quill\Render($this->deltas_h1); $this->assertEquals($expected, $quill->render()); } @@ -65,7 +65,7 @@ public function testOutputHeader2ToH2() { $expected = "

Heading 2

"; - $quill = new \DBlackborough\Quill($this->deltas_h2); + $quill = new \DBlackborough\Quill\Render($this->deltas_h2); $this->assertEquals($expected, $quill->render()); } @@ -73,7 +73,7 @@ public function testOutputHeadingThenText() { $expected = "

This is a heading

Now some normal text.

"; - $quill = new \DBlackborough\Quill($this->deltas_heading_then_text); + $quill = new \DBlackborough\Quill\Render($this->deltas_heading_then_text); $this->assertEquals($expected, $quill->render()); } @@ -81,7 +81,7 @@ public function testOutputHeadingTextThenHeading() { $expected = "

This is a heading

Now some normal text.

Now another heading

"; - $quill = new \DBlackborough\Quill($this->deltas_heading_text_then_heading); + $quill = new \DBlackborough\Quill\Render($this->deltas_heading_text_then_heading); $this->assertEquals($expected, $quill->render()); } } diff --git a/tests/MultipleAttributesTest.php b/tests/MultipleAttributesTest.php index 3a9a164..a7692bd 100644 --- a/tests/MultipleAttributesTest.php +++ b/tests/MultipleAttributesTest.php @@ -1,10 +1,10 @@ deltas_multiple_attributes, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_multiple_attributes, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -27,7 +27,7 @@ public function testOutputMultipleAttributes() { $expected = "

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. Donec sollicitudin, lacus sed luctus ultricies, quam sapien sollicitudin quam, nec auctor eros felis elementum quam. Fusce vel mollis enim. Sed ac augue tincidunt, cursus urna a, tempus ipsum. Donec pretium fermentum erat a elementum. 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.

"; - $quill = new \DBlackborough\Quill($this->deltas_multiple_attributes); + $quill = new \DBlackborough\Quill\Render($this->deltas_multiple_attributes); $this->assertEquals($expected, $quill->render()); } } diff --git a/tests/SetAttributesTest.php b/tests/SetAttributesTest.php new file mode 100644 index 0000000..7c03fd7 --- /dev/null +++ b/tests/SetAttributesTest.php @@ -0,0 +1,86 @@ +deltas_bold, 'HTML'); + $this->assertTrue(true); + } catch (\Exception $e) { + $this->fail(__METHOD__ . ' failure'); + } + } + + public function testOutputBoldToStrong() + { + $expected = '

Lorem ipsum dolor sit amet sollicitudin quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.

'; + + try { + $quill = new \DBlackborough\Quill\Render($this->deltas_bold); + $this->assertEquals($expected, $quill->render()); + } catch (\Exception $e) { + $this->fail(__METHOD__ . ' failure'); + } + } + + public function testOutputBoldToB() + { + $expected = '

Lorem ipsum dolor sit amet sollicitudin quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.

'; + + try { + $quill = new \DBlackborough\Quill\Render($this->deltas_bold); + $quill->setAttributeOption('bold', 'b'); + $this->assertEquals($expected, $quill->render()); + } catch (\Exception $e) { + $this->fail(__METHOD__ . ' failure'); + } + } + + public function testValidDeltasItalic() + { + try { + $quill = new \DBlackborough\Quill\Render($this->deltas_italic, 'HTML'); + $this->assertTrue(true); + } catch (\Exception $e) { + $this->fail(__METHOD__ . ' failure'); + } + } + + public function testOutputItalicToEm() + { + $expected = '

Lorem ipsum dolor sit amet sollicitudin quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.

'; + + try { + $quill = new \DBlackborough\Quill\Render($this->deltas_italic); + $this->assertEquals($expected, $quill->render()); + } catch (\Exception $e) { + $this->fail(__METHOD__ . ' failure'); + } + } + + public function testOutputItalicToI() + { + $expected = '

Lorem ipsum dolor sit amet sollicitudin quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.

'; + + try { + $quill = new \DBlackborough\Quill\Render($this->deltas_italic); + $quill->setAttributeOption('italic', 'i'); + $this->assertEquals($expected, $quill->render()); + } catch (\Exception $e) { + $this->fail(__METHOD__ . ' failure'); + } + } +} diff --git a/tests/SingleAttributesTest.php b/tests/SingleAttributesTest.php index debe258..942e281 100644 --- a/tests/SingleAttributesTest.php +++ b/tests/SingleAttributesTest.php @@ -1,10 +1,10 @@ deltas_bold, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_bold, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -32,7 +32,7 @@ public function testValidDeltasBold() public function testValidDeltastalic() { try { - $quill = new \DBlackborough\Quill($this->deltas_italic, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_italic, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -42,7 +42,7 @@ public function testValidDeltastalic() public function testValidDeltasLink() { try { - $quill = new \DBlackborough\Quill($this->deltas_link, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_link, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -52,7 +52,7 @@ public function testValidDeltasLink() public function testValidDeltasStrike() { try { - $quill = new \DBlackborough\Quill($this->deltas_strike, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_strike, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -62,7 +62,7 @@ public function testValidDeltasStrike() public function testValidDeltasSubscript() { try { - $quill = new \DBlackborough\Quill($this->deltas_subscript, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_subscript, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -72,7 +72,7 @@ public function testValidDeltasSubscript() public function testValidDeltasSuperscript() { try { - $quill = new \DBlackborough\Quill($this->deltas_superscript, 'HTML'); + $quill = new \DBlackborough\Quill\Render($this->deltas_superscript, 'HTML'); $this->assertTrue(true); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -84,7 +84,7 @@ public function testOutputBoldToStrong() $expected = '

Lorem ipsum dolor sit amet sollicitudin quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.

'; try { - $quill = new \DBlackborough\Quill($this->deltas_bold); + $quill = new \DBlackborough\Quill\Render($this->deltas_bold); $this->assertEquals($expected, $quill->render()); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -96,7 +96,7 @@ public function testOutputItalicToEm() $expected = '

Lorem ipsum dolor sit amet sollicitudin quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.

'; try { - $quill = new \DBlackborough\Quill($this->deltas_italic); + $quill = new \DBlackborough\Quill\Render($this->deltas_italic); $this->assertEquals($expected, $quill->render()); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -108,10 +108,10 @@ public function testOutputUnderlineToU() $expected = '

Lorem ipsum dolor sit amet sollicitudin quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.

'; try { - $quill = new \DBlackborough\Quill($this->deltas_underline); + $quill = new \DBlackborough\Quill\Render($this->deltas_underline); $this->assertEquals($expected, $quill->render()); } catch (\Exception $e) { - $this->fail(__METHOD__, ' failure'); + $this->fail(__METHOD__. ' failure'); } } @@ -120,7 +120,7 @@ public function testOutputStrikeToS() $expected = '

Lorem ipsum dolor sit amet sollicitudin quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.

'; try { - $quill = new \DBlackborough\Quill($this->deltas_strike); + $quill = new \DBlackborough\Quill\Render($this->deltas_strike); $this->assertEquals($expected, $quill->render()); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -132,10 +132,10 @@ public function testOutputLinkToHref() $expected = '

Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sed efficitur enim. Suspendisse mattis purus id odio varius suscipit. Nunc posuere fermentum blandit. In vitae eros nec mauris dignissim porttitor. Morbi a tempus tellus. Mauris quis velit sapien. Etiam sit amet enim venenatis, eleifend lectus ac, ultricies orci. Sed tristique laoreet mi nec imperdiet. Vivamus non dui diam. Aliquam erat eros, dignissim in quam id.

'; try { - $quill = new \DBlackborough\Quill($this->deltas_link); + $quill = new \DBlackborough\Quill\Render($this->deltas_link); $this->assertEquals($expected, $quill->render()); } catch (\Exception $e) { - $this->fail(__METHOD__, ' failure'); + $this->fail(__METHOD__. ' failure'); } } @@ -144,7 +144,7 @@ public function testOutputScriptSubToSub() $expected = '

Lorem ipsum dolor sitx amet, consectetur adipiscing elit. Pellentesque at elit dapibus risus molestie rhoncus dapibus eu nulla. Vestibulum at eros id augue cursus egestas.

'; try { - $quill = new \DBlackborough\Quill($this->deltas_subscript); + $quill = new \DBlackborough\Quill\Render($this->deltas_subscript); $this->assertEquals($expected, $quill->render()); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure'); @@ -156,7 +156,7 @@ public function testOutputScriptSuperToSup() $expected = '

Lorem ipsum dolor sitx amet, consectetur adipiscing elit. Pellentesque at elit dapibus risus molestie rhoncus dapibus eu nulla. Vestibulum at eros id augue cursus egestas.

'; try { - $quill = new \DBlackborough\Quill($this->deltas_superscript); + $quill = new \DBlackborough\Quill\Render($this->deltas_superscript); $this->assertEquals($expected, $quill->render()); } catch (\Exception $e) { $this->fail(__METHOD__ . ' failure');