From 8e18e7a95836c3e73629d43bb4f1cda47024f5fa Mon Sep 17 00:00:00 2001 From: Snorre Eskeland Brekke Date: Tue, 9 Aug 2022 16:11:16 +0200 Subject: [PATCH] fix: ignore blocks inside list items This fixes a bug where p-tags (and other block elements) inside li elements where incorrectly hoisted out of the li element. With this change blocks are ignored, and block children are added directly to the listItem instead. --- .gitignore | 4 ++++ .../src/HtmlDeserializer/rules/html.ts | 2 +- .../tests/HtmlDeserializer/lists/input.html | 4 ++++ .../tests/HtmlDeserializer/lists/output.json | 24 ++++++++++++++++++- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a63124cafe2..0cc25632e99 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,10 @@ jspm_packages # macOS finder cache file .DS_Store +# Intellij +.idea +*.iml + # VS Code settings .vscode diff --git a/packages/@sanity/block-tools/src/HtmlDeserializer/rules/html.ts b/packages/@sanity/block-tools/src/HtmlDeserializer/rules/html.ts index 7dac81f02bc..71851e4a35d 100644 --- a/packages/@sanity/block-tools/src/HtmlDeserializer/rules/html.ts +++ b/packages/@sanity/block-tools/src/HtmlDeserializer/rules/html.ts @@ -91,7 +91,7 @@ export default function createHTMLRules(blockContentType, options: any = {}) { return undefined } // Don't add blocks into list items - if (el.parentNode && tagName(el) === 'li') { + if (el.parentNode && tagName(el.parentNode) === 'li') { return next(el.childNodes) } // If style is not supported, return a defaultBlockType diff --git a/packages/@sanity/block-tools/test/tests/HtmlDeserializer/lists/input.html b/packages/@sanity/block-tools/test/tests/HtmlDeserializer/lists/input.html index 9898b0379c5..7b2fa783bdb 100644 --- a/packages/@sanity/block-tools/test/tests/HtmlDeserializer/lists/input.html +++ b/packages/@sanity/block-tools/test/tests/HtmlDeserializer/lists/input.html @@ -22,6 +22,10 @@
  • Link
  • +
  • +

    p in li.

    +

    block children are still processed.

    +
  • diff --git a/packages/@sanity/block-tools/test/tests/HtmlDeserializer/lists/output.json b/packages/@sanity/block-tools/test/tests/HtmlDeserializer/lists/output.json index 81f4fd8325f..2f2f8193666 100644 --- a/packages/@sanity/block-tools/test/tests/HtmlDeserializer/lists/output.json +++ b/packages/@sanity/block-tools/test/tests/HtmlDeserializer/lists/output.json @@ -126,5 +126,27 @@ } ], "style": "normal" + }, + { + "_key": "randomKey8", + "_type": "block", + "children": [ + { + "_key": "randomKey80", + "_type": "span", + "marks": [], + "text": "p in li. block children are still " + }, + { + "_key": "randomKey81", + "_type": "span", + "marks": ["strong"], + "text": "processed." + } + ], + "level": 1, + "listItem": "bullet", + "markDefs": [], + "style": "normal" } -] \ No newline at end of file +]