8
0
Просмотр исходного кода

Encapsulate some utils functions inside ImageCaptionEngine as they have not much sense as public API.

Szymon Kupś 9 лет назад
Родитель
Сommit
df69c886f9

+ 26 - 3
packages/ckeditor5-image/src/imagecaption/imagecaptionengine.js

@@ -15,15 +15,14 @@ import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
 import ViewRange from '@ckeditor/ckeditor5-engine/src/view/range';
 import viewWriter from '@ckeditor/ckeditor5-engine/src/view/writer';
 import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
+import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
 import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
 import { isImage, isImageWidget } from '../image/utils';
 import {
 	captionElementCreator,
 	isCaption,
 	getCaptionFromImage,
-	isInsideCaption,
-	matchImageCaption,
-	insertViewCaptionAndBind
+	matchImageCaption
 } from './utils';
 
 /**
@@ -249,3 +248,27 @@ function captionModelToView( elementCreator ) {
 		}
 	};
 }
+
+// Returns `true` if provided `node` is placed inside image's caption.
+//
+// @private
+// @param {module:engine/model/node~Node} node
+// @return {Boolean}
+function isInsideCaption( node ) {
+	return !!( node.parent && node.parent.name == 'caption' && node.parent.parent && node.parent.parent.name == 'image' );
+}
+
+// Inserts `viewCaption` at the end of `viewImage` and binds it to `modelCaption`.
+//
+// @private
+// @param {module:engine/view/containerelement~ContainerElement} viewCaption
+// @param {module:engine/model/element~Element} modelCaption
+// @param {module:engine/view/containerelement~ContainerElement} viewImage
+// @param {module:engine/conversion/mapper~Mapper} mapper
+export function insertViewCaptionAndBind( viewCaption, modelCaption, viewImage, mapper ) {
+	const viewPosition = ViewPosition.createAt( viewImage, 'end' );
+
+	viewWriter.insert( viewPosition, viewCaption );
+	mapper.bindElements( modelCaption, viewCaption );
+}
+

+ 0 - 27
packages/ckeditor5-image/src/imagecaption/utils.js

@@ -9,8 +9,6 @@
 
 import ViewEditableElement from '@ckeditor/ckeditor5-engine/src/view/editableelement';
 import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
-import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
-import viewWriter from '@ckeditor/ckeditor5-engine/src/view/writer';
 
 const captionSymbol = Symbol( 'imageCaption' );
 
@@ -64,16 +62,6 @@ export function getCaptionFromImage( imageModelElement ) {
 	return null;
 }
 
-/**
- * Returns `true` if provided `node` is placed inside image's caption.
- *
- * @param {module:engine/model/node~Node} node
- * @return {Boolean}
- */
-export function isInsideCaption( node ) {
-	return !!( node.parent && node.parent.name == 'caption' && node.parent.parent && node.parent.parent.name == 'image' );
-}
-
 /**
  * {@link module:engine/view/matcher~Matcher} pattern. Checks if given element is `figcaption` element and is placed
  * inside image `figure` element.
@@ -92,18 +80,3 @@ export function matchImageCaption( element ) {
 
 	return null;
 }
-
-/**
- * Inserts `viewCaption` at the end of `viewImage` and binds it to `modelCaption`.
- *
- * @param {module:engine/view/containerelement~ContainerElement} viewCaption
- * @param {module:engine/model/element~Element} modelCaption
- * @param {module:engine/view/containerelement~ContainerElement} viewImage
- * @param {module:engine/conversion/mapper~Mapper} mapper
- */
-export function insertViewCaptionAndBind( viewCaption, modelCaption, viewImage, mapper ) {
-	const viewPosition = ViewPosition.createAt( viewImage, 'end' );
-
-	viewWriter.insert( viewPosition, viewCaption );
-	mapper.bindElements( modelCaption, viewCaption );
-}

+ 1 - 75
packages/ckeditor5-image/tests/imagecaption/utils.js

@@ -6,17 +6,13 @@
 import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
 import ViewEditableElement from '@ckeditor/ckeditor5-engine/src/view/editableelement';
 import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
-import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
 import {
 	captionElementCreator,
 	isCaption,
 	getCaptionFromImage,
-	isInsideCaption,
-	matchImageCaption,
-	insertViewCaptionAndBind
+	matchImageCaption
 } from '../../src/imagecaption/utils';
 import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
-import Mapper from '@ckeditor/ckeditor5-engine/src/conversion/mapper';
 
 describe( 'image captioning utils', () => {
 	let element, document;
@@ -76,44 +72,6 @@ describe( 'image captioning utils', () => {
 		} );
 	} );
 
-	describe( 'isInsideCaption', () => {
-		it( 'should return false if node has no parent', () => {
-			const el = new ModelElement( 'test' );
-
-			expect( isInsideCaption( el ) ).to.be.false;
-		} );
-
-		it( 'should return false if node\'s parent is not caption', () => {
-			const el = new ModelElement( 'test' );
-			new ModelElement( 'test', null, el );
-
-			expect( isInsideCaption( el ) ).to.be.false;
-		} );
-
-		it( 'should return false if parent`s parent node is not defined', () => {
-			const el = new ModelElement( 'test' );
-			new ModelElement( 'caption', null, el );
-
-			expect( isInsideCaption( el ) ).to.be.false;
-		} );
-
-		it( 'should return false if parent\'s parent node is not an image', () => {
-			const el = new ModelElement( 'test' );
-			const parent = new ModelElement( 'caption', null, el );
-			new ModelElement( 'not-image', null, parent );
-
-			expect( isInsideCaption( el ) ).to.be.false;
-		} );
-
-		it( 'should return true if node is placed inside image\'s caption', () => {
-			const el = new ModelElement( 'test' );
-			const parent = new ModelElement( 'caption', null, el );
-			new ModelElement( 'image', null, parent );
-
-			expect( isInsideCaption( el ) ).to.be.true;
-		} );
-	} );
-
 	describe( 'matchImageCaption', () => {
 		it( 'should return null for element that is not a figcaption', () => {
 			const element = new ViewElement( 'div' );
@@ -148,36 +106,4 @@ describe( 'image captioning utils', () => {
 			expect( matchImageCaption( element ) ).to.deep.equal( { name: true } );
 		} );
 	} );
-
-	describe( 'insertViewCaptionAndBind', () => {
-		let viewCaption, modelCaption, viewImage, mapper;
-
-		beforeEach( () => {
-			viewCaption = new ViewContainerElement( 'figcaption' );
-			modelCaption = new ModelElement( 'caption' );
-			viewImage = new ViewContainerElement( 'figure', { class: 'image' } );
-			mapper = new Mapper();
-		} );
-
-		it( 'should insert provided caption to provided image', () => {
-			insertViewCaptionAndBind( viewCaption, modelCaption, viewImage, mapper );
-
-			expect( viewImage.getChild( 0 ) ).to.equal( viewCaption );
-		} );
-
-		it( 'should insert provided caption at the end of provided image', () => {
-			const dummyElement = new ViewElement( 'dummy' );
-			viewImage.appendChildren( dummyElement );
-
-			insertViewCaptionAndBind( viewCaption, modelCaption, viewImage, mapper );
-
-			expect( viewImage.getChild( 1 ) ).to.equal( viewCaption );
-		} );
-
-		it( 'should bind view caption to model caption using provided mapper', () => {
-			insertViewCaptionAndBind( viewCaption, modelCaption, viewImage, mapper );
-
-			expect( mapper.toModelElement( viewCaption ) ).to.equal( modelCaption );
-		} );
-	} );
 } );