8
0
فهرست منبع

Src and alt attributes are mandatory.

Szymon Kupś 9 سال پیش
والد
کامیت
3c52e2cc8a
2فایلهای تغییر یافته به همراه23 افزوده شده و 51 حذف شده
  1. 16 18
      packages/ckeditor5-image/src/converters.js
  2. 7 33
      packages/ckeditor5-image/tests/imageengine.js

+ 16 - 18
packages/ckeditor5-image/src/converters.js

@@ -87,6 +87,16 @@ export function viewToModelImage() {
 			return;
 		}
 
+		// Check if 'src' attribute can be converted.
+		if ( !viewImg.hasAttribute( 'src' ) || !consumable.consume( viewImg, { attributes: [ 'src' ] } ) ) {
+			return;
+		}
+
+		// Check if 'alt' attribute can be converted.
+		if ( !viewImg.hasAttribute( 'alt' ) || !consumable.consume( viewImg, { attributes: [ 'alt' ] } ) ) {
+			return;
+		}
+
 		// Consume img element.
 		if ( !consumable.consume( viewImg, { name: true } ) ) {
 			return;
@@ -95,15 +105,8 @@ export function viewToModelImage() {
 		// Create model element.
 		const modelImage = new ModelElement( 'image' );
 
-		// Add src if one is present.
-		if ( viewImg.hasAttribute( 'src' ) && consumable.consume( viewImg, { attributes: [ 'src' ] } ) ) {
-			modelImage.setAttribute( 'src', viewImg.getAttribute( 'src' ) );
-		}
-
-		// Add alt if one is present.
-		if ( viewImg.hasAttribute( 'alt' ) && consumable.consume( viewImg, { attributes: [ 'alt' ] } ) ) {
-			modelImage.setAttribute( 'alt', viewImg.getAttribute( 'alt' ) );
-		}
+		modelImage.setAttribute( 'src', viewImg.getAttribute( 'src' ) );
+		modelImage.setAttribute( 'alt', viewImg.getAttribute( 'alt' ) );
 
 		data.output = modelImage;
 	};
@@ -126,15 +129,10 @@ export function viewToModelImage() {
 export function modelToViewImage( isDataPipeline = false ) {
 	return ( data ) => {
 		const modelElement = data.item;
-		const viewImg = new ViewEmptyElement( 'img' );
-
-		if ( modelElement.hasAttribute( 'src' ) ) {
-			viewImg.setAttribute( 'src', modelElement.getAttribute( 'src' ) );
-		}
-
-		if ( modelElement.hasAttribute( 'alt' ) ) {
-			viewImg.setAttribute( 'alt', modelElement.getAttribute( 'alt' ) );
-		}
+		const viewImg = new ViewEmptyElement( 'img', {
+			src: modelElement.getAttribute( 'src' ),
+			alt: modelElement.getAttribute( 'alt' )
+		} );
 
 		return isDataPipeline ?
 			new ViewContainerElement( 'figure', { class: 'image' }, viewImg ) :

+ 7 - 33
packages/ckeditor5-image/tests/imageengine.js

@@ -40,18 +40,6 @@ describe( `ImageEngine`, () => {
 
 				expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png" alt="alt text"></figure>' );
 			} );
-
-			it( 'should convert without alt attribute', () => {
-				setModelData( document, '<image src="foo.png"></image>' );
-
-				expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
-			} );
-
-			it( 'should convert without src attribute', () => {
-				setModelData( document, '<image alt="alt text"></image>' );
-
-				expect( editor.getData() ).to.equal( '<figure class="image"><img alt="alt text"></figure>' );
-			} );
 		} );
 
 		describe( 'view to model', () => {
@@ -83,18 +71,18 @@ describe( `ImageEngine`, () => {
 					.to.equal( '' );
 			} );
 
-			it( 'should convert without alt attribute', () => {
+			it( 'should not convert without alt attribute', () => {
 				editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
 
 				expect( getModelData( document, { withoutSelection: true } ) )
-					.to.equal( '<image src="foo.png"></image>' );
+					.to.equal( '' );
 			} );
 
-			it( 'should convert without src attribute', () => {
+			it( 'should not convert without src attribute', () => {
 				editor.setData( '<figure class="image"><img alt="alt text" /></figure>' );
 
 				expect( getModelData( document, { withoutSelection: true } ) )
-					.to.equal( '<image alt="alt text"></image>' );
+					.to.equal( '' );
 			} );
 
 			it( 'should not convert in wrong context', () => {
@@ -147,20 +135,6 @@ describe( `ImageEngine`, () => {
 					.to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>' );
 			} );
 
-			it( 'should convert without alt attribute', () => {
-				setModelData( document, '<image src="foo.png"></image>' );
-
-				expect( getViewData( viewDocument, { withoutSelection: true } ) )
-					.to.equal( '<figure class="image ck-widget" contenteditable="false"><img src="foo.png"></img></figure>' );
-			} );
-
-			it( 'should convert without src attribute', () => {
-				setModelData( document, '<image alt="alt text"></image>' );
-
-				expect( getViewData( viewDocument, { withoutSelection: true } ) )
-					.to.equal( '<figure class="image ck-widget" contenteditable="false"><img alt="alt text"></img></figure>' );
-			} );
-
 			it( 'should widgetize element', () => {
 				setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
 				const figure = viewDocument.getRoot().getChild( 0 );
@@ -187,12 +161,12 @@ describe( `ImageEngine`, () => {
 			expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'alt text image widget' );
 		} );
 
-		it( 'should create proper fake selection label when alt attribute is not present', () => {
-			setModelData( document, '[<image src="foo.png"></image>]' );
+		it( 'should create proper fake selection label when alt attribute is empty', () => {
+			setModelData( document, '[<image src="foo.png" alt=""></image>]' );
 
 			expect( getViewData( viewDocument ) ).to.equal(
 				'[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
-					'<img src="foo.png"></img>' +
+					'<img alt="" src="foo.png"></img>' +
 				'</figure>]'
 			);