tiptap-php icon indicating copy to clipboard operation
tiptap-php copied to clipboard

Importing html with nested lists causes invalid doc structure

Open Coding-Kiwi opened this issue 2 years ago • 5 comments
trafficstars

See my comment https://github.com/ueberdosis/tiptap-php/issues/2#issuecomment-1546706151

If a <li> tag contains a <p> and a sublist <ul>, the ListItem Node wraps the content with an additional <p> tag.

The resulting json doc can be loaded into tiptap but as soon as you edit a list item, an Invalid content for node paragraph is thrown because there is now a <p> tag containing a <p> and <ul> tag which is not valid, because a <p> tag can only contain phrasing content.

Coding-Kiwi avatar May 13 '23 16:05 Coding-Kiwi

I'm seeing this issue as well.

> (new \Tiptap\Editor(['content' => '<p>List:</p><ul><li><p>bullet</p><ol><li><p>one</p></li></ol></li></ul>']))->getJSON()
= "{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"List:"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"paragraph","content":[{"type":"text","text":"bullet"}]},{"type":"orderedList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"one"}]}]}]}]}]}]}]}"

> (new \Tiptap\Editor(['content' => '<p>List:</p><ul><li><p>bullet</p><ol><li><p>one</p></li></ol></li></ul>']))->getHTML()
= "<p>List:</p><ul><li><p><p>bullet</p><ol><li><p>one</p></li></ol></p></li></ul>"

Note how <p>bullet</p> becomes <p><p>bullet</p>.

hivokas avatar Jun 26 '23 11:06 hivokas

Similarly to @Coding-Kiwi, I've also noticed this issue while trying to migrate from HTML to JSON TipTap doc.

hivokas avatar Jun 26 '23 11:06 hivokas

@timoisik if you can take a look at this issue when you have time, it would be great.

hivokas avatar Jun 26 '23 12:06 hivokas

I have the same issue with the sanitize method. Example:

$string = "<p>An example</p><ul><li><p>list item a</p><ul><li><p>list item b</p></li></ul></li></ul>";

$sanitizedHtml = (new Tiptap\Editor)->sanitize($string);

// output: <p>An example</p><ul><li><p><p>list item a</p><ul><li><p>list item b</p></li></ul></p></li></ul>

lukasleitsch avatar Jan 04 '24 07:01 lukasleitsch

The workaround from @Coding-Kiwi in https://github.com/ueberdosis/tiptap-php/issues/2#issuecomment-1546706151 solves the issue for me at this moment:

class TipTapListItem extends \Tiptap\Nodes\ListItem
{
    public static function wrapper($DOMNode)
    {
        return null;
    }
}

$string = "<p>An example</p><ul><li><p>list item a</p><ul><li><p>list item b</p></li></ul></li></ul>";

$sanitizedHtml = (new Tiptap\Editor([
        'extensions' => [
            new Tiptap\Extensions\StarterKit([
                'listItem' => false,
            ]),
            new TipTapListItem(),
        ],
    ]))->sanitize($string)

lukasleitsch avatar Jan 04 '24 08:01 lukasleitsch