Jelajahi Sumber

Refactoring.

Krzysztof Krztoń 7 tahun lalu
induk
melakukan
5692596d24

+ 19 - 6
packages/ckeditor5-paste-from-office/src/filters/image.js

@@ -7,11 +7,11 @@
  * @module paste-from-office/filters/image
  * @module paste-from-office/filters/image
  */
  */
 
 
+/* globals btoa */
+
 import ViewMatcher from '@ckeditor/ckeditor5-engine/src/view/matcher';
 import ViewMatcher from '@ckeditor/ckeditor5-engine/src/view/matcher';
 import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
 import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
 
 
-import { convertHexToBase64 } from './utils';
-
 /**
 /**
  * Replaces source attribute of all `<img>` elements representing regular
  * Replaces source attribute of all `<img>` elements representing regular
  * images (not the Word shapes) with inlined base64 image representation extracted from RTF or Blob data.
  * images (not the Word shapes) with inlined base64 image representation extracted from RTF or Blob data.
@@ -37,6 +37,19 @@ export function replaceImagesSourceWithBase64( documentFragment, rtfData ) {
 	}
 	}
 }
 }
 
 
+/**
+ * Converts given HEX string to base64 representation.
+ *
+ * @protected
+ * @param {String} hexString The HEX string to be converted.
+ * @returns {String} Base64 representation of a given HEX string.
+ */
+export function _convertHexToBase64( hexString ) {
+	return btoa( hexString.match( /\w{2}/g ).map( char => {
+		return String.fromCharCode( parseInt( char, 16 ) );
+	} ).join( '' ) );
+}
+
 // Finds all shapes (`<v:*>...</v:*>`) ids. Shapes can represent images (canvas)
 // Finds all shapes (`<v:*>...</v:*>`) ids. Shapes can represent images (canvas)
 // or Word shapes (which does not have RTF or Blob representation).
 // or Word shapes (which does not have RTF or Blob representation).
 //
 //
@@ -141,7 +154,7 @@ function findAllImageElementsWithLocalSource( documentFragment, writer ) {
 
 
 	for ( const value of range ) {
 	for ( const value of range ) {
 		if ( imageElementsMatcher.match( value.item ) ) {
 		if ( imageElementsMatcher.match( value.item ) ) {
-			if ( value.item.getAttribute( 'src' ).indexOf( 'file://' ) === 0 ) {
+			if ( value.item.getAttribute( 'src' ).startsWith( 'file://' ) ) {
 				imgs.push( value.item );
 				imgs.push( value.item );
 			}
 			}
 		}
 		}
@@ -171,9 +184,9 @@ function extractImageDataFromRtf( rtfData ) {
 		for ( const image of images ) {
 		for ( const image of images ) {
 			let imageType = false;
 			let imageType = false;
 
 
-			if ( image.indexOf( '\\pngblip' ) !== -1 ) {
+			if ( image.includes( '\\pngblip' ) ) {
 				imageType = 'image/png';
 				imageType = 'image/png';
-			} else if ( image.indexOf( '\\jpegblip' ) !== -1 ) {
+			} else if ( image.includes( '\\jpegblip' ) ) {
 				imageType = 'image/jpeg';
 				imageType = 'image/jpeg';
 			}
 			}
 
 
@@ -199,7 +212,7 @@ function replaceImagesFileSourceWithInlineRepresentation( imageElements, imagesH
 	// Assume there is an equal amount of image elements and images HEX sources so they can be matched accordingly based on existing order.
 	// Assume there is an equal amount of image elements and images HEX sources so they can be matched accordingly based on existing order.
 	if ( imageElements.length === imagesHexSources.length ) {
 	if ( imageElements.length === imagesHexSources.length ) {
 		for ( let i = 0; i < imageElements.length; i++ ) {
 		for ( let i = 0; i < imageElements.length; i++ ) {
-			const newSrc = `data:${ imagesHexSources[ i ].type };base64,${ convertHexToBase64( imagesHexSources[ i ].hex ) }`;
+			const newSrc = `data:${ imagesHexSources[ i ].type };base64,${ _convertHexToBase64( imagesHexSources[ i ].hex ) }`;
 			writer.setAttribute( 'src', newSrc, imageElements[ i ] );
 			writer.setAttribute( 'src', newSrc, imageElements[ i ] );
 		}
 		}
 	}
 	}

+ 0 - 22
packages/ckeditor5-paste-from-office/src/filters/utils.js

@@ -1,22 +0,0 @@
-/**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/**
- * @module paste-from-office/filters/utils
- */
-
-/* globals btoa */
-
-/**
- * Converts given HEX string to base64 representation.
- *
- * @param {String} hexString The HEX string to be converted.
- * @returns {String} Base64 representation of a given HEX string.
- */
-export function convertHexToBase64( hexString ) {
-	return btoa( hexString.match( /\w{2}/g ).map( char => {
-		return String.fromCharCode( parseInt( char, 16 ) );
-	} ).join( '' ) );
-}

+ 44 - 1
packages/ckeditor5-paste-from-office/tests/filters/image.js

@@ -9,7 +9,7 @@ import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml'
 import { stringify as stringifyView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 import { stringify as stringifyView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 
 
 import { parseHtml } from '../../src/filters/parse';
 import { parseHtml } from '../../src/filters/parse';
-import { replaceImagesSourceWithBase64 } from '../../src/filters/image';
+import { replaceImagesSourceWithBase64, _convertHexToBase64 } from '../../src/filters/image';
 import { browserFixtures } from '../_data/image/index';
 import { browserFixtures } from '../_data/image/index';
 
 
 describe( 'Filters', () => {
 describe( 'Filters', () => {
@@ -61,5 +61,48 @@ describe( 'Filters', () => {
 				} );
 				} );
 			} );
 			} );
 		} );
 		} );
+
+		describe( '_convertHexToBase64()', () => {
+			it( '#1', () => {
+				const hex = '48656c6c6f20576f726c6421';
+				const base64 = 'SGVsbG8gV29ybGQh';
+
+				expect( _convertHexToBase64( hex ) ).to.equal( base64 );
+			} );
+
+			it( '#2', () => {
+				const hex = '466f6f204261722042617a';
+				const base64 = 'Rm9vIEJhciBCYXo=';
+
+				expect( _convertHexToBase64( hex ) ).to.equal( base64 );
+			} );
+
+			it( '#3', () => {
+				const hex = '687474703a2f2f636b656469746f722e636f6d';
+				const base64 = 'aHR0cDovL2NrZWRpdG9yLmNvbQ==';
+
+				expect( _convertHexToBase64( hex ) ).to.equal( base64 );
+			} );
+
+			it( '#4', () => {
+				const hex = '434B456469746F72203520697320746865206265737421';
+				const base64 = 'Q0tFZGl0b3IgNSBpcyB0aGUgYmVzdCE=';
+
+				expect( _convertHexToBase64( hex ) ).to.equal( base64 );
+			} );
+
+			it( '#5', () => {
+				const hex = '496E74726F6475636564204D6564696120656D6265642C20626C6F636B20636F6E74656E7420696E207461626' +
+					'C657320616E6420696E746567726174696F6E73207769746820416E67756C617220322B20616E642052656163742E2046' +
+					'696E64206F7574206D6F726520696E2074686520434B456469746F722035207631312E312E302072656C6561736564206' +
+					'26C6F6720706F73742E';
+
+				const base64 = 'SW50cm9kdWNlZCBNZWRpYSBlbWJlZCwgYmxvY2sgY29udGVudCBpbiB0YWJsZXMgYW5kIGludGVncmF0aW9ucy' +
+					'B3aXRoIEFuZ3VsYXIgMisgYW5kIFJlYWN0LiBGaW5kIG91dCBtb3JlIGluIHRoZSBDS0VkaXRvciA1IHYxMS4xLjAgcmVsZWF' +
+					'zZWQgYmxvZyBwb3N0Lg==';
+
+				expect( _convertHexToBase64( hex ) ).to.equal( base64 );
+			} );
+		} );
 	} );
 	} );
 } );
 } );

+ 0 - 53
packages/ckeditor5-paste-from-office/tests/filters/utils.js

@@ -1,53 +0,0 @@
-/**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-import { convertHexToBase64 } from '../../src/filters/utils';
-
-describe( 'Filters', () => {
-	describe( 'utils', () => {
-		describe( 'convertHexToBase64()', () => {
-			it( '#1', () => {
-				const hex = '48656c6c6f20576f726c6421';
-				const base64 = 'SGVsbG8gV29ybGQh';
-
-				expect( convertHexToBase64( hex ) ).to.equal( base64 );
-			} );
-
-			it( '#2', () => {
-				const hex = '466f6f204261722042617a';
-				const base64 = 'Rm9vIEJhciBCYXo=';
-
-				expect( convertHexToBase64( hex ) ).to.equal( base64 );
-			} );
-
-			it( '#3', () => {
-				const hex = '687474703a2f2f636b656469746f722e636f6d';
-				const base64 = 'aHR0cDovL2NrZWRpdG9yLmNvbQ==';
-
-				expect( convertHexToBase64( hex ) ).to.equal( base64 );
-			} );
-
-			it( '#4', () => {
-				const hex = '434B456469746F72203520697320746865206265737421';
-				const base64 = 'Q0tFZGl0b3IgNSBpcyB0aGUgYmVzdCE=';
-
-				expect( convertHexToBase64( hex ) ).to.equal( base64 );
-			} );
-
-			it( '#5', () => {
-				const hex = '496E74726F6475636564204D6564696120656D6265642C20626C6F636B20636F6E74656E7420696E207461626' +
-					'C657320616E6420696E746567726174696F6E73207769746820416E67756C617220322B20616E642052656163742E2046' +
-					'696E64206F7574206D6F726520696E2074686520434B456469746F722035207631312E312E302072656C6561736564206' +
-					'26C6F6720706F73742E';
-
-				const base64 = 'SW50cm9kdWNlZCBNZWRpYSBlbWJlZCwgYmxvY2sgY29udGVudCBpbiB0YWJsZXMgYW5kIGludGVncmF0aW9ucy' +
-					'B3aXRoIEFuZ3VsYXIgMisgYW5kIFJlYWN0LiBGaW5kIG91dCBtb3JlIGluIHRoZSBDS0VkaXRvciA1IHYxMS4xLjAgcmVsZWF' +
-					'zZWQgYmxvZyBwb3N0Lg==';
-
-				expect( convertHexToBase64( hex ) ).to.equal( base64 );
-			} );
-		} );
-	} );
-} );