Skip to content

Commit

Permalink
fix: ignore trimmed empty spans
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Jul 23, 2024
1 parent 19819cf commit 6d1ce03
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/lib/hastToRichText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const hastToRichText = (
case RichTextNodeType.em:
case RichTextNodeType.label:
case RichTextNodeType.hyperlink: {
const length = toString(node).length;
const length = toString(node).trimEnd().length;

try {
builder.appendSpan(shorthandOrNode, length);
Expand Down Expand Up @@ -238,7 +238,7 @@ export const hastToRichText = (
case RichTextNodeType.label:
case RichTextNodeType.hyperlink: {
const span = serializeSpan(node, match);
const length = toString(node).length;
const length = toString(node).trimEnd().length;

try {
builder.appendSpan(span, length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ exports[`transforms HTML to rich text > spans > empty spans 1`] = `
{
"direction": "ltr",
"spans": [],
"text": "lorem ipsum dolor sit amet",
"text": "lorem ipsum dolorsit amet",
"type": "paragraph",
},
]
Expand Down
4 changes: 3 additions & 1 deletion test/helpers-unstable_htmlAsRichText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ describe("transforms HTML to rich text", () => {
});

testHTMLAsRichTextHelper("empty spans", {
input: /* html */ `<p>lorem <strong></strong>ipsum dolor sit amet</p>`,
// It's normal for `dolor` and `sit` to be concatenated here.
// That how the browser would render it.
input: /* html */ `<p>lorem <strong></strong>ipsum dolor<em> </em>sit amet</p>`,
expectHTMLToMatchInputExactly: false,
});

Expand Down

0 comments on commit 6d1ce03

Please sign in to comment.