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

Commit

Permalink
Emptiness check for placeholder containing elements will check number…
Browse files Browse the repository at this point in the history
… of its children.
  • Loading branch information
jodator committed Jun 19, 2019
1 parent da5670a commit b2203e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/view/placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,18 @@ export function needsPlaceholder( element ) {
return false;
}

// The element is empty only as long as it contains nothing but uiElements.
const isEmptyish = !Array.from( element.getChildren() )
.some( element => !element.is( 'uiElement' ) );
const isEmpty = !element.childCount;

// If the element is empty and the document is blurred.
if ( !doc.isFocused && isEmptyish ) {
if ( !doc.isFocused && isEmpty ) {
return true;
}

const viewSelection = doc.selection;
const selectionAnchor = viewSelection.anchor;

// If document is focused and the element is empty but the selection is not anchored inside it.
if ( isEmptyish && selectionAnchor && selectionAnchor.parent !== element ) {
if ( isEmpty && selectionAnchor && selectionAnchor.parent !== element ) {
return true;
}

Expand Down
14 changes: 11 additions & 3 deletions tests/view/placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe( 'placeholder', () => {
expect( element.hasClass( 'ck-placeholder' ) ).to.be.false;
} );

it( 'if element has only ui elements, set CSS class and data attribute', () => {
it( 'if element has only ui elements set only data attribute', () => {
setData( view, '<div><ui:span></ui:span><ui:span></ui:span></div><div>{another div}</div>' );
const element = viewRoot.getChild( 0 );

Expand All @@ -80,7 +80,7 @@ describe( 'placeholder', () => {
} );

expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'foo bar baz' );
expect( element.hasClass( 'ck-placeholder' ) ).to.be.true;
expect( element.hasClass( 'ck-placeholder' ) ).to.be.false;
} );

it( 'if element has selection inside set only data attribute', () => {
Expand Down Expand Up @@ -398,12 +398,20 @@ describe( 'placeholder', () => {
expect( needsPlaceholder( element ) ).to.be.true;
} );

it( 'should return true if element hosts UI elements only and document is blurred', () => {
it( 'should return false if element hosts UI elements only and document is blurred', () => {
setData( view, '<p><ui:span></ui:span></p>' );
viewDocument.isFocused = false;

const element = viewRoot.getChild( 0 );

expect( needsPlaceholder( element ) ).to.be.false;
} );

it( 'should return true if element is empty is set on it', () => {
setData( view, '[<p></p>]' );

const element = viewRoot.getChild( 0 );

expect( needsPlaceholder( element ) ).to.be.true;
} );

Expand Down

0 comments on commit b2203e7

Please sign in to comment.