bustle/mobiledoc-kit

Section parser: MarkupSection creating elements inside <li> creates invalid sections

Closed this issue · 1 comments

We've run into this when using mobiledoc-kit to migrate html content to mobiledoc content. This (invalid but surprisingly prevalent) html:

<ul>
    <h3>Testing:</h3>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
</ul>

Results in a sections array such as:

[MarkupSection, ListItem, ListItem, ListItem] 

ListItem should only exist inside of a ListSection so the Post model throws errors:

TypeError: Cannot set property post of #<ListItem> which has only a getter
         at LinkedList.adoptItem [as _adoptItem] (/Users/aileen/code/migrate/node_modules/@tryghost/mobiledoc-kit/dist/commonjs/mobiledoc-kit/models/post.js:41:23)
         at LinkedList.adoptItem (/Users/aileen/code/migrate/node_modules/@tryghost/mobiledoc-kit/dist/commonjs/mobiledoc-kit/utils/linked-list.js:34:14)
         at LinkedList.insertBefore (/Users/aileen/code/migrate/node_modules/@tryghost/mobiledoc-kit/dist/commonjs/mobiledoc-kit/utils/linked-list.js:71:12)
         at LinkedList.append (/Users/aileen/code/migrate/node_modules/@tryghost/mobiledoc-kit/dist/commonjs/mobiledoc-kit/utils/linked-list.js:54:12)
         at DOMParser.appendSection (/Users/aileen/code/migrate/node_modules/@tryghost/mobiledoc-kit/dist/commonjs/mobiledoc-kit/parsers/dom.js:149:23)
         at /Users/aileen/code/migrate/node_modules/@tryghost/mobiledoc-kit/dist/commonjs/mobiledoc-kit/parsers/dom.js:133:23
         at Array.forEach (<anonymous>)
         at forEach (/Users/aileen/code/migrate/node_modules/@tryghost/mobiledoc-kit/dist/commonjs/mobiledoc-kit/utils/array-utils.js:53:16)
         at DOMParser.appendSections (/Users/aileen/code/migrate/node_modules/@tryghost/mobiledoc-kit/dist/commonjs/mobiledoc-kit/parsers/dom.js:132:36)
         at /Users/aileen/code/migrate/node_modules/@tryghost/mobiledoc-kit/dist/commonjs/mobiledoc-kit/parsers/dom.js:116:15 }

I propose that the section parser should create a ListItem for unexpected children of a list element if it contains any text content, otherwise it should be ignored. PR for that incoming.

Looks like there is a deeper issue here, valid html with a MarkupSection creating element inside an <li> will create invalid sections...

<ul>
    <li>One</li>
    <li><h3>Two</h3></li>
    <li>Three</li>
    <li>Four</li>
</ul>

= [ListSection, MarkupSection, ListItem, ListItem]