diff --git a/Tests/Attributes/Html/ImageTest.php b/Tests/Attributes/Html/ImageTest.php index 83bd0bb..9cb2b1e 100644 --- a/Tests/Attributes/Html/ImageTest.php +++ b/Tests/Attributes/Html/ImageTest.php @@ -12,11 +12,15 @@ final class ImageTest extends \PHPUnit\Framework\TestCase { private $delta_image = '{"ops":[{"insert": {"image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAgCAYAAACcuBHKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAhdEVYdENyZWF0aW9uIFRpbWUAMjAxODowNTowNiAwMDowMToyNa9H3HIAAARpSURBVFhHtZa/b1NXFMePHceOE2iXAgIJFNpOrSNn6l4kYETZWColf0CnqnsyFqnK0KFjonQoYs0EQ2mGqmsiQWBCEZZAIIoSipTEdvx6P8fv+3JsOY1U0a90dH7ec773vvueXZqYmMgsYWxszHq9nkupVHJdLpdJOYhlWeYakMMHR0dHha8a1QHFVRN7ISle9iSNSAASkFKhFisHqIdobAqwVUMeKBZrVeMbIKBdRyEu3e12fYF8NVUjIJ8axdHqH31qFMMuzltBtI5X4FRoQI4mUQTZiqtfRKwRUVBWsYYoqYHy4yJsJBKNiH3QbIJakQAD/biYasgJCMS0SDaPJd4doCHVatV9cghxrUPH3v4IwjzfiorQ8jVIuXa7bY1Gw1ZWVmx3d9dz6OXlZY8fHBwMnJ7WE0M0GMTeHuMk6vV6IfhpZ1mlUnGdirLp6elsdXU1rT1Gv9cxyFOXGhfrEfVHS2q12oAuUQAj2ILUzw4PD21xcdFmZ2ctNbZms+m5iLjbiK2tLdvZ2bHNzU3vMTk56XUIa4Bm6WRKU1NTmR4FSTRHO2pAxEkkIqhJmyz6otPpDPh+afUNIAhOa/xfQG+EgQwGzOEk8Msa7k4Kyv+QYKCE/iIA0MV3Qvg/SNBTQ5mHj4YMV6HMEREUww8NDdNmddqI4G+Hjkcs9/f38/TpqDbOW/3Gp1a5dDaPmHVf/G37D55Z+9FrSxffP3IaPup7VDwOHCVZkN57j50k4Ic/1uzsfHOAAMAnPnnz84IAiLYIgRTrM9QpILxGkPg3cAJ3/vwl90ajfv2qVb74xG2Ia5N6S3QV/KccUUL2xsaGy0ngEZyE21/esMa5z9yuXrtSEJD4ZUyb12w/GwI6CTQyPj5u8/Pztre3582GMfwIBAj8dPM7a5zvkyhfPON9GQakgRNIs4uToNAD+ePhNFqtlhMZxvN3r3LLfMe/f/OzfVw7UxD49v6Pdvfxg7zi+FFIS5hVvKIqJBBJ8fO8vr5uCwsLXiNc+ehCbpm1ckIPE5FRBADD6InmcuqxK+ZfTLEiCPBVyCVdW1uzubm5gUfDawj2Dt/brXvf27ukRxHovXzvfeIc3T98UHwngHQkJHQ6HZuZmfG3hl/V2swFfw1PQ/vXp3b05C8fGi+/Ngr8TogREAHFWYjNo9ne3vaf96WlJf8QfdW+nK8ajc5vz627/WZgQ3Ees7BL/LFwI4mKScJSmlzcCafC/cGvNy/a+NeX/S0QeASdhy3rPX07QEA71zzgpPhno4RYRrYRsSEEQWysvMgL5OK3AR/RxvqdEgjGIrSaylfN8BAgnzps1SrGBScGiCtPbdJ9QxKhRWqKYIvQsI41w1AN8/QpUJ1fTBLuJI3E5rLVXLbysoGaKoeoN3YENTwKX59uPf+o81Q/WTAM5IhpqJ4vwB+GCJCLRLQWrZzb+rdNQJCvIsUATcBwHGDH0wLaEIhrNA+7+O2IQ6VjMc3iLgA7JK+digAx1QPVxbxiWZbZP/xo6NrxoHtXAAAAAElFTkSuQmCC"}}]}'; + private $delta_image_attribute = '{"ops":[{"insert": {"image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAgCAYAAACcuBHKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAhdEVYdENyZWF0aW9uIFRpbWUAMjAxODowNTowNiAwMDowMToyNa9H3HIAAARpSURBVFhHtZa/b1NXFMePHceOE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feIc3T98UHwngHQkJHQ6HZuZmfG3hl/V2swFfw1PQ/vXp3b05C8fGi+/Ngr8TogREAHFWYjNo9ne3vaf96WlJf8QfdW+nK8ajc5vz627/WZgQ3Ees7BL/LFwI4mKScJSmlzcCafC/cGvNy/a+NeX/S0QeASdhy3rPX07QEA71zzgpPhno4RYRrYRsSEEQWysvMgL5OK3AR/RxvqdEgjGIrSaylfN8BAgnzps1SrGBScGiCtPbdJ9QxKhRWqKYIvQsI41w1AN8/QpUJ1fTBLuJI3E5rLVXLbysoGaKoeoN3YENTwKX59uPf+o81Q/WTAM5IhpqJ4vwB+GCJCLRLQWrZzb+rdNQJCvIsUATcBwHGDH0wLaEIhrNA+7+O2IQ6VjMc3iLgA7JK+digAx1QPVxbxiWZbZP/xo6NrxoHtXAAAAAElFTkSuQmCC"},"attributes":{"width":"100"}}]}'; + private $delta_image_attributes = '{"ops":[{"insert": {"image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAgCAYAAACcuBHKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAhdEVYdENyZWF0aW9uIFRpbWUAMjAxODowNTowNiAwMDowMToyNa9H3HIAAARpSURBVFhHtZa/b1NXFMePHceOE2iXAgIJFNpOrSNn6l4kYETZWColf0CnqnsyFqnK0KFjonQoYs0EQ2mGqmsiQWBCEZZAIIoSipTEdvx6P8fv+3JsOY1U0a90dH7ec773vvueXZqYmMgsYWxszHq9nkupVHJdLpdJOYhlWeYakMMHR0dHha8a1QHFVRN7ISle9iSNSAASkFKhFisHqIdobAqwVUMeKBZrVeMbIKBdRyEu3e12fYF8NVUjIJ8axdHqH31qFMMuzltBtI5X4FRoQI4mUQTZiqtfRKwRUVBWsYYoqYHy4yJsJBKNiH3QbIJakQAD/biYasgJCMS0SDaPJd4doCHVatV9cghxrUPH3v4IwjzfiorQ8jVIuXa7bY1Gw1ZWVmx3d9dz6OXlZY8fHBwM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O2IQ6VjMc3iLgA7JK+digAx1QPVxbxiWZbZP/xo6NrxoHtXAAAAAElFTkSuQmCC"},"attributes":{"width":"100","height":"200"}}]}'; private $expected_image = '
'; + private $expected_image_attribute = ''; + private $expected_image_attributes = ''; /** - * Link + * Image * * @return void * @throws \Exception @@ -34,4 +38,42 @@ public function testImage() $this->assertEquals($this->expected_image, trim($result), __METHOD__ . ' Image failure'); } + /** + * Image with single attribute + * + * @return void + * @throws \Exception + */ + public function testImageWithAttribute() + { + $result = null; + + try { + $quill = new QuillRender($this->delta_image_attribute); + $result = $quill->render(); + } catch (\Exception $e) { + $this->fail(__METHOD__ . 'failure, ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString()); + } + + $this->assertEquals($this->expected_image_attribute, trim($result), __METHOD__ . ' Image failure'); + } + /** + * Image with multiple attributes + * + * @return void + * @throws \Exception + */ + public function testImageWithAttributes() + { + $result = null; + + try { + $quill = new QuillRender($this->delta_image_attributes); + $result = $quill->render(); + } catch (\Exception $e) { + $this->fail(__METHOD__ . 'failure, ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString()); + } + + $this->assertEquals($this->expected_image_attributes, trim($result), __METHOD__ . ' Image failure'); + } } diff --git a/Tests/Attributes/Html/VideoTest.php b/Tests/Attributes/Html/VideoTest.php index cb62f62..859168c 100644 --- a/Tests/Attributes/Html/VideoTest.php +++ b/Tests/Attributes/Html/VideoTest.php @@ -12,8 +12,12 @@ final class VideoTest extends \PHPUnit\Framework\TestCase { private $delta_video = '{"ops":[{"insert":{"video":"https://video.url"}}]}'; + private $delta_video_attribute = '{"ops":[{"insert":{"video":"https://video.url"},"attributes":{"width":"560"}}]}'; + private $delta_video_attributes = '{"ops":[{"insert":{"video":"https://video.url"},"attributes":{"width":"560","height":"315"}}]}'; private $expected_video = ''; + private $expected_video_attribute = ''; + private $expected_video_attributes = ''; /** * Video @@ -34,4 +38,44 @@ public function testVideo() $this->assertEquals($this->expected_video, trim($result), __METHOD__ . ' Video failure'); } + + /** + * Video with a single attribute + * + * @return void + * @throws \Exception + */ + public function testVideoWithAttribute() + { + $result = null; + + try { + $quill = new QuillRender($this->delta_video_attribute); + $result = $quill->render(); + } catch (\Exception $e) { + $this->fail(__METHOD__ . 'failure, ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString()); + } + + $this->assertEquals($this->expected_video_attribute, trim($result), __METHOD__ . ' Video failure'); + } + + /** + * Video with multiple attributes + * + * @return void + * @throws \Exception + */ + public function testVideoWithAttributes() + { + $result = null; + + try { + $quill = new QuillRender($this->delta_video_attributes); + $result = $quill->render(); + } catch (\Exception $e) { + $this->fail(__METHOD__ . 'failure, ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString()); + } + + $this->assertEquals($this->expected_video_attributes, trim($result), __METHOD__ . ' Video failure'); + } } diff --git a/src/Delta/Html/CompoundVideo.php b/src/Delta/Html/CompoundVideo.php new file mode 100644 index 0000000..93db383 --- /dev/null +++ b/src/Delta/Html/CompoundVideo.php @@ -0,0 +1,62 @@ + + * @copyright Dean Blackborough + * @license https://github.com/deanblackborough/php-quill-renderer/blob/master/LICENSE + */ +class CompoundVideo extends Delta +{ + /** + * Set the initial properties for the delta + * + * @param string $insert + */ + public function __construct(string $insert) + { + $this->insert = $insert; + } + + /** + * Pass in an attribute value for conversion + * + * @param string $attribute Attribute name + * @param string $value Attribute value to assign + * + * @return CompoundVideo + */ + public function setAttribute($attribute, $value): CompoundVideo + { + $this->attributes[$attribute] = $value; + + return $this; + } + + /** + * Render the HTML for the specific Delta type + * + * @return string + */ + public function render(): string + { + $video_attributes = ''; + foreach ($this->attributes as $attribute => $value) { + if ( + is_string($attribute) && + is_string($value) && + in_array($attribute, Settings::ignoredCustomAttributes()) === false + ) { + $video_attributes .= "{$attribute}=\"{$value}\" "; + } + } + return ''; + } +} diff --git a/src/Parser/Html.php b/src/Parser/Html.php index 0b8c27a..9080f27 100644 --- a/src/Parser/Html.php +++ b/src/Parser/Html.php @@ -7,6 +7,7 @@ use DBlackborough\Quill\Delta\Html\Color; use DBlackborough\Quill\Delta\Html\Compound; use DBlackborough\Quill\Delta\Html\CompoundImage; +use DBlackborough\Quill\Delta\Html\CompoundVideo; use DBlackborough\Quill\Delta\Html\Delta; use DBlackborough\Quill\Delta\Html\Header; use DBlackborough\Quill\Delta\Html\Image; @@ -287,9 +288,12 @@ public function compoundInsert(array $quill) if (count($quill['attributes']) > 0) { if (is_array($quill['insert']) === false) { $delta = new Compound($quill['insert']); - } else { + } else if(array_key_exists('image', $quill['insert']) === true) { $delta = new CompoundImage($quill['insert']['image']); + } else if(array_key_exists('video', $quill['insert']) === true) { + $delta = new CompoundVideo($quill['insert']['video']); } + foreach ($quill['attributes'] as $attribute => $value) { $delta->setAttribute($attribute, $value); } diff --git a/src/Parser/Parse.php b/src/Parser/Parse.php index bc565e7..fd0780a 100644 --- a/src/Parser/Parse.php +++ b/src/Parser/Parse.php @@ -292,7 +292,12 @@ public function parse(): bool break; default: - $this->insert($quill); + if (is_array($quill['insert'])) { + $this->compoundInsert($quill); + } else { + $this->insert($quill); + } + break; } }