Skip to content

Commit

Permalink
RichText: List: Fix getParentIndex (#13562)
Browse files Browse the repository at this point in the history
* RichText: List: Fix getParentIndex

* Fill out test name

* Add unit tests for getParentLineIndex

* Guard against negative lineIndex
  • Loading branch information
ellatrix committed Jan 29, 2019
1 parent ccbf07e commit bf1c941
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
5 changes: 3 additions & 2 deletions packages/rich-text/src/change-list-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import { getParentLineIndex } from './get-parent-line-index';
*/
export function changeListType( value, newFormat ) {
const { text, formats, start, end } = value;
const startLineFormats = formats[ getLineIndex( value, start ) ] || [];
const startingLineIndex = getLineIndex( value, start );
const startLineFormats = formats[ startingLineIndex ] || [];
const endLineFormats = formats[ getLineIndex( value, end ) ] || [];
const startIndex = getParentLineIndex( value, start );
const startIndex = getParentLineIndex( value, startingLineIndex );
const newFormats = formats.slice( 0 );
const startCount = startLineFormats.length - 1;
const endCount = endLineFormats.length - 1;
Expand Down
18 changes: 7 additions & 11 deletions packages/rich-text/src/get-parent-line-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,23 @@ import { LINE_SEPARATOR } from './special-characters';
* go through every list item until we find one with exactly one format type
* less.
*
* @param {Object} value Value to search.
* @param {number} startIndex Index to start search at.
* @param {Object} value Value to search.
* @param {number} lineIndex Line index of a child list item.
*
* @return {Array} The parent list line index.
*/
export function getParentLineIndex( { text, formats }, startIndex ) {
let index = startIndex;
let startFormats;
export function getParentLineIndex( { text, formats }, lineIndex ) {
const startFormats = formats[ lineIndex ] || [];

while ( index-- ) {
let index = lineIndex;

while ( index-- >= 0 ) {
if ( text[ index ] !== LINE_SEPARATOR ) {
continue;
}

const formatsAtIndex = formats[ index ] || [];

if ( ! startFormats ) {
startFormats = formatsAtIndex;
continue;
}

if ( formatsAtIndex.length === startFormats.length - 1 ) {
return index;
}
Expand Down
43 changes: 43 additions & 0 deletions packages/rich-text/src/test/get-parent-line-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';

/**
* Internal dependencies
*/

import { getParentLineIndex } from '../get-parent-line-index';
import { LINE_SEPARATOR } from '../special-characters';

describe( 'getParentLineIndex', () => {
const ul = { type: 'ul' };

it( 'should return undefined if there is only one line', () => {
expect( getParentLineIndex( deepFreeze( {
formats: [ , ],
text: '1',
} ), undefined ) ).toBe( undefined );
} );

it( 'should return undefined if the list is part of the first root list child', () => {
expect( getParentLineIndex( deepFreeze( {
formats: [ , ],
text: `1${ LINE_SEPARATOR }2`,
} ), 2 ) ).toBe( undefined );
} );

it( 'should return the line index of the parent list (1)', () => {
expect( getParentLineIndex( deepFreeze( {
formats: [ , , , [ ul ], , ],
text: `1${ LINE_SEPARATOR }2${ LINE_SEPARATOR }3`,
} ), 3 ) ).toBe( 1 );
} );

it( 'should return the line index of the parent list (2)', () => {
expect( getParentLineIndex( deepFreeze( {
formats: [ , [ ul ], , [ ul, ul ], , [ ul ], , ],
text: `1${ LINE_SEPARATOR }2${ LINE_SEPARATOR }3${ LINE_SEPARATOR }4`,
} ), 5 ) ).toBe( undefined );
} );
} );
22 changes: 22 additions & 0 deletions packages/rich-text/src/test/outdent-list-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,26 @@ describe( 'outdentListItems', () => {
expect( result ).not.toBe( record );
expect( getSparseArrayLength( result.formats ) ).toBe( 2 );
} );

it( 'should outdent list based on parent list', () => {
// As we're testing list formats, the text should remain the same.
const text = `1${ LINE_SEPARATOR }2${ LINE_SEPARATOR }3${ LINE_SEPARATOR }4`;
const record = {
formats: [ , [ ul ], , [ ul, ul ], , [ ul ], , ],
text,
start: 6,
end: 6,
};
const expected = {
formats: [ , [ ul ], , [ ul, ul ], , , , ],
text,
start: 6,
end: 6,
};
const result = outdentListItems( deepFreeze( record ) );

expect( result ).toEqual( expected );
expect( result ).not.toBe( record );
expect( getSparseArrayLength( result.formats ) ).toBe( 2 );
} );
} );

0 comments on commit bf1c941

Please sign in to comment.