Przeglądaj źródła

Merge branch 't/1' into t/2

Oskar Wrobel 9 lat temu
rodzic
commit
97255bafc6

+ 7 - 4
packages/ckeditor5-link/package.json

@@ -10,12 +10,15 @@
     "ckeditor5-dev-lint": "ckeditor/ckeditor5-dev-lint"
   },
   "engines": {
-    "node": ">=5.0.0",
+    "node": ">=6.0.0",
     "npm": ">=3.0.0"
   },
   "author": "CKSource (http://cksource.com/)",
   "license": "See LICENSE.md",
-  "homepage": "",
-  "bugs": "",
-  "repository": ""
+  "homepage": "https://ckeditor5.github.io",
+  "bugs": "https://github.com/ckeditor/ckeditor5-link/issues",
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/ckeditor/ckeditor5-link.git"
+  }
 }

+ 6 - 6
packages/ckeditor5-link/src/linkcommand.js

@@ -30,17 +30,17 @@ export default class LinkCommand extends Command {
 		 * @observable
 		 * @member {Boolean} core.command.ToggleAttributeCommand#value
 		 */
-		this.set( 'isValue', false );
+		this.set( 'hasValue', false );
 
 		this.listenTo( this.editor.document.selection, 'change:attribute', () => {
-			this.isValue = this.editor.document.selection.hasAttribute( 'link' );
+			this.hasValue = this.editor.document.selection.hasAttribute( 'link' );
 		} );
 	}
 
 	/**
 	 * Checks if {@link engine.model.Document#schema} allows to create attribute in {@link engine.model.Document#selection}
 	 *
-	 * @private
+	 * @protected
 	 * @returns {Boolean}
 	 */
 	_checkEnabled() {
@@ -50,16 +50,16 @@ export default class LinkCommand extends Command {
 	}
 
 	/**
-	 * Executes the command if it is enabled.
+	 * Executes the command.
 	 *
-	 * When selection is not-collapsed then `link` attribute will be applied to nodes inside selection, but only to
+	 * When selection is non-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
+	 * @protected
 	 * @param {String} href Link destination.
 	 */
 	_doExecute( href ) {

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

@@ -8,6 +8,7 @@ import buildModelConverter from '../engine/conversion/buildmodelconverter.js';
 import buildViewConverter from '../engine/conversion/buildviewconverter.js';
 import AttributeElement from '../engine/view/attributeelement.js';
 import LinkCommand from './linkcommand.js';
+import UnlinkCommand from './unlinkcommand.js';
 
 /**
  * The link engine feature.
@@ -42,7 +43,8 @@ export default class LinkEngine extends Feature {
 				value: viewElement.getAttribute( 'href' )
 			} ) );
 
-		// Create link command.
+		// Create linking commands.
 		editor.commands.set( 'link', new LinkCommand( editor ) );
+		editor.commands.set( 'unlink', new UnlinkCommand( editor ) );
 	}
 }

+ 90 - 0
packages/ckeditor5-link/src/unlinkcommand.js

@@ -0,0 +1,90 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import Command from '../core/command/command.js';
+import Range from '../engine/model/range.js';
+
+/**
+ * The unlink command. It is used by the {@link Link.Link link feature}.
+ *
+ * @memberOf link
+ * @extends core.command.Command
+ */
+export default class LinkCommand extends Command {
+	/**
+	 * Executes the command.
+	 *
+	 * When selection is collapsed then whole link will be unlinked. {@link engine.model.TreeWalker TreeWalker} is
+	 * looking for link bounds going forward and backward from the caret position.
+	 *
+	 * If selection is non-collapsed then only selected range will be unlinked.
+	 *
+	 * @protected
+	 */
+	_doExecute() {
+		const document = this.editor.document;
+		const selection = document.selection;
+		const ranges = selection.getRanges();
+		let rangesToUpdate = [];
+
+		document.enqueueChanges( () => {
+			// When selection is collapsed we have to find link bounds.
+			if ( selection.isCollapsed ) {
+				// Loop through each selection.
+				for ( let range of ranges ) {
+					// Get searching area.
+					const parentNodeRange = Range.createIn( range.start.parent );
+
+					// Create walker instances for going forward and backward.
+					const walker = parentNodeRange.getWalker( { startPosition: range.start } );
+					const backwardWalker = parentNodeRange.getWalker( { startPosition: range.start, direction: 'backward' } );
+
+					// Store current link attribute value.
+					const attributeValue = selection.getAttribute( 'link' );
+
+					// Search link bounds and store found range.
+					rangesToUpdate.push(
+						new Range( getLinkBound( backwardWalker, attributeValue ), getLinkBound( walker, attributeValue ) )
+					);
+				}
+			} else {
+				// When selection is non-collapsed then we are unlinking selected ranges.
+				rangesToUpdate = ranges;
+			}
+
+			// Keep it as one undo step.
+			const batch = document.batch();
+
+			// Remove attribute from each range.
+			for ( let range of rangesToUpdate ) {
+				batch.removeAttribute( range, 'link' );
+			}
+
+			// Remove attribute from selection.
+			selection.removeAttribute( 'link' );
+		} );
+	}
+}
+
+// Walk trough the range and search for link bound.
+//
+// @param {engine.model.TreeWalker} walker TreeWalker instance.
+// @param {String} linkValue Value of searching link.
+// @returns {engine.model.Position} Position of link bound.
+function getLinkBound( walker, linkValue ) {
+	let lastPosition = walker.position;
+	let current = walker.next();
+
+	while ( !current.done ) {
+		if ( current.value.item.getAttribute( 'link' ) != linkValue ) {
+			return lastPosition;
+		}
+
+		lastPosition = walker.position;
+		current = walker.next();
+	}
+
+	return lastPosition;
+}

+ 39 - 30
packages/ckeditor5-link/tests/linkcommand.js

@@ -30,18 +30,18 @@ describe( 'LinkCommand', () => {
 		command.destroy();
 	} );
 
-	describe( 'isValue', () => {
+	describe( 'hasValue', () => {
 		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;
+				expect( command.hasValue ).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;
+				expect( command.hasValue ).to.false;
 			} );
 		} );
 
@@ -49,13 +49,13 @@ describe( 'LinkCommand', () => {
 			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;
+				expect( command.hasValue ).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;
+				expect( command.hasValue ).to.false;
 			} );
 		} );
 	} );
@@ -65,81 +65,81 @@ describe( 'LinkCommand', () => {
 			it( 'should set link attribute to selected text', () => {
 				setData( document, 'f[ooba]r' );
 
-				expect( command.isValue ).to.false;
+				expect( command.hasValue ).to.false;
 
 				command._doExecute( 'url' );
 
 				expect( getData( document ) ).to.equal( 'f[<$text link="url">ooba</$text>]r' );
-				expect( command.isValue ).to.true;
+				expect( command.hasValue ).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;
+				expect( command.hasValue ).to.false;
 
 				command._doExecute( 'url' );
 
-				expect( command.isValue ).to.true;
+				expect( command.hasValue ).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' );
+				setData( document, 'f[o<$text link="other url">o</$text>ba]r' );
 
-				expect( command.isValue ).to.false;
+				expect( command.hasValue ).to.false;
 
 				command._doExecute( 'url' );
 
 				expect( getData( document ) ).to.equal( 'f[<$text link="url">ooba</$text>]r' );
-				expect( command.isValue ).to.true;
+				expect( command.hasValue ).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' );
+				setData( document, 'f<$text link="other url">o[ob]a</$text>r' );
 
-				expect( command.isValue ).to.true;
+				expect( command.hasValue ).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;
+					.to.equal( 'f<$text link="other url">o</$text>[<$text link="url">ob</$text>]<$text link="other url">a</$text>r' );
+				expect( command.hasValue ).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' );
+			it( 'should overwrite link attribute of selected text only, when selection start inside text with link attribute', () => {
+				setData( document, 'f<$text link="other url">o[o</$text>ba]r' );
 
-				expect( command.isValue ).to.true;
+				expect( command.hasValue ).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;
+				expect( getData( document ) ).to.equal( 'f<$text link="other url">o</$text>[<$text link="url">oba</$text>]r' );
+				expect( command.hasValue ).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' );
+				setData( document, 'f[o<$text link="other url">ob]a</$text>r' );
 
-				expect( command.isValue ).to.false;
+				expect( command.hasValue ).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;
+				expect( getData( document ) ).to.equal( 'f[<$text link="url">oob</$text>]<$text link="other url">a</$text>r' );
+				expect( command.hasValue ).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;
+				expect( command.hasValue ).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;
+				expect( command.hasValue ).to.true;
 			} );
 
 			it( 'should set link attribute only to allowed elements and omit disallowed', () => {
@@ -149,13 +149,13 @@ describe( 'LinkCommand', () => {
 
 				setData( document, '<p>f[oo<img></img>ba]r</p>' );
 
-				expect( command.isValue ).to.false;
+				expect( command.hasValue ).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;
+				expect( command.hasValue ).to.true;
 			} );
 		} );
 
@@ -168,7 +168,16 @@ describe( 'LinkCommand', () => {
 				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', () => {
+			it( 'should insert text with link attribute and data equal to href when selection is inside text with link attribute', () => {
+				setData( document, '<$text link="other url">foo[]bar</$text>' );
+
+				command._doExecute( 'url' );
+
+				expect( getData( document ) )
+					.to.equal( '<$text link="other url">foo</$text>[<$text link="url">url</$text>]<$text link="other url">bar</$text>' );
+			} );
+
+			it( 'should not insert text with link attribute when is not allowed in parent', () => {
 				document.schema.disallow( { name: '$text', attributes: 'link', inside: 'p' } );
 				setData( document, '<p>foo[]bar</p>' );
 

+ 9 - 0
packages/ckeditor5-link/tests/linkengine.js

@@ -5,6 +5,7 @@
 
 import LinkEngine from '/ckeditor5/link/linkengine.js';
 import LinkCommand from '/ckeditor5/link/linkcommand.js';
+import UnlinkCommand from '/ckeditor5/link/unlinkcommand.js';
 import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
 import { getData as getModelData, setData as setModelData } from '/tests/engine/_utils/model.js';
 import { getData as getViewData } from '/tests/engine/_utils/view.js';
@@ -41,6 +42,14 @@ describe( 'LinkEngine', () => {
 
 			expect( command ).to.be.instanceOf( LinkCommand );
 		} );
+
+		it( 'should register unlink command', () => {
+			expect( editor.commands.has( 'unlink' ) ).to.be.true;
+
+			const command = editor.commands.get( 'unlink' );
+
+			expect( command ).to.be.instanceOf( UnlinkCommand );
+		} );
 	} );
 
 	describe( 'data pipeline conversions', () => {

+ 148 - 0
packages/ckeditor5-link/tests/unlinkcommand.js

@@ -0,0 +1,148 @@
+/**
+ * @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 UnlinkCommand from '/ckeditor5/link/unlinkcommand.js';
+import { setData, getData } from '/tests/engine/_utils/model.js';
+
+describe( 'UnlinkCommand', () => {
+	let editor, document, command;
+
+	beforeEach( () => {
+		return ModelTestEditor.create()
+			.then( newEditor => {
+				editor = newEditor;
+				document = editor.document;
+				command = new UnlinkCommand( 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( '_doExecute', () => {
+		describe( 'non-collapsed selection', () => {
+			it( 'should remove link attribute from selected text', () => {
+				setData( document, '<$text link="url">f[ooba]r</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '<$text link="url">f</$text>[ooba]<$text link="url">r</$text>' );
+			} );
+
+			it( 'should remove link attribute from selected text and do not modified other attributes', () => {
+				setData( document, '<$text bold="true" link="url">f[ooba]r</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) )
+					.to.equal( '<$text bold="true" link="url">f</$text>[<$text bold="true">ooba</$text>]<$text bold="true" link="url">r</$text>' );
+			} );
+
+			it( 'should remove link attribute from selected text when attributes have different value', () => {
+				setData( document, '[<$text link="url">foo</$text><$text link="other url">bar</$text>]' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '[foobar]' );
+			} );
+		} );
+
+		describe( 'collapsed selection', () => {
+			it( 'should remove link attribute from selection siblings with the same attribute value', () => {
+				setData( document, '<$text link="url">foo[]bar</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( 'foo[]bar' );
+			} );
+
+			it( 'should remove link attribute from selection siblings with the same attribute value and do not ' +
+				'modified other attributes',
+			() => {
+				setData( document, '<$text bold="true" link="url">foo[]bar</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '<$text bold="true">foo[]bar</$text>' );
+			} );
+
+			it(
+				'should remove link attribute from selection siblings with the same attribute value and do nothing ' +
+				'with other value links',
+			() => {
+				setData( document, '<$text link="other url">fo</$text><$text link="url">o[]b</$text><$text link="other url">ar</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '<$text link="other url">fo</$text>o[]b<$text link="other url">ar</$text>' );
+			} );
+
+			it( 'should do nothing with the same value links when there is a link with other value between', () => {
+					setData(
+						document,
+						'<$text link="same url">f</$text>' +
+						'<$text link="other url">o</$text>' +
+						'<$text link="same url">o[]b</$text>' +
+						'<$text link="other url">a</$text>' +
+						'<$text link="same url">r</$text>'
+					);
+
+					command._doExecute();
+
+					expect( getData( document ) )
+						.to.equal(
+							'<$text link="same url">f</$text>' +
+							'<$text link="other url">o</$text>' +
+							'o[]b' +
+							'<$text link="other url">a</$text>' +
+							'<$text link="same url">r</$text>'
+						);
+				} );
+
+			it( 'should remove link attribute from selection siblings only in the same parent as selection parent', () => {
+				setData( document, '<p><$text link="url">fo[]o</$text></p><p><$text link="url">bar</$text></p>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '<p>fo[]o</p><p><$text link="url">bar</$text></p>' );
+			} );
+
+			it( 'should remove link attribute from selection siblings when selection is at the end of link', () => {
+				setData( document, '<$text link="url">foobar</$text>[]' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( 'foobar[]' );
+			} );
+
+			it( 'should remove link attribute from selection siblings when selection is at the beginning of link', () => {
+				setData( document, '[]<$text link="url">foobar</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '[]foobar' );
+			} );
+
+			it( 'should remove link attribute from selection siblings on the left side when selection is between two ' +
+				'elements with different link attributes',
+			() => {
+				setData( document, '<$text link="url">foo</$text>[]<$text link="other url">bar</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( 'foo[]<$text link="other url">bar</$text>' );
+			} );
+		} );
+	} );
+} );