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 #126 from deanblackborough/v3.17.5
Browse files Browse the repository at this point in the history
v3.18.0
  • Loading branch information
deanblackborough authored Nov 24, 2019
2 parents 07a7d0c + 5508b99 commit 1eb2fb5
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

Full changelog for PHP Quill Renderer

## v3.18.0 - 2019-11-24

* Custom attributes values which aren't strings are ignored.
* Custom attributes which the parser should ignore can now be defined.

## v3.17.4 - 2019-05-01

* Allow decoded json to be passed in rather than the library decode it, thank you [Lode Claassen](https://github.com/lode).
Expand Down
61 changes: 61 additions & 0 deletions Tests/Attributes/Html/TypographyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ final class TypographyTest extends \PHPUnit\Framework\TestCase
private $delta_super_script = '{"ops":[{"insert":"Lorem ipsum dolor sit"},{"attributes":{"script":"super"},"insert":"x"},{"insert":" amet, consectetur adipiscing elit. Pellentesque at elit dapibus risus molestie rhoncus dapibus eu nulla. Vestibulum at eros id augue cursus egestas."}]}';
private $delta_underline = '{"ops":[{"insert":"Lorem ipsum dolor sit amet "},{"attributes":{"underline":true},"insert":"sollicitudin"},{"insert":" quam, nec auctor eros felis elementum quam. Fusce vel mollis enim."}]}';
private $delta_single_attribute = '{"ops":[{"insert":"Lorem ipsum dolor sit amet "},{"attributes":{"class":"custom_class"},"insert":"sollicitudin"},{"insert":" quam, nec auctor eros felis elementum quam. Fusce vel mollis enim."}]}';
private $delta_custom_array_attribute = '
{
"ops": [
{
"insert": "world",
"attributes": {
"who": "3",
"bold": true
}
},
{
"insert": "\n",
"attributes": {
"who": "3",
"table-cell": {
"id": "table-id-hxrct",
"rowId": "table-row-efeap",
"cellId": "table-cell-vjhos"
}
}
},
{
"insert": "\n"
}
]
}';

private $expected_bold = '<p>Lorem ipsum dolor sit amet <strong>sollicitudin</strong> quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.</p>';
private $expected_bold_with_attributes = '<p>Lorem ipsum dolor sit amet <strong class="bold_attributes">sollicitudin</strong> quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.</p>';
Expand All @@ -30,6 +56,16 @@ final class TypographyTest extends \PHPUnit\Framework\TestCase
private $expected_super_script = '<p>Lorem ipsum dolor sit<sup>x</sup> amet, consectetur adipiscing elit. Pellentesque at elit dapibus risus molestie rhoncus dapibus eu nulla. Vestibulum at eros id augue cursus egestas.</p>';
private $expected_underline = '<p>Lorem ipsum dolor sit amet <u>sollicitudin</u> quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.</p>';
private $expected_single_attribute = '<p>Lorem ipsum dolor sit amet <span class="custom_class">sollicitudin</span> quam, nec auctor eros felis elementum quam. Fusce vel mollis enim.</p>';
private $expected_custom_array_attribute = '<p><strong who="3">world</strong>
<br />
</p>
';
private $expected_custom_array_attribute_ignore_who = '<p><strong>world</strong>
<br />
</p>
';

/**
* Test bold attribute
Expand Down Expand Up @@ -208,4 +244,29 @@ public function testSingleAttribute()

$this->assertEquals($this->expected_single_attribute, trim($result), __METHOD__ . ' - Single attribute failure');
}

/**
* Test a delta with a custom attribute which is an array, ignore them
*
* @return void
* @throws \Exception
*/
public function testIgnoreCustomAttribute()
{
$result = null;

try {
$quill = new QuillRender($this->delta_custom_array_attribute);
$quill->setIgnoredCustomAttributes(['who']);
$result = $quill->render();
} catch (\Exception $e) {
$this->fail(__METHOD__ . 'failure, ' . $e->getMessage());
}

$this->assertEquals(
$this->expected_custom_array_attribute_ignore_who,
$result,
__METHOD__ . ' - Custom attribute which is an array'
);
}
}
15 changes: 10 additions & 5 deletions src/Delta/Html/Compound.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Options;
use DBlackborough\Quill\Settings;

/**
* Compound HTML delta, collects all the attributes for a compound insert and returns the generated HTML
Expand Down Expand Up @@ -82,7 +83,9 @@ private function tags(): void
break;

default:
$this->element_attributes[$attribute] = $value;
if (in_array($attribute, Settings::ignoredCustomAttributes()) === false) {
$this->element_attributes[$attribute] = $value;
}
break;
}
}
Expand Down Expand Up @@ -123,10 +126,12 @@ public function render(): string

$element_attributes = '';
foreach ($this->element_attributes as $attribute => $value) {
if ($attribute == "color") {
$element_attributes .= "style=\"{$attribute}: $value\"";
} else {
$element_attributes .= "{$attribute}=\"{$value}\" ";
if (is_string($attribute) && is_string($value)) {
if ($attribute == "color") {
$element_attributes .= "style=\"{$attribute}: $value\"";
} else {
$element_attributes .= "{$attribute}=\"{$value}\" ";
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/Delta/Html/CompoundImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Settings;

/**
* Default delta class for compound image inserts, multiple attributes
*
Expand Down Expand Up @@ -47,7 +49,13 @@ public function render(): string
{
$image_attributes = '';
foreach ($this->attributes as $attribute => $value) {
$image_attributes .= "{$attribute}=\"{$value}\" ";
if (
is_string($attribute) &&
is_string($value) &&
in_array($attribute, Settings::ignoredCustomAttributes()) === false
) {
$image_attributes .= "{$attribute}=\"{$value}\" ";
}
}
return "<img src=\"{$this->insert}\" {$image_attributes}/>";
}
Expand Down
10 changes: 9 additions & 1 deletion src/Delta/Html/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Settings;

/**
* Default delta class for plain inserts, no attributes
*
Expand Down Expand Up @@ -50,7 +52,13 @@ public function render(): string
} else {
$html .= '<span';
foreach($this->attributes as $attribute => $value) {
$html .= " {$attribute}=\"{$value}\"";
if (
is_string($attribute) &&
is_string($value) &&
in_array($attribute, Settings::ignoredCustomAttributes()) === false
) {
$html .= " {$attribute}=\"{$value}\"";
}
}
$html .= ">{$this->escape($this->insert)}</span>";
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function parse(): bool
{
if ($this->valid === true) {
/**
* Before processing through the deltas, generate new deltas by splliting
* Before processing through the deltas, generate new deltas by splitting
* on all new lines, will make it much simpler to work out which
* delta belong to headings, lists etc.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public function __construct(string $quill_json, string $format = Options::FORMAT
}
}

/**
* Set the custom attributes which you would like the parser to ignore
*
* @param array $ignored_attributes
*/
public function setIgnoredCustomAttributes(array $ignored_attributes = [])
{
if (count($ignored_attributes) > 0) {
Settings::setIgnoredCustomAttributes($ignored_attributes);
}
}

/**
* Pass the content array to the renderer and return the generated output
*
Expand Down
37 changes: 37 additions & 0 deletions src/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace DBlackborough\Quill;

/**
* Custom settings for the parser
*
* @author Dean Blackborough <[email protected]>
* @copyright Dean Blackborough
* @license https://github.com/deanblackborough/php-quill-renderer/blob/master/LICENSE
*/
class Settings
{
private static $ignored_custom_attributes = [];

/**
* Set any attributes which you would like the parser to ignore, specifically
* the compound delta
*
* @param array $attributes
*/
static public function setIgnoredCustomAttributes(array $attributes)
{
self::$ignored_custom_attributes = $attributes;
}

/**
* Return the ignore attributes
*
* @return array
*/
static public function ignoredCustomAttributes(): array
{
return self::$ignored_custom_attributes;
}
}

0 comments on commit 1eb2fb5

Please sign in to comment.