Skip to content

Commit b1b9f89

Browse files
committed
Fix invalid content error for empty mention labels in search editor initialization
This change fixes a crash during editor initialization. The problem was that when a mention had an empty label, we created a mention text node with an empty string. ProseMirror does not allow empty text nodes, so Remirror failed when setting content. The fix is simple: when building mention text, we now use the label if it exists, and fall back to the mention id if the label is empty. This keeps the generated document valid.
1 parent 1508c9a commit b1b9f89

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/search-editor/utils/__tests__/search-to-remirror.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,57 @@ describe('search-to-remirror', () => {
174174
],
175175
});
176176
});
177+
178+
it('falls back to id when mention label is empty', async () => {
179+
const fieldsWithEmptyLabel: Fields = {
180+
...FIELDS,
181+
label: {
182+
...FIELDS.label,
183+
getExtraAttrs: async (id: string) => {
184+
if (!id) {
185+
return;
186+
}
187+
return { id, label: '', name: 'label' };
188+
},
189+
},
190+
};
191+
192+
const actual = await searchToRemirror({
193+
searchQuery: 'label:___label#label1',
194+
fields: fieldsWithEmptyLabel,
195+
});
196+
197+
expect(actual).toEqual({
198+
type: 'doc',
199+
content: [
200+
{
201+
type: 'paragraph',
202+
content: [
203+
{
204+
text: 'label:',
205+
type: 'text',
206+
},
207+
{
208+
marks: [
209+
{
210+
attrs: {
211+
id: '___label#label1',
212+
label: '',
213+
name: 'label',
214+
},
215+
type: 'mention',
216+
},
217+
],
218+
text: '___label#label1',
219+
type: 'text',
220+
},
221+
{
222+
text: ' ',
223+
type: 'text',
224+
},
225+
],
226+
},
227+
],
228+
});
229+
});
177230
});

src/search-editor/utils/search-to-remirror.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ function createMentionMarkJSON(
5151
if (!attrs) {
5252
return [];
5353
}
54+
55+
// ProseMirror rejects empty text nodes. Fall back to id when label is empty.
56+
const mentionText = `${attrs.label ?? ''}` || `${attrs.id ?? ''}`;
57+
5458
return [
5559
{
5660
type: 'text',
5761
text: `${attrs.name}:`,
5862
},
5963
{
6064
type: 'text',
61-
text: String(attrs.label),
65+
text: mentionText,
6266
marks: [
6367
{
6468
type: 'mention',

0 commit comments

Comments
 (0)