From 6dca97ec7e35e2caec8cc954f5c508c17be5a274 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Thu, 9 Aug 2018 14:28:54 +0200 Subject: [PATCH] Changed: Make `Schema#getValidRanges` a generator. --- src/model/schema.js | 37 +++++++++++-------------------------- tests/model/schema.js | 2 +- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/model/schema.js b/src/model/schema.js index da6a66aa3..180ec353a 100644 --- a/src/model/schema.js +++ b/src/model/schema.js @@ -665,19 +665,14 @@ export default class Schema { * * @param {Array.} ranges Ranges to be validated. * @param {String} attribute The name of the attribute to check. - * @returns {Array.} Ranges in which the attribute is allowed. + * @returns {Iterator.} Ranges in which the attribute is allowed. */ - getValidRanges( ranges, attribute ) { + * getValidRanges( ranges, attribute ) { ranges = convertToMinimalFlatRanges( ranges ); - const result = []; for ( const range of ranges ) { - const validRanges = this._getValidRangesForRange( range, attribute ); - - result.push( ...validRanges ); + yield* this._getValidRangesForRange( range, attribute ); } - - return result; } /** @@ -689,22 +684,20 @@ export default class Schema { * @private * @param {module:engine/model/range~Range} range Range to process. * @param {String} attribute The name of the attribute to check. - * @returns {Array.} Ranges in which the attribute is allowed. + * @returns {Iterator.} Ranges in which the attribute is allowed. */ - _getValidRangesForRange( range, attribute ) { - const result = []; - + * _getValidRangesForRange( range, attribute ) { let start = range.start; let end = range.start; for ( const item of range.getItems( { shallow: true } ) ) { if ( item.is( 'element' ) ) { - result.push( ...this._getValidRangesForRange( Range.createIn( item ), attribute ) ); + yield* this._getValidRangesForRange( Range.createIn( item ), attribute ); } if ( !this.checkAttribute( item, attribute ) ) { if ( !start.isEqual( end ) ) { - result.push( new Range( start, end ) ); + yield new Range( start, end ); } start = Position.createAfter( item ); @@ -714,10 +707,8 @@ export default class Schema { } if ( !start.isEqual( end ) ) { - result.push( new Range( start, end ) ); + yield new Range( start, end ); } - - return result; } /** @@ -1602,15 +1593,9 @@ function* combineWalkers( backward, forward ) { // all those minimal flat ranges. // // @param {Array.} ranges Ranges to process. -// @returns {Array.} Minimal flat ranges of given `ranges`. -function convertToMinimalFlatRanges( ranges ) { - const result = []; - +// @returns {Iterator.} Minimal flat ranges of given `ranges`. +function* convertToMinimalFlatRanges( ranges ) { for ( const range of ranges ) { - const minimal = range.getMinimalFlatRanges(); - - result.push( ...minimal ); + yield* range.getMinimalFlatRanges(); } - - return result; } diff --git a/tests/model/schema.js b/tests/model/schema.js index fd85549a7..890341eb6 100644 --- a/tests/model/schema.js +++ b/tests/model/schema.js @@ -1178,7 +1178,7 @@ describe( 'Schema', () => { setData( model, '[

foobar

]' ); - const validRanges = schema.getValidRanges( doc.selection.getRanges(), 'foo' ); + const validRanges = Array.from( schema.getValidRanges( doc.selection.getRanges(), 'foo' ) ); expect( validRanges.length ).to.equal( 2 );