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

Commit

Permalink
Reused a part of the repated code from ContainerElement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Piechaczek committed Nov 13, 2018
1 parent c244c2c commit 378b862
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"dependencies": {
"@ckeditor/ckeditor5-core": "^11.0.1",
"@ckeditor/ckeditor5-engine": "^11.0.0",
"@ckeditor/ckeditor5-paragraph": "^10.0.3",
"@ckeditor/ckeditor5-ui": "^11.1.0",
"@ckeditor/ckeditor5-utils": "^11.0.0"
Expand All @@ -20,7 +21,6 @@
"@ckeditor/ckeditor5-block-quote": "^10.1.0",
"@ckeditor/ckeditor5-clipboard": "^10.0.3",
"@ckeditor/ckeditor5-editor-classic": "^11.0.1",
"@ckeditor/ckeditor5-engine": "^11.0.0",
"@ckeditor/ckeditor5-enter": "^10.1.2",
"@ckeditor/ckeditor5-heading": "^10.1.0",
"@ckeditor/ckeditor5-link": "^10.0.4",
Expand Down
17 changes: 5 additions & 12 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @module list/utils
*/

import { getFillerOffset } from '@ckeditor/ckeditor5-engine/src/view/containerelement';

/**
* Creates list item {@link module:engine/view/containerelement~ContainerElement}.
*
Expand All @@ -15,29 +17,20 @@
*/
export function createViewListItemElement( writer ) {
const viewItem = writer.createContainerElement( 'li' );
viewItem.getFillerOffset = getFillerOffset;
viewItem.getFillerOffset = _getFillerOffset;

return viewItem;
}

// Implementation of getFillerOffset for view list item element.
//
// @returns {Number|null} Block filler offset or `null` if block filler is not needed.
function getFillerOffset() {
function _getFillerOffset() {
const hasOnlyLists = !this.isEmpty && ( this.getChild( 0 ).name == 'ul' || this.getChild( 0 ).name == 'ol' );

if ( this.isEmpty || hasOnlyLists ) {
return 0;
}

const children = [ ...this.getChildren() ];
const lastChild = children[ this.childCount - 1 ];

// Block filler is required after a `<br>` if it's the last element in its container.
// See: https://github.com/ckeditor/ckeditor5/issues/1312#issuecomment-436669045.
if ( lastChild && lastChild.is( 'element', 'br' ) ) {
return this.childCount;
}

return null;
return getFillerOffset.call( this );
}
9 changes: 9 additions & 0 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ describe( 'utils', () => {
expect( item.getFillerOffset() ).to.equal( 4 );
} );

it( 'ignores the ui elements', () => {
const item = createViewListItemElement( writer );

writer.insert( writer.createPositionAt( item, 0 ), writer.createUIElement( 'span' ) );
writer.insert( writer.createPositionAt( item, 1 ), writer.createEmptyElement( 'br' ) );

expect( item.getFillerOffset() ).to.equal( 2 );
} );

it( 'empty element must be the <br> element', () => {
const item = createViewListItemElement( writer );

Expand Down

0 comments on commit 378b862

Please sign in to comment.