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

Commit

Permalink
Changed: Make Schema#getValidRanges a generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
scofalik committed Aug 9, 2018
1 parent af94040 commit 6dca97e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
37 changes: 11 additions & 26 deletions src/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,19 +665,14 @@ export default class Schema {
*
* @param {Array.<module:engine/model/range~Range>} ranges Ranges to be validated.
* @param {String} attribute The name of the attribute to check.
* @returns {Array.<module:engine/model/range~Range>} Ranges in which the attribute is allowed.
* @returns {Iterator.<module:engine/model/range~Range>} 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;
}

/**
Expand All @@ -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.<module:engine/model/range~Range>} Ranges in which the attribute is allowed.
* @returns {Iterator.<module:engine/model/range~Range>} 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 );
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -1602,15 +1593,9 @@ function* combineWalkers( backward, forward ) {
// all those minimal flat ranges.
//
// @param {Array.<module:engine/model/range~Range>} ranges Ranges to process.
// @returns {Array.<module:engine/model/range~Range>} Minimal flat ranges of given `ranges`.
function convertToMinimalFlatRanges( ranges ) {
const result = [];

// @returns {Iterator.<module:engine/model/range~Range>} 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;
}
2 changes: 1 addition & 1 deletion tests/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ describe( 'Schema', () => {

setData( model, '[<p>foo<img></img>bar</p>]' );

const validRanges = schema.getValidRanges( doc.selection.getRanges(), 'foo' );
const validRanges = Array.from( schema.getValidRanges( doc.selection.getRanges(), 'foo' ) );

expect( validRanges.length ).to.equal( 2 );

Expand Down

0 comments on commit 6dca97e

Please sign in to comment.