Repository: nadar/quill-delta-parser (php)

Issue:
<p> without close tag is added when list has bold text and a previous linebreak
**Describe the bug**
When a list that contains text with attribute bold (it may happen with other kind of inline attributes) has a previous linebreak, the next paragraph after the list breaks.

**The delta code which generates the Problem**

This is the delta code that fails:
```
[
  {
    "insert": "This is begin text:\n\n"
  },
  {
    "attributes": {
      "bold": true
    },
    "insert": "Bold text"
  },
  {
    "insert": " and regular text"
  },
  {
    "attributes": {
      "list": "bullet"
    },
    "insert": "\n"
  },
  {
    "insert": "Another bullet"
  },
  {
    "attributes": {
      "list": "bullet"
    },
    "insert": "\n"
  },
  {
    "insert": "Another text after list"
  }
]
``` 

**The expected html output the delta should produce**

It should produce this:

```
<p>This is begin text:</p><p><br></p><ul><li><strong>Bold text</strong> and regular text</li><li>Another bullet</li></ul><p>Another text after list</p>
```
But the result is this:

```
<p>This is begin text:</p><p><br></p><p><ul><li><strong>Bold text</strong> and regular text</li><li>Another bullet</li></ul>Another text after list</p>
```
Notice the additional `<p>` tag before the `<ul>`, which breaks the HTML structure.

**Additional context**
Upon investigating the Text.render function, it appears that the issue stems from this conditional statement:
```
} elseif ($pick->line->isEmpty() && $next) {
     $isOpen = $this->output($output, self::CLOSEP.self::OPENP, true);
}
```
In cases where there is a line break, the variable $next contains the line text `<strong>Bold text</strong>`, which is inline but also marked as "Done." Consequently, an opening `<p>` tag is generated due to the fulfillment of the if condition, but it fails to close properly because the line is marked as "Done."

**Proposed solution:**

Updating the condition to the following resolves the issue:

```
elseif ($pick->line->isEmpty() && $next && !$next->isDone()) {
```

This adjustment ensures that the `<p>` tag is only opened when the next line is not marked as "Done," effectively resolving the problem.



Pull request:
Fix issue #87
This pull request addresses the issue described in #87, where a line break preceding a list containing inline attributes results in improper HTML formatting.

this closes #87 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7554a7f..89e85ec 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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.4.2 (5. April 2024)
+
++ [#87](https://github.com/nadar/quill-delta-parser/issues/87) Fixed a bug where a line break preceding a list containing inline attributes results in improper HTML formatting for next paragraphs.
+
 ## 3.4.1 (13. March 2024)
 
 + [#84](https://github.com/nadar/quill-delta-parser/issues/84) Allow align `left` as possible value.
diff --git a/src/listener/Text.php b/src/listener/Text.php
index 5d7e876..e8595bc 100644
--- a/src/listener/Text.php
+++ b/src/listener/Text.php
@@ -86,7 +86,7 @@ class Text extends BlockListener
 
                 // If this element is empty we should maybe directly close and reopen this paragraph as it could be an empty line with
                 // a next elmenet
-                } elseif ($pick->line->isEmpty() && $next) {
+                } elseif ($pick->line->isEmpty() && $next && !$next->isDone()) {
                     $isOpen = $this->output($output, self::CLOSEP.self::OPENP, true);
 
                 // if its open, and it had an end newline, lets close


Test patch:
diff --git a/tests/Issue87Test.php b/tests/Issue87Test.php
new file mode 100644
index 0000000..77a4283
--- /dev/null
+++ b/tests/Issue87Test.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace nadar\quill\tests;
+
+class Issue87Test extends DeltaTestCase
+{
+    public $json = <<<'JSON'
+{
+    "ops": 
+    [
+      {
+        "insert": "This is begin text:\n\n"
+      },
+      {
+        "attributes": {
+          "bold": true
+        },
+        "insert": "Bold text"
+      },
+      {
+        "insert": " and regular text"
+      },
+      {
+        "attributes": {
+          "list": "bullet"
+        },
+        "insert": "\n"
+      },
+      {
+        "insert": "Another bullet"
+      },
+      {
+        "attributes": {
+          "list": "bullet"
+        },
+        "insert": "\n"
+      },
+      {
+        "insert": "Another text after list"
+      }
+    ]
+}
+JSON;
+
+    public $html = <<<'EOT'
+    <p>This is begin text:</p><p><br></p><ul><li><strong>Bold text</strong> and regular text</li><li>Another bullet</li></ul><p>Another text after list</p>
+EOT;
+}
\ No newline at end of file