Bladeren bron

Tests: Added tests to check the placeholder feature of the UI. Code refactoring.

Aleksander Nowodzinski 6 jaren geleden
bovenliggende
commit
d2bfda10be

+ 1 - 1
packages/ckeditor5-editor-inline/src/inlineeditor.js

@@ -70,7 +70,7 @@ export default class InlineEditor extends Editor {
 			this.sourceElement = sourceElementOrData;
 		}
 
-		const view = new InlineEditorUIView( this.locale, this.sourceElement, this.editing.view );
+		const view = new InlineEditorUIView( this.locale, this.editing.view, this.sourceElement );
 		this.ui = new InlineEditorUI( this, view );
 
 		attachToForm( this );

+ 2 - 1
packages/ckeditor5-editor-inline/src/inlineeditorui.js

@@ -162,7 +162,8 @@ export default class InlineEditorUI extends EditorUI {
 		const editingView = editor.editing.view;
 		const editingRoot = editingView.document.getRoot();
 
-		const placeholderText = editor.config.get( 'placeholder' ) || editor.sourceElement.getAttribute( 'placeholder' );
+		const placeholderText = editor.config.get( 'placeholder' ) ||
+			editor.sourceElement && editor.sourceElement.getAttribute( 'placeholder' );
 
 		if ( placeholderText ) {
 			enablePlaceholder( {

+ 2 - 1
packages/ckeditor5-editor-inline/src/inlineeditoruiview.js

@@ -22,10 +22,11 @@ export default class InlineEditorUIView extends EditorUIView {
 	 * Creates an instance of the inline editor UI view.
 	 *
 	 * @param {module:utils/locale~Locale} locale The {@link module:core/editor/editor~Editor#locale} instance.
+	 * @param {module:engine/view/view~View} editingView The editing view instance this view is related to.
 	 * @param {HTMLElement} [editableElement] The editable element. If not specified, it will be automatically created by
 	 * {@link module:ui/editableui/editableuiview~EditableUIView}. Otherwise, the given element will be used.
 	 */
-	constructor( locale, editableElement, editingView ) {
+	constructor( locale, editingView, editableElement ) {
 		super( locale );
 
 		/**

+ 73 - 18
packages/ckeditor5-editor-inline/tests/inlineeditorui.js

@@ -10,11 +10,13 @@ import View from '@ckeditor/ckeditor5-ui/src/view';
 import InlineEditorUI from '../src/inlineeditorui';
 import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
 import InlineEditorUIView from '../src/inlineeditoruiview';
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import { isElement } from 'lodash-es';
 
 describe( 'InlineEditorUI', () => {
 	let editor, view, ui, viewElement;
@@ -23,7 +25,7 @@ describe( 'InlineEditorUI', () => {
 
 	beforeEach( () => {
 		return VirtualInlineTestEditor
-			.create( {
+			.create( 'foo', {
 				toolbar: [ 'foo', 'bar' ],
 			} )
 			.then( newEditor => {
@@ -65,7 +67,7 @@ describe( 'InlineEditorUI', () => {
 
 			it( 'sets view#viewportTopOffset, if specified', () => {
 				return VirtualInlineTestEditor
-					.create( {
+					.create( 'foo', {
 						toolbar: {
 							viewportTopOffset: 100
 						}
@@ -128,23 +130,72 @@ describe( 'InlineEditorUI', () => {
 					{ isFocused: true }
 				);
 			} );
+		} );
 
-			it( 'binds view.editable#isReadOnly', () => {
-				utils.assertBinding(
-					view.editable,
-					{ isReadOnly: false },
-					[
-						[ editable, { isReadOnly: true } ]
-					],
-					{ isReadOnly: true }
-				);
+		describe( 'placeholder', () => {
+			it( 'sets placeholder from editor.config.placeholder', () => {
+				return VirtualInlineTestEditor
+					.create( 'foo', {
+						extraPlugins: [ Paragraph ],
+						placeholder: 'placeholder-text',
+					} )
+					.then( newEditor => {
+						editor = newEditor;
+
+						const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
+
+						expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
+
+						return editor.destroy();
+					} );
+			} );
+
+			it( 'sets placeholder from "placeholder" attribute of a passed element', () => {
+				const element = document.createElement( 'div' );
+
+				element.setAttribute( 'placeholder', 'placeholder-text' );
+
+				return VirtualInlineTestEditor
+					.create( element, {
+						extraPlugins: [ Paragraph ]
+					} )
+					.then( newEditor => {
+						editor = newEditor;
+
+						const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
+
+						expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
+
+						return editor.destroy();
+					} );
+			} );
+
+			it( 'uses editor.config.placeholder rather than "placeholder" attribute of a passed element', () => {
+				const element = document.createElement( 'div' );
+
+				element.setAttribute( 'placeholder', 'placeholder-text' );
+
+				return VirtualInlineTestEditor
+					.create( element, {
+						placeholder: 'config takes precedence',
+						extraPlugins: [ Paragraph ]
+					} )
+					.then( newEditor => {
+						editor = newEditor;
+
+						const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
+
+						expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'config takes precedence' );
+
+						return editor.destroy();
+					} );
 			} );
 		} );
 
 		describe( 'view.toolbar#items', () => {
 			it( 'are filled with the config.toolbar (specified as an Array)', () => {
 				return VirtualInlineTestEditor
-					.create( {
+					.create( '', {
 						toolbar: [ 'foo', 'bar' ]
 					} )
 					.then( editor => {
@@ -159,7 +210,7 @@ describe( 'InlineEditorUI', () => {
 
 			it( 'are filled with the config.toolbar (specified as an Object)', () => {
 				return VirtualInlineTestEditor
-					.create( {
+					.create( '', {
 						toolbar: {
 							items: [ 'foo', 'bar' ],
 							viewportTopOffset: 100
@@ -177,7 +228,7 @@ describe( 'InlineEditorUI', () => {
 		} );
 
 		it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
-			return VirtualInlineTestEditor.create()
+			return VirtualInlineTestEditor.create( '' )
 				.then( editor => {
 					const ui = editor.ui;
 					const view = ui.view;
@@ -233,10 +284,14 @@ function viewCreator( name ) {
 }
 
 class VirtualInlineTestEditor extends VirtualTestEditor {
-	constructor( config ) {
+	constructor( sourceElementOrData, config ) {
 		super( config );
 
-		const view = new InlineEditorUIView( this.locale );
+		if ( isElement( sourceElementOrData ) ) {
+			this.sourceElement = sourceElementOrData;
+		}
+
+		const view = new InlineEditorUIView( this.locale, this.editing.view );
 		this.ui = new InlineEditorUI( this, view );
 
 		this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
@@ -249,9 +304,9 @@ class VirtualInlineTestEditor extends VirtualTestEditor {
 		return super.destroy();
 	}
 
-	static create( config ) {
+	static create( sourceElementOrData, config ) {
 		return new Promise( resolve => {
-			const editor = new this( config );
+			const editor = new this( sourceElementOrData, config );
 
 			resolve(
 				editor.initPlugins()

+ 18 - 3
packages/ckeditor5-editor-inline/tests/inlineeditoruiview.js

@@ -4,6 +4,8 @@
  */
 
 import InlineEditorUIView from '../src/inlineeditoruiview';
+import EditingView from '@ckeditor/ckeditor5-engine/src/view/view';
+import ViewRootEditableElement from '@ckeditor/ckeditor5-engine/src/view/rooteditableelement';
 import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
 import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
 import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
@@ -12,13 +14,15 @@ import Locale from '@ckeditor/ckeditor5-utils/src/locale';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 
 describe( 'InlineEditorUIView', () => {
-	let locale, view;
+	let locale, view, editingView, editingViewRoot;
 
 	testUtils.createSinonSandbox();
 
 	beforeEach( () => {
 		locale = new Locale( 'en' );
-		view = new InlineEditorUIView( locale );
+		setUpEditingView();
+		view = new InlineEditorUIView( locale, editingView );
+		view.editable.name = editingViewRoot.rootName;
 	} );
 
 	describe( 'constructor()', () => {
@@ -110,7 +114,11 @@ describe( 'InlineEditorUIView', () => {
 
 	describe( 'init()', () => {
 		it( 'appends #toolbar to panel#content', () => {
-			const view = new InlineEditorUIView( locale );
+			locale = new Locale( 'en' );
+			setUpEditingView();
+			const view = new InlineEditorUIView( locale, editingView );
+
+			view.editable.name = editingViewRoot.rootName;
 
 			expect( view.panel.content ).to.have.length( 0 );
 
@@ -305,4 +313,11 @@ describe( 'InlineEditorUIView', () => {
 			} );
 		}
 	} );
+
+	function setUpEditingView() {
+		editingView = new EditingView();
+		editingViewRoot = new ViewRootEditableElement( 'div' );
+		editingViewRoot._document = editingView.document;
+		editingView.document.roots.add( editingViewRoot );
+	}
 } );