Skip to content

Commit

Permalink
prepare test for issue #78 (#79)
Browse files Browse the repository at this point in the history
* prepare test for issue #78

* handle empty line, fix wrong test from issue #53

* changelog
  • Loading branch information
nadar authored Mar 24, 2023
1 parent ea08502 commit 40e21fd
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](http://semver.org/).

## 3.3.1 (24. March 2023)

+ [#78](https://github.com/nadar/quill-delta-parser/issues/78) Fixed a bug where lists with empty contents would break all output.

## 3.3.0 (10. March 2023)

+ [#77](https://github.com/nadar/quill-delta-parser/pull/77) Allow method chaining for `registerListener()` and `overwriteListener()`.
Expand Down
15 changes: 9 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 36 additions & 23 deletions src/listener/Lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,33 @@ public function process(Line $line)
public function render(Lexer $lexer)
{
$isOpen = false;
$isEmpty = false;
$listTag = null;
foreach ($this->picks() as $pick) {
$first = $this->getFirstLine($pick);

// while from first to the pick line and store content in buffer
$buffer = null;
$first->while(static function (&$index, Line $line) use (&$buffer, $pick) {
++$index;
$buffer.= $line->getInput();
$line->setDone();
if ($index == $pick->line->getIndex()) {
return false;
}
});

// defines whether this attribute list element is the last one of a list serie.
$isLast = false;

// if this is an empty line .... the first attribute contains the list information, otherwise
// the first line contains content.
if ($first->getAttribute(self::ATTRIBUTE_LIST)) {
$isEmpty = true;
}

// while from first to the pick line and store content in buffer
$buffer = null;
if (!$isEmpty) {
$first->while(static function (&$index, Line $line) use (&$buffer, $pick) {
++$index;
$buffer.= $line->getInput();
$line->setDone();
if ($index == $pick->line->getIndex()) {
return false;
}
});
}

// go to the next element with endlinew and check if it contains a list type until then
$hasNextInside = false;
$pick->line->whileNext(static function (Line $line) use (&$hasNextInside) {
Expand Down Expand Up @@ -113,19 +122,23 @@ public function render(Lexer $lexer)
}
});

$output .= '<li>';
$output .= $buffer;

if ($nextIndent > $pick->line->getAttribute('indent', 0)) {
$output .= '<'.$this->getListAttribute($pick).'>'.PHP_EOL;
} elseif ($nextIndent < $pick->line->getAttribute('indent', 0)) {
$output .= '</li></'.$this->getListAttribute($pick).'></li>'.PHP_EOL;
$closeGap = $pick->line->getAttribute('indent', 0) - $nextIndent;
if ($closeGap > 1) {
$output .= '</'.$this->getListAttribute($pick).'></li>'.PHP_EOL;
}
if ($isEmpty) {
$output .= '<li></li>';
} else {
$output.= '</li>'.PHP_EOL;
$output .= '<li>';
$output .= $buffer;

if ($nextIndent > $pick->line->getAttribute('indent', 0)) {
$output .= '<'.$this->getListAttribute($pick).'>'.PHP_EOL;
} elseif ($nextIndent < $pick->line->getAttribute('indent', 0)) {
$output .= '</li></'.$this->getListAttribute($pick).'></li>'.PHP_EOL;
$closeGap = $pick->line->getAttribute('indent', 0) - $nextIndent;
if ($closeGap > 1) {
$output .= '</'.$this->getListAttribute($pick).'></li>'.PHP_EOL;
}
} else {
$output.= '</li>'.PHP_EOL;
}
}

// close the opening OL/UL tag if:
Expand Down
2 changes: 1 addition & 1 deletion tests/Issue53Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class Issue53Test extends DeltaTestCase
JSON;

public $html = <<<'EOT'
<ul><li>Bullet point content</li></ul>
<ul><li></li></ul><p>Bullet point content</p>
EOT;
}
26 changes: 26 additions & 0 deletions tests/Issue53TestFixedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace nadar\quill\tests;

class Issue53TestFixedTest extends DeltaTestCase
{
public $json = <<<'JSON'
{"ops":[
{
"insert": "Bullet point content"
},
{
"attributes": {
"list": {
"depth": 0,
"type": "bullet"
}
},
"insert": "\n"
}
]}
JSON;

public $html = <<<'EOT'
<ul><li>Bullet point content</li></ul>
EOT;
}
52 changes: 52 additions & 0 deletions tests/Issue78Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace nadar\quill\tests;

class Issue78Test extends DeltaTestCase
{
public $json = <<<'JSON'
{
"ops":
[
{
"insert": "JUST A LIST",
"attributes": {
"bold": true,
"color": "#000000",
"background": "transparent"
}
},
{
"insert": "\n",
"attributes": {
"align": "center"
}
},
{
"insert": "\n\n",
"attributes": {
"list": "ordered"
}
},
{
"insert": "\n"
},
{
"insert": "New title",
"attributes": {
"bold": true,
"color": "#000000",
"background": "transparent"
}
},
{
"insert": "\n\n"
}
]
}
JSON;

public $html = <<<'EOT'
<p style="text-align: center;"><span style="color:#000000"><strong>JUST A LIST</strong></span></p><ol><li></li><li></li></ol><p><br></p><p><span style="color:#000000"><strong>New title</strong></span></p><p><br></p>
EOT;
}

0 comments on commit 40e21fd

Please sign in to comment.