浏览代码

Merge pull request #90 from ckeditor/t/89

Other: Removed ViewListItemElement class and introduced createViewListItemElement utility method. Closes #89.
Piotr Jasiun 7 年之前
父节点
当前提交
9c4e6d87f4

+ 2 - 3
packages/ckeditor5-list/src/converters.js

@@ -7,8 +7,6 @@
  * @module list/converters
  */
 
-import ViewListItemElement from './viewlistitemelement';
-
 import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
 import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
 
@@ -17,6 +15,7 @@ import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
 import ViewRange from '@ckeditor/ckeditor5-engine/src/view/range';
 import ViewTreeWalker from '@ckeditor/ckeditor5-engine/src/view/treewalker';
 import viewWriter from '@ckeditor/ckeditor5-engine/src/view/writer';
+import { createViewListItemElement } from './utils';
 
 /**
  * A model-to-view converter for `listItem` model element insertion.
@@ -792,7 +791,7 @@ export function modelIndentPasteFixer( evt, [ content, selection ] ) {
 // The function then returns created view list item (<li>).
 function generateLiInUl( modelItem, mapper ) {
 	const listType = modelItem.getAttribute( 'type' ) == 'numbered' ? 'ol' : 'ul';
-	const viewItem = new ViewListItemElement();
+	const viewItem = createViewListItemElement();
 
 	const viewList = new ViewContainerElement( listType, null );
 	viewList.appendChildren( viewItem );

+ 31 - 0
packages/ckeditor5-list/src/utils.js

@@ -0,0 +1,31 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
+
+/**
+ * @module list/utils
+ */
+
+/**
+ * Creates list item {@link module:engine/view/containerelement~ContainerElement}.
+ *
+ * @returns {module:engine/view/containerelement~ContainerElement}
+ */
+export function createViewListItemElement() {
+	const viewItem = new ViewContainerElement( 'li' );
+	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() {
+	const hasOnlyLists = !this.isEmpty && ( this.getChild( 0 ).name == 'ul' || this.getChild( 0 ).name == 'ol' );
+
+	return this.isEmpty || hasOnlyLists ? 0 : null;
+}

+ 0 - 44
packages/ckeditor5-list/src/viewlistitemelement.js

@@ -1,44 +0,0 @@
-/**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/**
- * @module list/viewlistitemelement
- */
-
-import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
-
-/**
- * View element class representing a list item (`<li>`). It extends {@link module:engine/view/containerelement~ContainerElement}
- * and overwrites {@link module:list/viewlistitemelement~ViewListItemElement#getFillerOffset evaluating whether filler offset}
- * is needed.
- *
- * @extends module:engine/view/containerelement~ContainerElement
- */
-export default class ViewListItemElement extends ViewContainerElement {
-	/**
-	 * Creates a `<li>` view item.
-	 *
-	 * @param {Object|Iterable} [attrs] A collection of attributes.
-	 * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children] The list of nodes to be inserted
-	 * into the created element.
-	 */
-	constructor( attrs, children ) {
-		super( 'li', attrs, children );
-
-		/**
-		 * @inheritDoc
-		 */
-		this.getFillerOffset = getFillerOffset;
-	}
-}
-
-// Implementation of getFillerOffset for ViewListItemElements.
-//
-// @returns {Number|null} Block filler offset or `null` if block filler is not needed.
-function getFillerOffset() {
-	const hasOnlyLists = !this.isEmpty && ( this.getChild( 0 ).name == 'ul' || this.getChild( 0 ).name == 'ol' );
-
-	return this.isEmpty || hasOnlyLists ? 0 : null;
-}

+ 51 - 0
packages/ckeditor5-list/tests/utils.js

@@ -0,0 +1,51 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
+import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
+import { createViewListItemElement } from '../src/utils';
+
+describe( 'utils', () => {
+	describe( 'createViewListItemElement()', () => {
+		it( 'should create ViewContainerElement', () => {
+			const item = createViewListItemElement();
+
+			expect( item ).to.be.instanceof( ViewContainerElement );
+		} );
+
+		it( 'should have li name', () => {
+			const item = createViewListItemElement();
+
+			expect( item.name ).to.equal( 'li' );
+		} );
+
+		describe( 'getFillerOffset', () => {
+			it( 'should return 0 if item is empty', () => {
+				const item = createViewListItemElement();
+
+				expect( item.getFillerOffset() ).to.equal( 0 );
+			} );
+
+			it( 'should return 0 if item has only lists as children', () => {
+				const innerListItem1 = createViewListItemElement();
+				innerListItem1.appendChildren( new ViewText( 'foo' ) );
+				const innerListItem2 = createViewListItemElement();
+				innerListItem2.appendChildren( new ViewText( 'bar' ) );
+				const innerList = new ViewContainerElement( 'ul', null, [ innerListItem1, innerListItem2 ] );
+				const outerListItem = createViewListItemElement();
+				outerListItem.appendChildren( innerList );
+
+				expect( outerListItem.getFillerOffset() ).to.equal( 0 );
+			} );
+
+			it( 'should return null if item has non-list contents', () => {
+				const item = createViewListItemElement();
+				item.appendChildren( new ViewText( 'foo' ) );
+
+				expect( item.getFillerOffset() ).to.be.null;
+			} );
+		} );
+	} );
+} );

+ 0 - 47
packages/ckeditor5-list/tests/viewlistitemelement.js

@@ -1,47 +0,0 @@
-/**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-import ViewListItemElement from '../src/viewlistitemelement';
-import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
-import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
-
-describe( 'ViewListItemElement', () => {
-	it( 'should extend ViewContainerElement', () => {
-		const item = new ViewListItemElement();
-
-		expect( item ).to.be.instanceof( ViewContainerElement );
-	} );
-
-	it( 'should have li name', () => {
-		const item = new ViewListItemElement();
-
-		expect( item.name ).to.equal( 'li' );
-	} );
-
-	describe( 'getFillerOffset', () => {
-		it( 'should return 0 if item is empty', () => {
-			const item = new ViewListItemElement();
-
-			expect( item.getFillerOffset() ).to.equal( 0 );
-		} );
-
-		it( 'should return 0 if item has only lists as children', () => {
-			const item = new ViewListItemElement( null, [
-				new ViewContainerElement( 'ul', null, [
-					new ViewListItemElement( null, new ViewText( 'foo' ) ),
-					new ViewListItemElement( null, new ViewText( 'bar' ) )
-				] )
-			] );
-
-			expect( item.getFillerOffset() ).to.equal( 0 );
-		} );
-
-		it( 'should return null if item has non-list contents', () => {
-			const item = new ViewListItemElement( null, new ViewText( 'foo' ) );
-
-			expect( item.getFillerOffset() ).to.be.null;
-		} );
-	} );
-} );