nadar / quill-delta-parser

A PHP library to parse and render Quill WYSIWYG Deltas into HTML - Flexibel and extendible for custom elements.
MIT License
122 stars 20 forks source link

<p> without close tag is added when list has bold text and a previous linebreak #87

Closed shuedo closed 7 months ago

shuedo commented 7 months ago

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.

nadar commented 7 months ago

thanks for the report. i will create a Pull Request.

nadar commented 7 months ago

(@shuedo maybe you like to create a pull request, would be welcome.)

shuedo commented 7 months ago

(@shuedo maybe you like to create a pull request, would be welcome.)

Ok thanks! I will create it.