diff --git a/src/lib/RichTextFieldBuilder.ts b/src/lib/RichTextFieldBuilder.ts index d0ce1bbf..77034d42 100644 --- a/src/lib/RichTextFieldBuilder.ts +++ b/src/lib/RichTextFieldBuilder.ts @@ -110,6 +110,11 @@ export class RichTextFieldBuilder { } } + // Ignore empty spans. + if (span.start === span.end) { + return; + } + // Prefer to extend the last span of the same type end // position if it ends at the start of the new span. if (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) { diff --git a/test/__snapshots__/helpers-unstable_htmlAsRichText.test.ts.snap b/test/__snapshots__/helpers-unstable_htmlAsRichText.test.ts.snap index c66640ae..3478057c 100644 --- a/test/__snapshots__/helpers-unstable_htmlAsRichText.test.ts.snap +++ b/test/__snapshots__/helpers-unstable_htmlAsRichText.test.ts.snap @@ -996,6 +996,17 @@ exports[`transforms HTML to rich text > spans > directly adjacent spans > does n ] `; +exports[`transforms HTML to rich text > spans > empty spans 1`] = ` +[ + { + "direction": "ltr", + "spans": [], + "text": "lorem ipsum dolor sit amet", + "type": "paragraph", + }, +] +`; + exports[`transforms HTML to rich text > spans > hyperlink 1`] = ` [ { diff --git a/test/helpers-unstable_htmlAsRichText.test.ts b/test/helpers-unstable_htmlAsRichText.test.ts index e2e9e9cb..b3e2c2eb 100644 --- a/test/helpers-unstable_htmlAsRichText.test.ts +++ b/test/helpers-unstable_htmlAsRichText.test.ts @@ -46,6 +46,11 @@ describe("transforms HTML to rich text", () => { expectHTMLToMatchInputExactly: false, }); + testHTMLAsRichTextHelper("empty spans", { + input: /* html */ `

lorem ipsum dolor sit amet

`, + expectHTMLToMatchInputExactly: false, + }); + testHTMLAsRichTextHelper("nested spans", { input: /* html */ `

lorem ipsum dolor sit amet

`, });