-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RichText: List: Fix outdent with children (#13559)
* Fix outdent * Add unit test * Add e2e test * Add unit tests for getLastChildIndex
- Loading branch information
1 parent
e8e9eb9
commit b9cac35
Showing
6 changed files
with
160 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
|
||
import { LINE_SEPARATOR } from './special-characters'; | ||
|
||
/** | ||
* Gets the line index of the last child in the list. | ||
* | ||
* @param {Object} value Value to search. | ||
* @param {number} lineIndex Line index of a list item in the list. | ||
* | ||
* @return {Array} The index of the last child. | ||
*/ | ||
export function getLastChildIndex( { text, formats }, lineIndex ) { | ||
const lineFormats = formats[ lineIndex ] || []; | ||
// Use the given line index in case there are no next children. | ||
let childIndex = lineIndex; | ||
|
||
// `lineIndex` could be `undefined` if it's the first line. | ||
for ( let index = lineIndex || 0; index < text.length; index++ ) { | ||
// We're only interested in line indices. | ||
if ( text[ index ] !== LINE_SEPARATOR ) { | ||
continue; | ||
} | ||
|
||
const formatsAtIndex = formats[ index ] || []; | ||
|
||
// If the amout of formats is equal or more, store it, then return the | ||
// last one if the amount of formats is less. | ||
if ( formatsAtIndex.length >= lineFormats.length ) { | ||
childIndex = index; | ||
} else { | ||
return childIndex; | ||
} | ||
} | ||
|
||
// If the end of the text is reached, return the last child index. | ||
return childIndex; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import deepFreeze from 'deep-freeze'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
|
||
import { getLastChildIndex } from '../get-last-child-index'; | ||
import { LINE_SEPARATOR } from '../special-characters'; | ||
|
||
describe( 'outdentListItems', () => { | ||
const ul = { type: 'ul' }; | ||
|
||
it( 'should return undefined if there is only one line', () => { | ||
expect( getLastChildIndex( deepFreeze( { | ||
formats: [ , ], | ||
text: '1', | ||
} ), undefined ) ).toBe( undefined ); | ||
} ); | ||
|
||
it( 'should return the last line if no line is indented', () => { | ||
expect( getLastChildIndex( deepFreeze( { | ||
formats: [ , ], | ||
text: `1${ LINE_SEPARATOR }`, | ||
} ), undefined ) ).toBe( 1 ); | ||
} ); | ||
|
||
it( 'should return the last child index', () => { | ||
expect( getLastChildIndex( deepFreeze( { | ||
formats: [ , [ ul ], , [ ul ], , ], | ||
text: `1${ LINE_SEPARATOR }2${ LINE_SEPARATOR }3`, | ||
} ), undefined ) ).toBe( 3 ); | ||
} ); | ||
|
||
it( 'should return the last child index by sibling', () => { | ||
expect( getLastChildIndex( deepFreeze( { | ||
formats: [ , [ ul ], , [ ul ], , ], | ||
text: `1${ LINE_SEPARATOR }2${ LINE_SEPARATOR }3`, | ||
} ), 1 ) ).toBe( 3 ); | ||
} ); | ||
|
||
it( 'should return the last child index (with further lower indented items)', () => { | ||
expect( getLastChildIndex( deepFreeze( { | ||
formats: [ , [ ul ], , , , ], | ||
text: `1${ LINE_SEPARATOR }2${ LINE_SEPARATOR }3`, | ||
} ), 1 ) ).toBe( 1 ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters