|
| 1 | +import {type Kit, runAlphabetKitTestSuite} from '../../__tests__/setup'; |
| 2 | +import {toHtml, toJsonMl} from '../../export/export'; |
| 3 | +import {CommonSliceType} from '../../slice'; |
| 4 | + |
| 5 | +const runTests = (setup: () => Kit) => { |
| 6 | + describe('JSON-ML', () => { |
| 7 | + test('can export two paragraphs', () => { |
| 8 | + const {editor, peritext} = setup(); |
| 9 | + editor.cursor.setAt(10); |
| 10 | + editor.saved.insMarker(CommonSliceType.p); |
| 11 | + peritext.refresh(); |
| 12 | + const fragment = peritext.fragment(peritext.rangeAt(4, 10)); |
| 13 | + fragment.refresh(); |
| 14 | + const html = toJsonMl(fragment.toJson()); |
| 15 | + expect(html).toEqual(['', null, ['p', null, 'efghij'], ['p', null, 'klm']]); |
| 16 | + }); |
| 17 | + |
| 18 | + test('can export two paragraphs with inline formatting', () => { |
| 19 | + const {editor, peritext} = setup(); |
| 20 | + editor.cursor.setAt(10); |
| 21 | + editor.saved.insMarker(CommonSliceType.p); |
| 22 | + editor.cursor.setAt(6, 2); |
| 23 | + editor.saved.insOverwrite(CommonSliceType.b); |
| 24 | + editor.cursor.setAt(7, 2); |
| 25 | + editor.saved.insOverwrite(CommonSliceType.i); |
| 26 | + peritext.refresh(); |
| 27 | + const fragment = peritext.fragment(peritext.rangeAt(4, 10)); |
| 28 | + fragment.refresh(); |
| 29 | + const html = toJsonMl(fragment.toJson()); |
| 30 | + expect(html).toEqual([ |
| 31 | + '', |
| 32 | + null, |
| 33 | + ['p', null, 'ef', ['b', null, 'g'], ['i', null, ['b', null, 'h']], ['i', null, 'i'], 'j'], |
| 34 | + ['p', null, 'klm'], |
| 35 | + ]); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('HTML', () => { |
| 40 | + test('can export two paragraphs', () => { |
| 41 | + const {editor, peritext} = setup(); |
| 42 | + editor.cursor.setAt(10); |
| 43 | + editor.saved.insMarker(CommonSliceType.p); |
| 44 | + peritext.refresh(); |
| 45 | + const fragment = peritext.fragment(peritext.rangeAt(4, 10)); |
| 46 | + fragment.refresh(); |
| 47 | + const html = toHtml(fragment.toJson()); |
| 48 | + expect(html).toBe('<p>efghij</p><p>klm</p>'); |
| 49 | + }); |
| 50 | + |
| 51 | + test('can export two paragraphs (formatted)', () => { |
| 52 | + const {editor, peritext} = setup(); |
| 53 | + editor.cursor.setAt(10); |
| 54 | + editor.saved.insMarker(CommonSliceType.p); |
| 55 | + peritext.refresh(); |
| 56 | + const fragment = peritext.fragment(peritext.rangeAt(4, 10)); |
| 57 | + fragment.refresh(); |
| 58 | + const html = toHtml(fragment.toJson(), ' '); |
| 59 | + expect(html).toBe('<p>efghij</p>\n<p>klm</p>'); |
| 60 | + }); |
| 61 | + |
| 62 | + test('can export two paragraphs (formatted and wrapped in <div>)', () => { |
| 63 | + const {editor, peritext} = setup(); |
| 64 | + editor.cursor.setAt(10); |
| 65 | + editor.saved.insMarker(CommonSliceType.p); |
| 66 | + peritext.refresh(); |
| 67 | + const fragment = peritext.fragment(peritext.rangeAt(4, 10)); |
| 68 | + fragment.refresh(); |
| 69 | + const json = fragment.toJson(); |
| 70 | + json[0] = 'div'; |
| 71 | + const html = toHtml(json, ' '); |
| 72 | + expect(html).toBe('<div>\n <p>efghij</p>\n <p>klm</p>\n</div>'); |
| 73 | + }); |
| 74 | + |
| 75 | + test('can export two paragraphs with inline formatting', () => { |
| 76 | + const {editor, peritext} = setup(); |
| 77 | + editor.cursor.setAt(10); |
| 78 | + editor.saved.insMarker(CommonSliceType.p); |
| 79 | + editor.cursor.setAt(6, 2); |
| 80 | + editor.saved.insOverwrite(CommonSliceType.b); |
| 81 | + editor.cursor.setAt(7, 2); |
| 82 | + editor.saved.insOverwrite(CommonSliceType.i); |
| 83 | + peritext.refresh(); |
| 84 | + const fragment = peritext.fragment(peritext.rangeAt(4, 10)); |
| 85 | + fragment.refresh(); |
| 86 | + const json = fragment.toJson(); |
| 87 | + const html = toHtml(json, ''); |
| 88 | + expect(html).toEqual('<p>ef<b>g</b><i><b>h</b></i><i>i</i>j</p><p>klm</p>'); |
| 89 | + }); |
| 90 | + |
| 91 | + test('can export two paragraphs with inline formatting (formatted)', () => { |
| 92 | + const {editor, peritext} = setup(); |
| 93 | + editor.cursor.setAt(10); |
| 94 | + editor.saved.insMarker(CommonSliceType.p); |
| 95 | + editor.cursor.setAt(6, 2); |
| 96 | + editor.saved.insOverwrite(CommonSliceType.b); |
| 97 | + editor.cursor.setAt(7, 2); |
| 98 | + editor.saved.insOverwrite(CommonSliceType.i); |
| 99 | + peritext.refresh(); |
| 100 | + const fragment = peritext.fragment(peritext.rangeAt(4, 10)); |
| 101 | + fragment.refresh(); |
| 102 | + const json = fragment.toJson(); |
| 103 | + const html = toHtml(json, ' '); |
| 104 | + expect(html).toEqual( |
| 105 | + '<p>\n ef\n <b>g</b>\n <i>\n <b>h</b>\n </i>\n <i>i</i>\n j\n</p>\n<p>klm</p>', |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + test('can export two paragraphs with inline formatting (formatted, wrapped in <div>)', () => { |
| 110 | + const {editor, peritext} = setup(); |
| 111 | + editor.cursor.setAt(10); |
| 112 | + editor.saved.insMarker(CommonSliceType.p); |
| 113 | + editor.cursor.setAt(6, 2); |
| 114 | + editor.saved.insOverwrite(CommonSliceType.b); |
| 115 | + editor.cursor.setAt(7, 2); |
| 116 | + editor.saved.insOverwrite(CommonSliceType.i); |
| 117 | + peritext.refresh(); |
| 118 | + const fragment = peritext.fragment(peritext.rangeAt(4, 10)); |
| 119 | + fragment.refresh(); |
| 120 | + const json = fragment.toJson(); |
| 121 | + json[0] = 'div'; |
| 122 | + const html = toHtml(json, ' '); |
| 123 | + expect('\n' + html).toEqual(` |
| 124 | +<div> |
| 125 | + <p> |
| 126 | + ef |
| 127 | + <b>g</b> |
| 128 | + <i> |
| 129 | + <b>h</b> |
| 130 | + </i> |
| 131 | + <i>i</i> |
| 132 | + j |
| 133 | + </p> |
| 134 | + <p>klm</p> |
| 135 | +</div>`); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}; |
| 139 | + |
| 140 | +describe('Fragment.toJson()', () => { |
| 141 | + runAlphabetKitTestSuite(runTests); |
| 142 | +}); |
0 commit comments