From 5e51831296fdc79acf1c7df0f387c404913c658f Mon Sep 17 00:00:00 2001 From: Snorre Eskeland Brekke Date: Mon, 15 Aug 2022 09:05:25 +0200 Subject: [PATCH] fix(block-tools): ignore blocks inside list items (#3492) 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 5ea5861993a..b0c874e5d16 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,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 5f967504de2..9caf4ce55de 100644 --- a/packages/@sanity/block-tools/src/HtmlDeserializer/rules/html.ts +++ b/packages/@sanity/block-tools/src/HtmlDeserializer/rules/html.ts @@ -111,7 +111,7 @@ export default function createHTMLRules( 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 +]