-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for align attribute. (#31)
* Add support for align attribute. * Update to wrapElement method
- Loading branch information
Showing
6 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace nadar\quill\listener; | ||
|
||
use Exception; | ||
use nadar\quill\Line; | ||
use nadar\quill\BlockListener; | ||
use nadar\quill\Lexer; | ||
|
||
/** | ||
* Convert align attributes into p tags with text-align css applied. | ||
* | ||
* @author Gaëtan Faugère <[email protected]> | ||
* @since 2.4.0 | ||
*/ | ||
class Align extends BlockListener | ||
{ | ||
/** | ||
* @var array Supported alignments. | ||
*/ | ||
public $alignments = ['center', 'right', 'justify']; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function process(Line $line) | ||
{ | ||
$alignment = $line->getAttribute('align'); | ||
if ($alignment) { | ||
$this->pick($line, ['alignment' => $alignment]); | ||
$line->setDone(); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @throws Exception for unknown alignment values | ||
*/ | ||
public function render(Lexer $lexer) | ||
{ | ||
foreach ($this->picks() as $pick) { | ||
if (!in_array($pick->alignment, $this->alignments)) { | ||
// prevent html injection in case the attribute is user input | ||
throw new Exception('An unknown alignment "' . $pick->alignment . '" has been detected.'); | ||
} | ||
|
||
} | ||
|
||
$this->wrapElement('<p style="text-align: {alignment};">{__buffer__}</p>', ['alignment']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
namespace nadar\quill\tests; | ||
|
||
class AlignContentTest extends DeltaTestCase | ||
{ | ||
public $json = <<<'JSON' | ||
{"ops":[ | ||
{"insert":"Lorem "}, | ||
{"attributes":{"bold":true},"insert":"Ipsum"}, | ||
{"insert":" Dolor "}, | ||
{"attributes":{"underline":true,"italic":true},"insert":"Sit"}, | ||
{"insert":" Amet"}, | ||
{"attributes":{"align":"center"},"insert":"\n"}, | ||
{"insert":"This images is right aligned : "}, | ||
{"insert":{"image":"https://example.com/image.jpg"}}, | ||
{"attributes":{"align":"right"},"insert":"\n"}, | ||
{"insert":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer "}, | ||
{"attributes":{"bold":true},"insert":"Lorem"}, | ||
{"insert":" nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla "}, | ||
{"attributes":{"bold":true},"insert":"cursus"}, | ||
{"insert":" quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus "}, | ||
{"attributes":{"bold":true},"insert":"sagittis"}, | ||
{"insert":" sed augue semper porta. Mauris massa. Vestibulum "}, | ||
{"attributes":{"italic":true},"insert":"quis"}, | ||
{"insert":" lacinia arcu "}, | ||
{"attributes":{"italic":true},"insert":"sem"}, | ||
{"insert":" eget "}, | ||
{"attributes":{"bold":true},"insert":"augue"}, | ||
{"insert":" nulla. Class aptent taciti sociosqu "}, | ||
{"attributes":{"bold":true},"insert":"massa."}, | ||
{"insert":" ad litora torquent per conubia nostra, per "}, | ||
{"attributes":{"italic":true},"insert":"porta."}, | ||
{"insert":" inceptos himenaeos. Curabitur "}, | ||
{"attributes":{"bold":true},"insert":"taciti"}, | ||
{"insert":" sodales ligula in "}, | ||
{"attributes":{"bold":true},"insert":"conubia"}, | ||
{"insert":" libero."}, | ||
{"attributes":{"align":"justify"},"insert":"\n"} | ||
]} | ||
JSON; | ||
|
||
public $html = <<<'EOT' | ||
<p style="text-align: center;">Lorem <strong>Ipsum</strong> Dolor <u><em>Sit</em></u> Amet</p><p style="text-align: right;">This images is right aligned : <img src="https://example.com/image.jpg" alt="" class="img-responsive img-fluid" /></p><p style="text-align: justify;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer <strong>Lorem</strong> nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla <strong>cursus</strong> quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus <strong>sagittis</strong> sed augue semper porta. Mauris massa. Vestibulum <em>quis</em> lacinia arcu <em>sem</em> eget <strong>augue</strong> nulla. Class aptent taciti sociosqu <strong>massa.</strong> ad litora torquent per conubia nostra, per <em>porta.</em> inceptos himenaeos. Curabitur <strong>taciti</strong> sodales ligula in <strong>conubia</strong> libero.</p> | ||
EOT; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace nadar\quill\tests; | ||
|
||
use nadar\quill\Lexer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class AlignInjectionTest extends TestCase | ||
{ | ||
public function testUnknownAlignException() | ||
{ | ||
$lexer = new Lexer('[{"insert":"Test"},{"attributes":{"align":"<script>alert(1)</script>"},"insert":"\n"}]'); | ||
|
||
$this->expectException(\Exception::class); | ||
$this->expectExceptionMessage('An unknown alignment "<script>alert(1)</script>" has been detected.'); | ||
$lexer->render(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters