Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit d99c568

Browse files
authored
Merge pull request #1059 from ckeditor/t/1058
Fix: `view.Range#getTrimmed()` was returning incorrect ranges in some cases. Fixes #1058.
2 parents 43e5ccc + f0864bb commit d99c568

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/view/range.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ export default class Range {
9898
* @returns {module:engine/view/range~Range} Enlarged range.
9999
*/
100100
getEnlarged() {
101-
let start = this.start.getLastMatchingPosition( enlargeShrinkSkip, { direction: 'backward' } );
102-
let end = this.end.getLastMatchingPosition( enlargeShrinkSkip );
101+
let start = this.start.getLastMatchingPosition( enlargeTrimSkip, { direction: 'backward' } );
102+
let end = this.end.getLastMatchingPosition( enlargeTrimSkip );
103103

104104
// Fix positions, in case if they are in Text node.
105105
if ( start.parent.is( 'text' ) && start.isAtStart ) {
@@ -130,8 +130,8 @@ export default class Range {
130130
* @returns {module:engine/view/range~Range} Shrink range.
131131
*/
132132
getTrimmed() {
133-
let start = this.start.getLastMatchingPosition( enlargeShrinkSkip );
134-
let end = this.end.getLastMatchingPosition( enlargeShrinkSkip, { direction: 'backward' } );
133+
let start = this.start.getLastMatchingPosition( enlargeTrimSkip, { boundaries: new Range( this.start, this.end ) } );
134+
let end = this.end.getLastMatchingPosition( enlargeTrimSkip, { boundaries: new Range( start, this.end ), direction: 'backward' } );
135135
const nodeAfterStart = start.nodeAfter;
136136
const nodeBeforeEnd = end.nodeBefore;
137137

@@ -438,7 +438,7 @@ export default class Range {
438438
}
439439

440440
// Function used by getEnlagred and getShrinked methods.
441-
function enlargeShrinkSkip( value ) {
441+
function enlargeTrimSkip( value ) {
442442
if ( value.item.is( 'attributeElement' ) || value.item.is( 'uiElement' ) ) {
443443
return true;
444444
}

tests/view/range.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,36 @@ describe( 'Range', () => {
162162
.to.equal( '<p><b>f</b>{oo}<b><span></span>bar</b></p>' );
163163
} );
164164

165-
it( 'case6', () => {
165+
it( 'case 6', () => {
166166
expect( trim( '<p>foo[</p><p>bar</p><p>]bom</p>' ) )
167167
.to.equal( '<p>foo[</p><p>bar</p><p>]bom</p>' );
168168
} );
169169

170-
it( 'case7', () => {
170+
it( 'case 7', () => {
171171
expect( trim( '<p>foo[<b><img></img></b>]bom</p>' ) )
172172
.to.equal( '<p>foo<b>[<img></img>]</b>bom</p>' );
173173
} );
174174

175+
// Other results may theoretically be correct too. It is not decided whether the trimmed range should
176+
// be collapsed in attribute element, at its start or its end. This is one of possible correct results
177+
// and we won't know for sure unless we have more cases. See #1058.
178+
it( 'case 8', () => {
179+
expect( trim( '<p>[<b></b>]</p>' ) )
180+
.to.equal( '<p><b></b>[]</p>' );
181+
} );
182+
183+
// As above.
184+
it( 'case 9', () => {
185+
expect( trim( '<p><b></b>[<b></b>]<b></b></p>' ) )
186+
.to.equal( '<p><b></b><b></b>[]<b></b></p>' );
187+
} );
188+
189+
// As above.
190+
it( 'case 10', () => {
191+
expect( trim( '<p>[<b></b><b></b>]</p>' ) )
192+
.to.equal( '<p><b></b><b></b>[]</p>' );
193+
} );
194+
175195
function trim( data ) {
176196
data = data
177197
.replace( /<p>/g, '<container:p>' )

0 commit comments

Comments
 (0)