Skip to content

Commit

Permalink
fix: ignore empty spans
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Jul 23, 2024
1 parent ad9a37b commit ab94da3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/lib/RichTextFieldBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions test/__snapshots__/helpers-unstable_htmlAsRichText.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `
[
{
Expand Down
5 changes: 5 additions & 0 deletions test/helpers-unstable_htmlAsRichText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ describe("transforms HTML to rich text", () => {
expectHTMLToMatchInputExactly: false,
});

testHTMLAsRichTextHelper("empty spans", {
input: /* html */ `<p>lorem <strong></strong>ipsum dolor sit amet</p>`,
expectHTMLToMatchInputExactly: false,
});

testHTMLAsRichTextHelper("nested spans", {
input: /* html */ `<p>lorem <strong>ips<em>um <a href="https://prismic.io">dolor</a></em></strong> sit amet</p>`,
});
Expand Down

0 comments on commit ab94da3

Please sign in to comment.