浏览代码

Merge remote-tracking branch 'origin/t/1' into t/2

Aleksander Nowodzinski 9 年之前
父节点
当前提交
8360d20b2d

+ 1 - 1
packages/ckeditor5-link/src/link.js

@@ -52,7 +52,7 @@ export default class Link extends Feature {
 		} );
 
 		// Bind button model to command.
-		buttonModel.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
+		buttonModel.bind( 'isOn', 'isEnabled' ).to( command, 'isValue', 'isEnabled' );
 
 		// Execute command.
 		const hrefValue = 'http://www.cksource.com'; // Temporary href value.

+ 45 - 13
packages/ckeditor5-link/src/linkcommand.js

@@ -4,6 +4,8 @@
  */
 
 import Command from '../core/command/command.js';
+import Text from '../engine/model/text.js';
+import Range from '../engine/model/range.js';
 import getSchemaValidRanges from '../core/command/helpers/getschemavalidranges.js';
 import isAttributeAllowedInSelection from '../core/command/helpers/isattributeallowedinselection.js';
 
@@ -28,10 +30,10 @@ export default class LinkCommand extends Command {
 		 * @observable
 		 * @member {Boolean} core.command.ToggleAttributeCommand#value
 		 */
-		this.set( 'value', false );
+		this.set( 'isValue', false );
 
 		this.listenTo( this.editor.document.selection, 'change:attribute', () => {
-			this.value = this.editor.document.selection.hasAttribute( 'link' );
+			this.isValue = this.editor.document.selection.hasAttribute( 'link' );
 		} );
 	}
 
@@ -44,12 +46,19 @@ export default class LinkCommand extends Command {
 	_checkEnabled() {
 		const document = this.editor.document;
 
-		return isAttributeAllowedInSelection( this.attributeKey, document.selection, document.schema );
+		return isAttributeAllowedInSelection( 'link', document.selection, document.schema );
 	}
 
 	/**
 	 * Executes the command if it is enabled.
 	 *
+	 * When selection is not-collapsed then `link` attribute will be applied to nodes inside selection, but only to
+	 * this nodes where `link` attribute is allowed (disallowed nodes will be omitted).
+	 *
+	 * When selection is collapsed then new {@link engine.model.Text Text node} with `link` attribute will be inserted
+	 * in place of caret, but only if such an element is allowed in this place. _data of inserted text will be equal
+	 * to `href` parameter. Selection will be updated to wrap just inserted text node.
+	 *
 	 * @private
 	 * @param {String} href Link destination.
 	 */
@@ -57,20 +66,43 @@ export default class LinkCommand extends Command {
 		const document = this.editor.document;
 		const selection = document.selection;
 
-		if ( selection.isCollapsed ) {
-			selection.setAttribute( 'link', href );
-		} else {
-			// If selection has non-collapsed ranges, we change attribute on nodes inside those ranges.
-			document.enqueueChanges( () => {
-				const ranges = getSchemaValidRanges( 'link', selection.getRanges(), document.schema );
+		document.enqueueChanges( () => {
+			// Keep it as one undo step.
+			const batch = document.batch();
+
+			if ( selection.isCollapsed ) {
+				const ranges = selection.getRanges();
+				const updatedRanges = [];
+
+				for ( let range of ranges ) {
+					// Get parent of current selection position.
+					const parent = range.start.parent;
 
-				// Keep it as one undo step.
-				const batch = document.batch();
+					// Insert Text node with link attribute if is allowed in parent.
+					if ( document.schema.check( { name: '$text', attributes: 'link', inside: parent.name } ) ) {
+						const node = new Text( href, { link: href } );
+
+						batch.insert( range.start, node );
+						// Create new range wrapping just created node.
+						updatedRanges.push( Range.createOn( node ) );
+					}
+				}
+
+				// Update selection.
+				// If there is no updatedRanges it means that each insertion was disallowed.
+				if ( updatedRanges.length ) {
+					selection.setRanges( updatedRanges, selection.isBackward );
+					selection.setAttribute( 'link', href );
+				}
+			} else {
+				// If selection has non-collapsed ranges, we change attribute on nodes inside those ranges
+				// omitting nodes where `link` attribute is disallowed.
+				const ranges = getSchemaValidRanges( 'link', selection.getRanges(), document.schema );
 
 				for ( let range of ranges ) {
 					batch.setAttribute( range, 'link', href );
 				}
-			} );
-		}
+			}
+		} );
 	}
 }

+ 215 - 0
packages/ckeditor5-link/tests/linkcommand.js

@@ -0,0 +1,215 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import ModelTestEditor from '/tests/core/_utils/modeltesteditor.js';
+import LinkCommand from '/ckeditor5/link/linkcommand.js';
+import { setData, getData } from '/tests/engine/_utils/model.js';
+
+describe( 'LinkCommand', () => {
+	let editor, document, command;
+
+	beforeEach( () => {
+		return ModelTestEditor.create()
+			.then( newEditor => {
+				editor = newEditor;
+				document = editor.document;
+				command = new LinkCommand( editor );
+
+				// Allow text in $root.
+				document.schema.allow( { name: '$text', inside: '$root' } );
+
+				// Allow text with link attribute in paragraph.
+				document.schema.registerItem( 'p', '$block' );
+				document.schema.allow( { name: '$text', attributes: 'link', inside: '$root' } );
+			} );
+	} );
+
+	afterEach( () => {
+		command.destroy();
+	} );
+
+	describe( 'isValue', () => {
+		describe( 'collapsed selection', () => {
+			it( 'should be equal `true` when selection is placed inside element with link attribute', () => {
+				setData( document, `<$text link="url">foo[]bar</$text>` );
+
+				expect( command.isValue ).to.true;
+			} );
+
+			it( 'should be equal `false` when selection is placed inside element without link attribute', () => {
+				setData( document, `<$text bold="true">foo[]bar</$text>` );
+
+				expect( command.isValue ).to.false;
+			} );
+		} );
+
+		describe( 'non-collapsed selection', () => {
+			it( 'should be equal `true` when selection contains only elements with link attribute', () => {
+				setData( document, 'fo[<$text link="url">ob</$text>]ar' );
+
+				expect( command.isValue ).to.true;
+			} );
+
+			it( 'should be equal `false` when selection contains not only elements with link attribute', () => {
+				setData( document, 'f[o<$text link="url">ob</$text>]ar' );
+
+				expect( command.isValue ).to.false;
+			} );
+		} );
+	} );
+
+	describe( '_doExecute', () => {
+		describe( 'non-collapsed selection', () => {
+			it( 'should set link attribute to selected text', () => {
+				setData( document, 'f[ooba]r' );
+
+				expect( command.isValue ).to.false;
+
+				command._doExecute( 'url' );
+
+				expect( getData( document ) ).to.equal( 'f[<$text link="url">ooba</$text>]r' );
+				expect( command.isValue ).to.true;
+			} );
+
+			it( 'should set link attribute to selected text when text already has attributes', () => {
+				setData( document, 'f[o<$text bold="true">oba]r</$text>' );
+
+				expect( command.isValue ).to.false;
+
+				command._doExecute( 'url' );
+
+				expect( command.isValue ).to.true;
+				expect( getData( document ) )
+					.to.equal( 'f[<$text link="url">o</$text><$text bold="true" link="url">oba</$text>]<$text bold="true">r</$text>' );
+			} );
+
+			it( 'should overwrite existing link attribute when selected text wraps text with link attribute', () => {
+				setData( document, 'f[o<$text link="some url">o</$text>ba]r' );
+
+				expect( command.isValue ).to.false;
+
+				command._doExecute( 'url' );
+
+				expect( getData( document ) ).to.equal( 'f[<$text link="url">ooba</$text>]r' );
+				expect( command.isValue ).to.true;
+			} );
+
+			it( 'should split text and overwrite attribute value when selection is inside text with link attribute', () => {
+				setData( document, 'f<$text link="some url">o[ob]a</$text>r' );
+
+				expect( command.isValue ).to.true;
+
+				command._doExecute( 'url' );
+
+				expect( getData( document ) )
+					.to.equal( 'f<$text link="some url">o</$text>[<$text link="url">ob</$text>]<$text link="some url">a</$text>r' );
+				expect( command.isValue ).to.true;
+			} );
+
+			it( 'should overwrite link attribute of selected text only, when selection starts inside text with link attribute', () => {
+				setData( document, 'f<$text link="some url">o[o</$text>ba]r' );
+
+				expect( command.isValue ).to.true;
+
+				command._doExecute( 'url' );
+
+				expect( getData( document ) ).to.equal( 'f<$text link="some url">o</$text>[<$text link="url">oba</$text>]r' );
+				expect( command.isValue ).to.true;
+			} );
+
+			it( 'should overwrite link attribute of selected text only, when selection end inside text with link attribute', () => {
+				setData( document, 'f[o<$text link="some url">ob]a</$text>r' );
+
+				expect( command.isValue ).to.false;
+
+				command._doExecute( 'url' );
+
+				expect( getData( document ) ).to.equal( 'f[<$text link="url">oob</$text>]<$text link="some url">a</$text>r' );
+				expect( command.isValue ).to.true;
+			} );
+
+			it( 'should set link attribute to selected text when text is split by $block element', () => {
+				setData( document, '<p>f[oo</p><p>ba]r</p>' );
+
+				expect( command.isValue ).to.false;
+
+				command._doExecute( 'url' );
+
+				expect( getData( document ) )
+					.to.equal( '<p>f[<$text link="url">oo</$text></p><p><$text link="url">ba</$text>]r</p>' );
+				expect( command.isValue ).to.true;
+			} );
+
+			it( 'should set link attribute only to allowed elements and omit disallowed', () => {
+				// Disallow text in img.
+				document.schema.registerItem( 'img', '$block' );
+				document.schema.disallow( { name: '$text', attributes: 'link', inside: 'img' } );
+
+				setData( document, '<p>f[oo<img></img>ba]r</p>' );
+
+				expect( command.isValue ).to.false;
+
+				command._doExecute( 'url' );
+
+				expect( getData( document ) )
+					.to.equal( '<p>f[<$text link="url">oo</$text><img></img><$text link="url">ba</$text>]r</p>' );
+				expect( command.isValue ).to.true;
+			} );
+		} );
+
+		describe( 'collapsed selection', () => {
+			it( 'should insert text with link attribute and data equal to href', () => {
+				setData( document, 'foo[]bar' );
+
+				command._doExecute( 'url' );
+
+				expect( getData( document ) ).to.equal( 'foo[<$text link="url">url</$text>]bar' );
+			} );
+
+			it( 'should not insert text with link attribute when text is not allowed in parent', () => {
+				document.schema.disallow( { name: '$text', attributes: 'link', inside: 'p' } );
+				setData( document, '<p>foo[]bar</p>' );
+
+				command._doExecute( 'url' );
+
+				expect( getData( document ) ).to.equal( '<p>foo[]bar</p>' );
+			} );
+		} );
+	} );
+
+	describe( '_checkEnabled', () => {
+		// This test doesn't tests every possible case.
+		// Method `_checkEnabled` uses `isAttributeAllowedInSelection` helper which is fully tested in his own test.
+
+		beforeEach( () => {
+			document.schema.registerItem( 'x', '$block' );
+			document.schema.disallow( { name: '$text', inside: 'x', attributes: 'link' } );
+		} );
+
+		describe( 'when selection is collapsed', () => {
+			it( 'should return true if characters with the attribute can be placed at caret position', () => {
+				setData( document, '<p>f[]oo</p>' );
+				expect( command._checkEnabled() ).to.be.true;
+			} );
+
+			it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
+				setData( document, '<x>fo[]o</x>' );
+				expect( command._checkEnabled() ).to.be.false;
+			} );
+		} );
+
+		describe( 'when selection is not collapsed', () => {
+			it( 'should return true if there is at least one node in selection that can have the attribute', () => {
+				setData( document, '<p>[foo]</p>' );
+				expect( command._checkEnabled() ).to.be.true;
+			} );
+
+			it( 'should return false if there are no nodes in selection that can have the attribute', () => {
+				setData( document, '<x>[foo]</x>' );
+				expect( command._checkEnabled() ).to.be.false;
+			} );
+		} );
+	} );
+} );

+ 1 - 2
packages/ckeditor5-link/tests/linkengine.js

@@ -40,12 +40,11 @@ describe( 'LinkEngine', () => {
 			const command = editor.commands.get( 'link' );
 
 			expect( command ).to.be.instanceOf( LinkCommand );
-			expect( command ).to.have.property( 'attributeKey', 'link' );
 		} );
 	} );
 
 	describe( 'data pipeline conversions', () => {
-		it( 'should convert `<a href="url">` to `bold="url"` attribute', () => {
+		it( 'should convert `<a href="url">` to `link="url"` attribute', () => {
 			editor.setData( '<a href="url">foo</a>bar' );
 
 			expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<$text link="url">foo</$text>bar' );