Преглед изворни кода

Rename utils method add unit test for it.

Mateusz Samsel пре 6 година
родитељ
комит
907cedc7c8

+ 2 - 2
packages/ckeditor5-link/src/linkediting.js

@@ -10,7 +10,7 @@
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import LinkCommand from './linkcommand';
 import LinkCommand from './linkcommand';
 import UnlinkCommand from './unlinkcommand';
 import UnlinkCommand from './unlinkcommand';
-import { createLinkElement, ensureSafeUrl, getLocalizedDecorators, getNormalizedDecorators } from './utils';
+import { createLinkElement, ensureSafeUrl, getLocalizedDecorators, normalizeDecorators } from './utils';
 import AutomaticDecorators from './utils/automaticdecorators';
 import AutomaticDecorators from './utils/automaticdecorators';
 import ManualDecorator from './utils/manualdecorator';
 import ManualDecorator from './utils/manualdecorator';
 import bindTwoStepCaretToAttribute from '@ckeditor/ckeditor5-engine/src/utils/bindtwostepcarettoattribute';
 import bindTwoStepCaretToAttribute from '@ckeditor/ckeditor5-engine/src/utils/bindtwostepcarettoattribute';
@@ -77,7 +77,7 @@ export default class LinkEditing extends Plugin {
 		editor.commands.add( 'link', new LinkCommand( editor ) );
 		editor.commands.add( 'link', new LinkCommand( editor ) );
 		editor.commands.add( 'unlink', new UnlinkCommand( editor ) );
 		editor.commands.add( 'unlink', new UnlinkCommand( editor ) );
 
 
-		const linkDecorators = getLocalizedDecorators( editor.t, getNormalizedDecorators( editor ) );
+		const linkDecorators = getLocalizedDecorators( editor.t, normalizeDecorators( editor.config.get( 'link.decorators' ) ) );
 
 
 		this._enableAutomaticDecorators( linkDecorators.filter( item => item.mode === DECORATOR_AUTOMATIC ) );
 		this._enableAutomaticDecorators( linkDecorators.filter( item => item.mode === DECORATOR_AUTOMATIC ) );
 		this._enableManualDecorators( linkDecorators.filter( item => item.mode === DECORATOR_MANUAL ) );
 		this._enableManualDecorators( linkDecorators.filter( item => item.mode === DECORATOR_MANUAL ) );

+ 2 - 3
packages/ckeditor5-link/src/utils.js

@@ -94,10 +94,9 @@ export function getLocalizedDecorators( t, decorators ) {
 /**
 /**
  * Converts Obj of decorators to Array of decorators with nice identifiers.
  * Converts Obj of decorators to Array of decorators with nice identifiers.
  *
  *
- * @param {module:core/editor/editor~Editor} editor
+ * @param {Object} decorators
  */
  */
-export function getNormalizedDecorators( editor ) {
-	const decorators = editor.config.get( 'link.decorators' );
+export function normalizeDecorators( decorators ) {
 	const retArray = [];
 	const retArray = [];
 
 
 	if ( decorators ) {
 	if ( decorators ) {

+ 59 - 1
packages/ckeditor5-link/tests/utils.js

@@ -9,7 +9,7 @@ import AttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeeleme
 import ContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
 import ContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
 import Text from '@ckeditor/ckeditor5-engine/src/view/text';
 import Text from '@ckeditor/ckeditor5-engine/src/view/text';
 
 
-import { createLinkElement, isLinkElement, ensureSafeUrl } from '../src/utils';
+import { createLinkElement, isLinkElement, ensureSafeUrl, normalizeDecorators } from '../src/utils';
 
 
 describe( 'utils', () => {
 describe( 'utils', () => {
 	describe( 'isLinkElement()', () => {
 	describe( 'isLinkElement()', () => {
@@ -157,4 +157,62 @@ describe( 'utils', () => {
 			expect( ensureSafeUrl( url ) ).to.equal( '#' );
 			expect( ensureSafeUrl( url ) ).to.equal( '#' );
 		} );
 		} );
 	} );
 	} );
+
+	describe( 'normalizeDecorators()', () => {
+		it( 'should transform an entry object to a normalized array', () => {
+			const callback = () => {};
+			const entryObject = {
+				foo: {
+					mode: 'manual',
+					label: 'Foo',
+					attributes: {
+						foo: 'foo'
+					}
+				},
+				bar: {
+					mode: 'automatic',
+					callback,
+					attributes: {
+						bar: 'bar'
+					}
+				},
+				baz: {
+					mode: 'manual',
+					label: 'Baz label',
+					attributes: {
+						target: '_blank',
+						rel: 'noopener noreferrer'
+					}
+				}
+			};
+
+			expect( normalizeDecorators( entryObject ) ).to.deep.equal( [
+				{
+					id: 'linkFoo',
+					mode: 'manual',
+					label: 'Foo',
+					attributes: {
+						foo: 'foo'
+					}
+				},
+				{
+					id: 'linkBar',
+					mode: 'automatic',
+					callback,
+					attributes: {
+						bar: 'bar'
+					}
+				},
+				{
+					id: 'linkBaz',
+					mode: 'manual',
+					label: 'Baz label',
+					attributes: {
+						target: '_blank',
+						rel: 'noopener noreferrer'
+					}
+				}
+			] );
+		} );
+	} );
 } );
 } );