diff --git a/packages/core/src/api/parsers/html/util/__snapshots__/nestedLists.test.ts.snap b/packages/core/src/api/parsers/html/util/__snapshots__/nestedLists.test.ts.snap index 1db488255b..d9e81112b2 100644 --- a/packages/core/src/api/parsers/html/util/__snapshots__/nestedLists.test.ts.snap +++ b/packages/core/src/api/parsers/html/util/__snapshots__/nestedLists.test.ts.snap @@ -11,14 +11,16 @@ exports[`Lift nested lists > Lifts multiple bullet lists 1`] = `
  • Nested Bullet List Item 2
  • - + + +
  • Bullet List Item 2
  • @@ -36,16 +38,17 @@ exports[`Lift nested lists > Lifts multiple bullet lists with content in between
  • Nested Bullet List Item 2
  • -
  • + In between content -
  • + +
  • Bullet List Item 2
  • @@ -63,7 +66,8 @@ exports[`Lift nested lists > Lifts nested bullet lists 1`] = `
  • Nested Bullet List Item 2
  • - + +
  • Bullet List Item 2
  • @@ -81,9 +85,9 @@ exports[`Lift nested lists > Lifts nested bullet lists with content after nested
  • Nested Bullet List Item 2
  • -
  • + More content in list item 1 -
  • +
  • Bullet List Item 2
  • @@ -118,7 +122,8 @@ exports[`Lift nested lists > Lifts nested mixed lists 1`] = `
  • Bullet List Item 2
  • - + +
  • Numbered List Item 2
  • @@ -136,7 +141,8 @@ exports[`Lift nested lists > Lifts nested numbered lists 1`] = `
  • Nested Numbered List Item 2
  • - + +
  • Numbered List Item 2
  • diff --git a/packages/core/src/api/parsers/html/util/nestedLists.test.ts b/packages/core/src/api/parsers/html/util/nestedLists.test.ts index e88c859784..6ee15c077d 100644 --- a/packages/core/src/api/parsers/html/util/nestedLists.test.ts +++ b/packages/core/src/api/parsers/html/util/nestedLists.test.ts @@ -196,9 +196,10 @@ describe("content after a nested list", () => { ``, ); - // The content after the nested list is in a list item of its own. + // The content after the nested list is a child of the list item: it is in + // the block group, after the nested list. expect(htmlNode.innerHTML).toMatchInlineSnapshot( - `""`, + `""`, ); expect(inActiveDocument).toEqual([]); }); diff --git a/packages/core/src/api/parsers/html/util/nestedLists.ts b/packages/core/src/api/parsers/html/util/nestedLists.ts index e683db2e41..7b3f967c42 100644 --- a/packages/core/src/api/parsers/html/util/nestedLists.ts +++ b/packages/core/src/api/parsers/html/util/nestedLists.ts @@ -1,11 +1,76 @@ import { detachedDoc } from "../../../../util/detachedDoc.js"; -function getChildIndex(node: Element) { - return Array.prototype.indexOf.call(node.parentElement!.childNodes, node); +function isElement(node: Node): node is Element { + return node.nodeType === 1; } -function isWhitespaceNode(node: Node) { - return node.nodeType === 3 && !/\S/.test(node.nodeValue || ""); +function isList(node: Node) { + return node.nodeName === "UL" || node.nodeName === "OL"; +} + +// The elements that end a paragraph in the parse. These are the block elements +// of the ProseMirror DOM parser, and the elements that BlockNote parses as a +// block (the last line). The first three lines are a copy of `blockTags`, which +// prosemirror-model does not export: +// https://github.com/ProseMirror/prosemirror-model/blob/master/src/from_dom.ts +const BLOCK_ELEMENTS = [ + "address, article, aside, blockquote, body, canvas, dd, div, dl, fieldset", + "figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup", + "hr, li, noscript, ol, output, p, pre, section, table, tfoot, ul", + "audio, details, embed, img, video", +].join(", "); + +/** True for a block element, and for an element that has a block in it. */ +function isBlock(node: Node) { + return ( + isElement(node) && + (node.matches(BLOCK_ELEMENTS) || + node.querySelector(BLOCK_ELEMENTS) !== null) + ); +} + +/** + * True for text and for an element with text. These nodes give no content in + * the parse: white space, a comment (node type 8), and an element without text + * (for example an anchor, or the check box of a check list item). + */ +function hasContent(node: Node) { + return node.nodeType !== 8 && /\S/.test(node.textContent || ""); +} + +/** + * True for an element that the parse reads as one block with text: a + * paragraph, a heading, a code block or a quote. + */ +function isTextBlock(node: Node) { + return /^(P|H[1-6]|PRE|BLOCKQUOTE)$/.test(node.nodeName) && hasContent(node); +} + +/** + * Returns the index of the first child in the nodes of a list item that has a + * nested list. The nodes before it are the own content of the list item: its + * text and inline elements up to the first block, or else that first block, if + * it is a block with text. + * + * If the first block is not a block with text (for example an image, or the + * nested list), the nodes before the nested list stay in the list item. The + * parse of the list item (`getListItemContent`) then finds its content. + */ +function childrenStart(nodes: Node[]) { + const firstBlock = nodes.findIndex(isBlock); + if (nodes.slice(0, firstBlock).some(hasContent)) { + return firstBlock; + } + if (isTextBlock(nodes[firstBlock])) { + return firstBlock + 1; + } + return nodes.findIndex(isList); +} + +function createBlockGroup() { + const blockGroup = detachedDoc().createElement("div"); + blockGroup.setAttribute("data-node-type", "blockGroup"); + return blockGroup; } /** @@ -29,32 +94,48 @@ function isWhitespaceNode(node: Node) { * * * + * A list item with content of its own keeps that content. All the nodes after + * it are the children of the list item. They move out of the list item into a + * block group, in order: + * + *
  • + *

    item

    + *
    ...
    + * + * text + *
  • + * + * Into: + *
  • item

  • + *
    + *
    ...
    + * + * text + *
    + * */ function liftNestedListsToParent(element: HTMLElement) { element.querySelectorAll("li > ul, li > ol").forEach((list) => { - const index = getChildIndex(list); const parentListItem = list.parentElement!; - const siblingsAfter = Array.from(parentListItem.childNodes).slice( - index + 1, - ); - list.remove(); - siblingsAfter.forEach((sibling) => { - sibling.remove(); - }); - - parentListItem.insertAdjacentElement("afterend", list); - - siblingsAfter.reverse().forEach((sibling) => { - if (isWhitespaceNode(sibling)) { - return; - } - const siblingContainer = detachedDoc().createElement("li"); - siblingContainer.append(sibling); - list.insertAdjacentElement("afterend", siblingContainer); - }); - if (parentListItem.childNodes.length === 0) { - parentListItem.remove(); + // An earlier list of the same list item moved this list out already. + if (parentListItem.nodeName !== "LI") { + return; } + + const nodes = Array.from(parentListItem.childNodes); + const start = childrenStart(nodes); + const children = nodes.slice(start); + + if (start === 0) { + // The list item starts with the nested list. Its nodes take its place, + // and step 2 gives the nested list to the list item before it. + parentListItem.replaceWith(...children); + return; + } + + const blockGroup = createBlockGroup(); + parentListItem.after(blockGroup); + blockGroup.append(...children); }); } @@ -78,18 +159,21 @@ function liftNestedListsToParent(element: HTMLElement) { * * * + * If step 1 made a block group for the list item, that block group is used. + * * This resulting format is parsed */ function createGroups(element: HTMLElement) { - element.querySelectorAll("li + ul, li + ol").forEach((list) => { - const listItem = list.previousElementSibling as HTMLElement; + const firstChildren = + 'li + ul, li + ol, li + div[data-node-type="blockGroup"]'; + element.querySelectorAll(firstChildren).forEach((firstChild) => { + const listItem = firstChild.previousElementSibling as HTMLElement; const blockContainer = detachedDoc().createElement("div"); listItem.insertAdjacentElement("afterend", blockContainer); blockContainer.append(listItem); - const blockGroup = detachedDoc().createElement("div"); - blockGroup.setAttribute("data-node-type", "blockGroup"); + const blockGroup = isList(firstChild) ? createBlockGroup() : firstChild; blockContainer.append(blockGroup); while ( diff --git a/packages/core/src/blocks/ListItem/NumberedListItem/block.ts b/packages/core/src/blocks/ListItem/NumberedListItem/block.ts index 57530d741b..beca47a7fa 100644 --- a/packages/core/src/blocks/ListItem/NumberedListItem/block.ts +++ b/packages/core/src/blocks/ListItem/NumberedListItem/block.ts @@ -52,11 +52,18 @@ export const createNumberedListItemBlockSpec = createBlockSpec( parent.tagName === "OL" || (parent.tagName === "DIV" && parent.parentElement?.tagName === "OL") ) { - const startIndex = parseInt(parent.getAttribute("start") || "1"); + // The parser puts an item with nested blocks into a `div`, together + // with the block group of these blocks (see `createGroups` in + // `nestedLists.ts`). Then that `div` has the place of the item in the + // list. + const item = parent.tagName === "DIV" ? parent : element; + const startIndex = parseInt( + item.parentElement?.getAttribute("start") || "1", + ); const defaultProps = parseDefaultProps(element); - if (element.previousElementSibling || startIndex === 1) { + if (item.previousElementSibling || startIndex === 1) { return defaultProps; } diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/checkListItemBlocksAroundNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/checkListItemBlocksAroundNestedList.json new file mode 100644 index 0000000000..80090261e2 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/checkListItemBlocksAroundNestedList.json @@ -0,0 +1,55 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "After", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "checked": true, + "textAlignment": "left", + "textColor": "default", + }, + "type": "checkListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/emptyNestedCheckListItem.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/emptyNestedCheckListItem.json index f98bf7027f..756f3ab988 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/emptyNestedCheckListItem.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/emptyNestedCheckListItem.json @@ -13,26 +13,26 @@ }, "type": "checkListItem", }, - ], - "content": [], - "id": "1", - "props": { - "backgroundColor": "default", - "textAlignment": "left", - "textColor": "default", - }, - "type": "bulletListItem", - }, - { - "children": [], - "content": [ { - "styles": {}, - "text": "Paragraph", - "type": "text", + "children": [], + "content": [ + { + "styles": {}, + "text": "Paragraph", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", }, ], - "id": "3", + "content": [], + "id": "1", "props": { "backgroundColor": "default", "textAlignment": "left", diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemBlockBeforeNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemBlockBeforeNestedList.json new file mode 100644 index 0000000000..b5246b77d2 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemBlockBeforeNestedList.json @@ -0,0 +1,69 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Code", + "type": "text", + }, + ], + "id": "2", + "props": { + "language": "javascript", + }, + "type": "codeBlock", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Item 2", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemBlocksAroundNestedLists.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemBlocksAroundNestedLists.json new file mode 100644 index 0000000000..0c8591235a --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemBlocksAroundNestedLists.json @@ -0,0 +1,122 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Before", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 1", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Between", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 2", + "type": "text", + }, + ], + "id": "5", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "After", + "type": "text", + }, + ], + "id": "6", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Item 2", + "type": "text", + }, + ], + "id": "7", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemContentInDivBeforeNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemContentInDivBeforeNestedList.json new file mode 100644 index 0000000000..f99d36f46c --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemContentInDivBeforeNestedList.json @@ -0,0 +1,37 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemEmptyNodeBeforeContent.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemEmptyNodeBeforeContent.json new file mode 100644 index 0000000000..2d61d9f3e0 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemEmptyNodeBeforeContent.json @@ -0,0 +1,177 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 1", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 2", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 2", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 3", + "type": "text", + }, + ], + "id": "6", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 3", + "type": "text", + }, + ], + "id": "5", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 4", + "type": "text", + }, + ], + "id": "8", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 4", + "type": "text", + }, + ], + "id": "7", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 5", + "type": "text", + }, + ], + "id": "10", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 5", + "type": "text", + }, + ], + "id": "9", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemEmptyParagraphBeforeContent.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemEmptyParagraphBeforeContent.json new file mode 100644 index 0000000000..f99d36f46c --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemEmptyParagraphBeforeContent.json @@ -0,0 +1,37 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemHeadingCodeQuoteBeforeNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemHeadingCodeQuoteBeforeNestedList.json new file mode 100644 index 0000000000..3d2b5d2465 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemHeadingCodeQuoteBeforeNestedList.json @@ -0,0 +1,158 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Child 1", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 1", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Heading", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Child 2", + "type": "text", + }, + ], + "id": "5", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 2", + "type": "text", + }, + ], + "id": "6", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Code", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Child 3", + "type": "text", + }, + ], + "id": "8", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 3", + "type": "text", + }, + ], + "id": "9", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Quote", + "type": "text", + }, + ], + "id": "7", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemImageAfterNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemImageAfterNestedList.json new file mode 100644 index 0000000000..ca2aefb695 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemImageAfterNestedList.json @@ -0,0 +1,68 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": undefined, + "id": "3", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "", + "showPreview": true, + "textAlignment": "left", + "url": "exampleURL", + }, + "type": "image", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Item 2", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemLinkedImageBeforeNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemLinkedImageBeforeNestedList.json new file mode 100644 index 0000000000..8bc60f3a4f --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemLinkedImageBeforeNestedList.json @@ -0,0 +1,51 @@ +[ + { + "children": [ + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "alt", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/image.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemParagraphAfterNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemParagraphAfterNestedList.json new file mode 100644 index 0000000000..46259dd67e --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemParagraphAfterNestedList.json @@ -0,0 +1,71 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "After", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Item 2", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemTextAfterNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemTextAfterNestedList.json new file mode 100644 index 0000000000..118b321ba2 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemTextAfterNestedList.json @@ -0,0 +1,78 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": " After ", + "type": "text", + }, + { + "styles": { + "bold": true, + }, + "text": "bold", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Item 2", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemTextAndBlockBeforeNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemTextAndBlockBeforeNestedList.json new file mode 100644 index 0000000000..11a3d63913 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/listItemTextAndBlockBeforeNestedList.json @@ -0,0 +1,54 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Child", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/nestedCheckListInOwnListItem.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/nestedCheckListInOwnListItem.json new file mode 100644 index 0000000000..db9d68cf82 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/nestedCheckListInOwnListItem.json @@ -0,0 +1,57 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "checked": false, + "textAlignment": "left", + "textColor": "default", + }, + "type": "checkListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "checked": true, + "textAlignment": "left", + "textColor": "default", + }, + "type": "checkListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Item 2", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "checked": false, + "textAlignment": "left", + "textColor": "default", + }, + "type": "checkListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/nestedListInAndNextToListItem.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/nestedListInAndNextToListItem.json new file mode 100644 index 0000000000..1e4ed87ee0 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/nestedListInAndNextToListItem.json @@ -0,0 +1,54 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 1", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested 2", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/nestedListInOwnListItem.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/nestedListInOwnListItem.json new file mode 100644 index 0000000000..460e19bf88 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/nestedListInOwnListItem.json @@ -0,0 +1,54 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Item 2", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/orderedListStartWithNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/orderedListStartWithNestedList.json new file mode 100644 index 0000000000..c534ad46e9 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/orderedListStartWithNestedList.json @@ -0,0 +1,90 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested Item 1", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "List Item 2", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "start": 2, + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested Item 2", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "List Item 3", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "List Item 4", + "type": "text", + }, + ], + "id": "5", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/complex.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/complex.json index 8f30c274ae..18c925e2c5 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/complex.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/complex.json @@ -261,24 +261,23 @@ }, "type": "bulletListItem", }, - ], - "content": [ { - "styles": {}, - "text": "Bullet List Item", - "type": "text", + "children": [], + "content": [ + { + "styles": {}, + "text": "Paragraph", + "type": "text", + }, + ], + "id": "17", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", }, - ], - "id": "15", - "props": { - "backgroundColor": "default", - "textAlignment": "left", - "textColor": "default", - }, - "type": "bulletListItem", - }, - { - "children": [ { "children": [], "content": [ @@ -369,11 +368,11 @@ "content": [ { "styles": {}, - "text": "Paragraph", + "text": "Bullet List Item", "type": "text", }, ], - "id": "17", + "id": "15", "props": { "backgroundColor": "default", "textAlignment": "left", diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/listItemParagraphAfterNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/listItemParagraphAfterNestedList.json new file mode 100644 index 0000000000..46259dd67e --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/listItemParagraphAfterNestedList.json @@ -0,0 +1,71 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "After", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item 1", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Item 2", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index 33f4be5e4d..f907e0a38b 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -389,6 +389,164 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // The cases below are for a list item with a nested list and other blocks. + // All the blocks stay in the list item, in the order of the HTML. + { + testCase: { + name: "listItemParagraphAfterNestedList", + content: ``, + }, + executeTest: testParseHTML, + }, + // The text after the nested list is a paragraph. It keeps one space of the + // HTML source at its start, as each text after a block does in the parse. + { + testCase: { + name: "listItemTextAfterNestedList", + content: ``, + }, + executeTest: testParseHTML, + }, + { + testCase: { + name: "listItemImageAfterNestedList", + content: ``, + }, + executeTest: testParseHTML, + }, + { + testCase: { + name: "listItemBlockBeforeNestedList", + content: ``, + }, + executeTest: testParseHTML, + }, + { + testCase: { + name: "listItemBlocksAroundNestedLists", + content: `
    1. Item 1

      Before

      Between

      1. Nested 2

      After

    2. Item 2

    `, + }, + executeTest: testParseHTML, + }, + { + testCase: { + name: "checkListItemBlocksAroundNestedList", + content: ``, + }, + executeTest: testParseHTML, + }, + // A comment or an empty element before the content of a list item is not + // the content of the list item. + { + testCase: { + name: "listItemEmptyNodeBeforeContent", + content: ``, + }, + executeTest: testParseHTML, + }, + // An empty paragraph before the content of a list item is not the content + // of the list item. Tiptap writes this form for an empty first line. + { + testCase: { + name: "listItemEmptyParagraphBeforeContent", + content: ``, + }, + executeTest: testParseHTML, + }, + // The HTML export of Notion: the text of the list item is directly in the + // `
  • `, and a child block comes after it. + { + testCase: { + name: "listItemTextAndBlockBeforeNestedList", + content: ``, + }, + executeTest: testParseHTML, + }, + // GitHub puts an image into a link. The image is a block, thus the text + // before the link is the content of the list item. + { + testCase: { + name: "listItemLinkedImageBeforeNestedList", + content: ``, + }, + executeTest: testParseHTML, + }, + // A markdown renderer writes these forms for a list item that starts with a + // heading, a code block or a quote. + { + testCase: { + name: "listItemHeadingCodeQuoteBeforeNestedList", + content: ``, + }, + executeTest: testParseHTML, + }, + // Plate puts the content of a list item into a `div`. The `div` is not the + // first child block of the list item. + { + testCase: { + name: "listItemContentInDivBeforeNestedList", + content: ``, + }, + executeTest: testParseHTML, + }, + // Lexical puts a nested list into a list item of its own. The nested list + // goes to the list item before it. + { + testCase: { + name: "nestedListInOwnListItem", + content: ``, + }, + executeTest: testParseHTML, + }, + // The same form with check boxes. Lexical writes the check state as + // `aria-checked`, which the parser does not read. Thus this case has `input` + // elements. + { + testCase: { + name: "nestedCheckListInOwnListItem", + content: ``, + }, + executeTest: testParseHTML, + }, + // A nested list in the list item, and a list next to the list item (the + // form of Google Docs). Both are children of the list item. No known tool + // writes the two forms together. + { + testCase: { + name: "nestedListInAndNextToListItem", + content: ``, + }, + executeTest: testParseHTML, + }, { testCase: { name: "textTableCell", @@ -913,6 +1071,27 @@ console.log("Third Line")`,
  • List Item 2
  • List Item 3
  • List Item 4
  • +`, + }, + executeTest: testParseHTML, + }, + // Only the first item gets the start of the list, also if items have a + // nested list. + { + testCase: { + name: "orderedListStartWithNestedList", + content: `
      +
    1. List Item 2 + +
    2. +
    3. List Item 3 + +
    4. +
    5. List Item 4
    `, }, executeTest: testParseHTML, @@ -1270,6 +1449,21 @@ Paragraph * Bullet List Item 1. Numbered List Item +`, + }, + executeTest: testParseMarkdown, + }, + // The paragraph after the nested list stays in its list item. + { + testCase: { + name: "listItemParagraphAfterNestedList", + content: `* Item 1 + + * Nested + + After + +* Item 2 `, }, executeTest: testParseMarkdown,