|
| 1 | +import {Schema} from 'prosemirror-model'; |
| 2 | +import {builders} from 'prosemirror-test-builder'; |
| 3 | + |
| 4 | +import {trimListItems} from './utils'; |
| 5 | + |
| 6 | +const schema = new Schema({ |
| 7 | + nodes: { |
| 8 | + doc: {content: 'block+'}, |
| 9 | + text: {group: 'inline'}, |
| 10 | + paragraph: {group: 'block', content: 'text*', toDOM: () => ['p', 0]}, |
| 11 | + bullet_list: {content: 'list_item+', group: 'block'}, |
| 12 | + ordered_list: {content: 'list_item+', group: 'block'}, |
| 13 | + list_item: {content: 'block+'}, |
| 14 | + }, |
| 15 | + marks: {}, |
| 16 | +}); |
| 17 | + |
| 18 | +const {doc, paragraph: p} = builders(schema); |
| 19 | +const bulletList = builders(schema)['bullet_list']; |
| 20 | +const orderedList = builders(schema)['ordered_list']; |
| 21 | +const listItem = builders(schema)['list_item']; |
| 22 | + |
| 23 | +describe('trimListItems', () => { |
| 24 | + it('removes empty list items at the start and end of a bullet list', () => { |
| 25 | + const fragment = doc( |
| 26 | + bulletList(listItem(), listItem(p('11')), listItem(p('22')), listItem()), |
| 27 | + ).content; |
| 28 | + |
| 29 | + const trimmedFragment = trimListItems(fragment); |
| 30 | + expect(schema.nodes.doc.create(null, trimmedFragment)).toMatchNode( |
| 31 | + doc(bulletList(listItem(p('11')), listItem(p('22')))), |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it('removes empty list items at the start and end of an ordered list', () => { |
| 36 | + const fragment = doc( |
| 37 | + orderedList(listItem(), listItem(p('11')), listItem(p('22')), listItem()), |
| 38 | + ).content; |
| 39 | + |
| 40 | + const trimmedFragment = trimListItems(fragment); |
| 41 | + expect(schema.nodes.doc.create(null, trimmedFragment)).toMatchNode( |
| 42 | + doc(orderedList(listItem(p('11')), listItem(p('22')))), |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it('removes only empty list items at the start and end, keeping empty ones in the middle', () => { |
| 47 | + const fragment = doc( |
| 48 | + bulletList(listItem(), listItem(p('11')), listItem(), listItem(p('22')), listItem()), |
| 49 | + ).content; |
| 50 | + |
| 51 | + const trimmedFragment = trimListItems(fragment); |
| 52 | + expect(schema.nodes.doc.create(null, trimmedFragment)).toMatchNode( |
| 53 | + doc(bulletList(listItem(p('11')), listItem(), listItem(p('22')))), |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('does not modify a list with no empty items', () => { |
| 58 | + const fragment = doc(bulletList(listItem(p('11')), listItem(p('22')))).content; |
| 59 | + |
| 60 | + const trimmedFragment = trimListItems(fragment); |
| 61 | + expect(schema.nodes.doc.create(null, trimmedFragment)).toMatchNode( |
| 62 | + doc(bulletList(listItem(p('11')), listItem(p('22')))), |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it('removes an entire list if it contains only empty list items', () => { |
| 67 | + const fragment = doc(bulletList(listItem(), listItem(), listItem())).content; |
| 68 | + |
| 69 | + const trimmedFragment = trimListItems(fragment); |
| 70 | + expect(schema.nodes.doc.create(null, trimmedFragment)).toMatchNode(doc()); |
| 71 | + }); |
| 72 | + |
| 73 | + it('correctly handles multiple lists in a single fragment', () => { |
| 74 | + const fragment = doc( |
| 75 | + bulletList(listItem(), listItem(p('11')), listItem(), listItem(p('22')), listItem()), |
| 76 | + orderedList(listItem(), listItem(p('A')), listItem(p('B')), listItem()), |
| 77 | + ).content; |
| 78 | + |
| 79 | + const trimmedFragment = trimListItems(fragment); |
| 80 | + expect(schema.nodes.doc.create(null, trimmedFragment)).toMatchNode( |
| 81 | + doc( |
| 82 | + bulletList(listItem(p('11')), listItem(), listItem(p('22'))), |
| 83 | + orderedList(listItem(p('A')), listItem(p('B'))), |
| 84 | + ), |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it('does not modify paragraphs and other nodes outside lists', () => { |
| 89 | + const fragment = doc( |
| 90 | + p('Some text'), |
| 91 | + bulletList(listItem(), listItem(p('11')), listItem(p('22')), listItem()), |
| 92 | + p('More text'), |
| 93 | + ).content; |
| 94 | + |
| 95 | + const trimmedFragment = trimListItems(fragment); |
| 96 | + expect(schema.nodes.doc.create(null, trimmedFragment)).toMatchNode( |
| 97 | + doc(p('Some text'), bulletList(listItem(p('11')), listItem(p('22'))), p('More text')), |
| 98 | + ); |
| 99 | + }); |
| 100 | +}); |
0 commit comments