|
| 1 | +/** |
| 2 | + * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. |
| 3 | + * For licensing, see LICENSE.md. |
| 4 | + */ |
| 5 | + |
| 6 | +/* globals document */ |
| 7 | + |
| 8 | +import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor'; |
| 9 | +import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; |
| 10 | +import Position from '../../src/model/position'; |
| 11 | + |
| 12 | +import { setData as setModelData, getData as getModelData } from '../../src/dev-utils/model'; |
| 13 | + |
| 14 | +describe( 'Bug ckeditor5-engine#1281', () => { |
| 15 | + let element, editor, model; |
| 16 | + |
| 17 | + beforeEach( () => { |
| 18 | + element = document.createElement( 'div' ); |
| 19 | + document.body.appendChild( element ); |
| 20 | + |
| 21 | + return ClassicTestEditor |
| 22 | + .create( element, { plugins: [ Paragraph ] } ) |
| 23 | + .then( newEditor => { |
| 24 | + editor = newEditor; |
| 25 | + model = editor.model; |
| 26 | + } ); |
| 27 | + } ); |
| 28 | + |
| 29 | + afterEach( () => { |
| 30 | + element.remove(); |
| 31 | + |
| 32 | + return editor.destroy(); |
| 33 | + } ); |
| 34 | + |
| 35 | + it( 'loads content that contains multi-range selection', () => { |
| 36 | + setModelData( model, |
| 37 | + '<paragraph>Paragraph 1.</paragraph>' + |
| 38 | + '<paragraph>Paragraph 2.</paragraph>' + |
| 39 | + '<paragraph>[Paragraph 3.]</paragraph>' + |
| 40 | + '<paragraph>[Paragraph 4.]</paragraph>' |
| 41 | + ); |
| 42 | + |
| 43 | + const root = model.document.getRoot(); |
| 44 | + const thirdParagraph = root.getNodeByPath( [ 2 ] ); |
| 45 | + const fourthParagraph = root.getNodeByPath( [ 3 ] ); |
| 46 | + const selRanges = Array.from( model.document.selection.getRanges() ); |
| 47 | + |
| 48 | + expect( selRanges.length ).to.equal( 2 ); |
| 49 | + |
| 50 | + assertPositions( Position.createAt( thirdParagraph ), selRanges[ 0 ].start ); |
| 51 | + assertPositions( Position.createAt( thirdParagraph, 'end' ), selRanges[ 0 ].end ); |
| 52 | + |
| 53 | + assertPositions( Position.createAt( fourthParagraph ), selRanges[ 1 ].start ); |
| 54 | + assertPositions( Position.createAt( fourthParagraph, 'end' ), selRanges[ 1 ].end ); |
| 55 | + } ); |
| 56 | + |
| 57 | + it( 'does not throw an error when content before the selection is being removed (last element is selected)', () => { |
| 58 | + setModelData( model, |
| 59 | + '<paragraph>Paragraph 1.</paragraph>' + |
| 60 | + '<paragraph>Paragraph 2.</paragraph>' + |
| 61 | + '<paragraph>[Paragraph 3.]</paragraph>' + |
| 62 | + '<paragraph>[Paragraph 4.]</paragraph>' |
| 63 | + ); |
| 64 | + |
| 65 | + model.change( writer => { |
| 66 | + const root = model.document.getRoot(); |
| 67 | + const firstParagraph = root.getNodeByPath( [ 0 ] ); |
| 68 | + |
| 69 | + expect( () => { |
| 70 | + writer.remove( firstParagraph ); |
| 71 | + } ).to.not.throw(); |
| 72 | + |
| 73 | + assertOutput( |
| 74 | + '<paragraph>Paragraph 2.</paragraph>' + |
| 75 | + '<paragraph>[Paragraph 3.]</paragraph>' + |
| 76 | + '<paragraph>[Paragraph 4.]</paragraph>' |
| 77 | + ); |
| 78 | + } ); |
| 79 | + } ); |
| 80 | + |
| 81 | + it( 'does not throw an error when content before the selection is being removed (last element is not selected)', () => { |
| 82 | + setModelData( model, |
| 83 | + '<paragraph>Paragraph 1.</paragraph>' + |
| 84 | + '<paragraph>Paragraph 2.</paragraph>' + |
| 85 | + '<paragraph>[Paragraph 3.]</paragraph>' + |
| 86 | + '<paragraph>[Paragraph 4.]</paragraph>' + |
| 87 | + '<paragraph>Paragraph 5.</paragraph>' |
| 88 | + ); |
| 89 | + |
| 90 | + model.change( writer => { |
| 91 | + const root = model.document.getRoot(); |
| 92 | + const firstParagraph = root.getNodeByPath( [ 0 ] ); |
| 93 | + |
| 94 | + expect( () => { |
| 95 | + writer.remove( firstParagraph ); |
| 96 | + } ).to.not.throw(); |
| 97 | + |
| 98 | + assertOutput( |
| 99 | + '<paragraph>Paragraph 2.</paragraph>' + |
| 100 | + '<paragraph>[Paragraph 3.]</paragraph>' + |
| 101 | + '<paragraph>[Paragraph 4.]</paragraph>' + |
| 102 | + '<paragraph>Paragraph 5.</paragraph>' |
| 103 | + ); |
| 104 | + } ); |
| 105 | + } ); |
| 106 | + |
| 107 | + it( 'does not throw an error when content after the selection is being removed (first element is selected)', () => { |
| 108 | + setModelData( model, |
| 109 | + '<paragraph>[Paragraph 1.]</paragraph>' + |
| 110 | + '<paragraph>Paragraph 2.</paragraph>' + |
| 111 | + '<paragraph>Paragraph 3.</paragraph>' + |
| 112 | + '<paragraph>[Paragraph 4.]</paragraph>' + |
| 113 | + '<paragraph>Paragraph 5.</paragraph>' |
| 114 | + ); |
| 115 | + |
| 116 | + model.change( writer => { |
| 117 | + const root = model.document.getRoot(); |
| 118 | + const lastParagraph = root.getNodeByPath( [ 4 ] ); |
| 119 | + |
| 120 | + expect( () => { |
| 121 | + writer.remove( lastParagraph ); |
| 122 | + } ).to.not.throw(); |
| 123 | + |
| 124 | + assertOutput( |
| 125 | + '<paragraph>[Paragraph 1.]</paragraph>' + |
| 126 | + '<paragraph>Paragraph 2.</paragraph>' + |
| 127 | + '<paragraph>Paragraph 3.</paragraph>' + |
| 128 | + '<paragraph>[Paragraph 4.]</paragraph>' |
| 129 | + ); |
| 130 | + } ); |
| 131 | + } ); |
| 132 | + |
| 133 | + it( 'does not throw an error when content after the selection is being removed (first element is not selected)', () => { |
| 134 | + setModelData( model, |
| 135 | + '<paragraph>Paragraph 1.</paragraph>' + |
| 136 | + '<paragraph>Paragraph 2.</paragraph>' + |
| 137 | + '<paragraph>[Paragraph 3.]</paragraph>' + |
| 138 | + '<paragraph>[Paragraph 4.]</paragraph>' + |
| 139 | + '<paragraph>Paragraph 5.</paragraph>' |
| 140 | + ); |
| 141 | + |
| 142 | + model.change( writer => { |
| 143 | + const root = model.document.getRoot(); |
| 144 | + const lastParagraph = root.getNodeByPath( [ 4 ] ); |
| 145 | + |
| 146 | + expect( () => { |
| 147 | + writer.remove( lastParagraph ); |
| 148 | + } ).to.not.throw(); |
| 149 | + |
| 150 | + assertOutput( |
| 151 | + '<paragraph>Paragraph 1.</paragraph>' + |
| 152 | + '<paragraph>Paragraph 2.</paragraph>' + |
| 153 | + '<paragraph>[Paragraph 3.]</paragraph>' + |
| 154 | + '<paragraph>[Paragraph 4.]</paragraph>' |
| 155 | + ); |
| 156 | + } ); |
| 157 | + } ); |
| 158 | + |
| 159 | + it( 'does not throw an error when content between the selection\'s ranges is being removed (last element is selected)', () => { |
| 160 | + setModelData( model, |
| 161 | + '<paragraph>Paragraph 1.</paragraph>' + |
| 162 | + '<paragraph>[Paragraph 2.]</paragraph>' + |
| 163 | + '<paragraph>Paragraph 3.</paragraph>' + |
| 164 | + '<paragraph>[Paragraph 4.]</paragraph>' |
| 165 | + ); |
| 166 | + |
| 167 | + model.change( writer => { |
| 168 | + const root = model.document.getRoot(); |
| 169 | + const thirdParagraph = root.getNodeByPath( [ 2 ] ); |
| 170 | + |
| 171 | + expect( () => { |
| 172 | + writer.remove( thirdParagraph ); |
| 173 | + } ).to.not.throw(); |
| 174 | + |
| 175 | + assertOutput( |
| 176 | + '<paragraph>Paragraph 1.</paragraph>' + |
| 177 | + '<paragraph>[Paragraph 2.]</paragraph>' + |
| 178 | + '<paragraph>[Paragraph 4.]</paragraph>' |
| 179 | + ); |
| 180 | + } ); |
| 181 | + } ); |
| 182 | + |
| 183 | + it( 'does not throw an error when content between the selection\'s ranges is being removed (last element is not selected)', () => { |
| 184 | + setModelData( model, |
| 185 | + '<paragraph>Paragraph 1.</paragraph>' + |
| 186 | + '<paragraph>[Paragraph 2.]</paragraph>' + |
| 187 | + '<paragraph>Paragraph 3.</paragraph>' + |
| 188 | + '<paragraph>[Paragraph 4.]</paragraph>' + |
| 189 | + '<paragraph>Paragraph 5.</paragraph>' |
| 190 | + ); |
| 191 | + |
| 192 | + model.change( writer => { |
| 193 | + const root = model.document.getRoot(); |
| 194 | + const thirdParagraph = root.getNodeByPath( [ 2 ] ); |
| 195 | + |
| 196 | + expect( () => { |
| 197 | + writer.remove( thirdParagraph ); |
| 198 | + } ).to.not.throw(); |
| 199 | + |
| 200 | + assertOutput( |
| 201 | + '<paragraph>Paragraph 1.</paragraph>' + |
| 202 | + '<paragraph>[Paragraph 2.]</paragraph>' + |
| 203 | + '<paragraph>[Paragraph 4.]</paragraph>' + |
| 204 | + '<paragraph>Paragraph 5.</paragraph>' |
| 205 | + ); |
| 206 | + } ); |
| 207 | + } ); |
| 208 | + |
| 209 | + function assertPositions( firstPosition, secondPosition ) { |
| 210 | + expect( firstPosition.isEqual( secondPosition ) ).to.be.true; |
| 211 | + } |
| 212 | + |
| 213 | + function assertOutput( output ) { |
| 214 | + expect( getModelData( model ) ).to.equal( output ); |
| 215 | + } |
| 216 | +} ); |
0 commit comments