8
0
Quellcode durchsuchen

Simpliyfy findLinkRange helper.

Oskar Wrobel vor 9 Jahren
Ursprung
Commit
2a7dda769c

+ 0 - 79
packages/ckeditor5-link/src/findLinkRange.js

@@ -1,79 +0,0 @@
-/**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-import Range from '../engine/model/range.js';
-import Position from '../engine/model/position.js';
-
-/**
- * Walk backward and forward from start position node by node as long as they have the same `linkHref` attribute value and return
- * {@link engine.model.Range Range} with founded link.
- *
- * @param {engine.model.Position} position Start position.
- * @param {String} value `linkHref` attribute value.
- * @returns {engine.model.Range} Link range.
- */
-export default function findLinkRange( position, value ) {
-	return new Range( _findBackwardBound( position, value ), _findForwardBound( position, value ) );
-}
-
-// Walk backward node by node as long as they have the same `linkHref` attribute value and return position just before last matched node.
-//
-// @param {engine.model.Position} position Start position.
-// @param {String} value `linkHref` attribute value.
-// @returns {engine.model.Position} Position just before last matched node.
-function _findBackwardBound( position, value ) {
-	// Get node before position. When position is inside text node then start searching from text node.
-	let node = position.textNode === null ? position.nodeBefore : position.textNode;
-
-	// When node doesn't match it means we are at the beginning of link.
-	if ( !node || node.getAttribute( 'linkHref' ) != value ) {
-		return position;
-	}
-
-	let lastNode = node;
-
-	while ( node ) {
-		// When node doesn't match it means that previous node is the last matches node and we need to return position before it.
-		if ( node.getAttribute( 'linkHref' ) != value ) {
-			return Position.createBefore( lastNode );
-		}
-
-		lastNode = node;
-		node = node.previousSibling;
-	}
-
-	// We reach the bound.
-	return Position.createBefore( lastNode );
-}
-
-// Walk forward node by node as long as they have the same `linkHref` attribute value and return position just after last matched node.
-//
-// @param {engine.model.Position} position Start position.
-// @param {String} value `linkHref` attribute value.
-// @returns {engine.model.Position} Position just after last matched node.
-function _findForwardBound( position, value ) {
-	// Get node after position. When position is inside text node then start searching from text node.
-	let node = position.textNode === null ? position.nodeAfter : position.textNode;
-
-	// When node doesn't match it means we are at the end of link.
-	if ( !node || node.getAttribute( 'linkHref' ) != value ) {
-		return position;
-	}
-
-	let lastNode = node;
-
-	while ( node ) {
-		// When node doesn't match it means that previous node is the last matches node and we need to return position after it.
-		if ( node.getAttribute( 'linkHref' ) != value ) {
-			return Position.createAfter( lastNode );
-		}
-
-		lastNode = node;
-		node = node.nextSibling;
-	}
-
-	// We reach the bound.
-	return Position.createAfter( lastNode );
-}

+ 41 - 0
packages/ckeditor5-link/src/findlinkrange.js

@@ -0,0 +1,41 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import Range from '../engine/model/range.js';
+import Position from '../engine/model/position.js';
+
+/**
+ * Walk backward and forward from start position, node by node as long as they have the same `linkHref` attribute value and return
+ * {@link engine.model.Range Range} with found link.
+ *
+ * @param {engine.model.Position} position Start position.
+ * @param {String} value `linkHref` attribute value.
+ * @returns {engine.model.Range} Link range.
+ */
+export default function findLinkRange( position, value ) {
+	return new Range( _findBound( position, value, true ), _findBound( position, value, false ) );
+}
+
+// Walk forward or backward (depends on `lookBack` flag), node by node as long as they have the same `linkHref` attribute value
+// and return position just before or after (depends on `lookBack` flag) last matched node.
+//
+// @param {engine.model.Position} position Start position.
+// @param {String} value `linkHref` attribute value.
+// @param {Boolean} lookBack Whether walk direction is forward `false` or backward `true`.
+// @returns {engine.model.Position} Position just before last matched node.
+function _findBound( position, value, lookBack ) {
+	// Get node before or after position (depends on `lookBack` flag).
+	// When position is inside text node then start searching from text node.
+	let node = position.textNode || ( lookBack ? position.nodeBefore : position.nodeAfter );
+
+	let lastNode = null;
+
+	while ( node && node.getAttribute( 'linkHref' ) == value ) {
+		lastNode = node;
+		node = lookBack ? node.previousSibling : node.nextSibling;
+	}
+
+	return lastNode ? Position.createAt( lastNode, lookBack ? 'before' : 'after' ) : position;
+}

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

@@ -8,7 +8,7 @@ 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';
-import findLinkRange from './findLinkRange.js';
+import findLinkRange from './findlinkrange.js';
 
 /**
  * The link command. It is used by the {@link Link.Link link feature}.
@@ -86,9 +86,9 @@ export default class LinkCommand extends Command {
 
 					// Create new range wrapping changed link.
 					selection.setRanges( [ linkRange ] );
-
+				}
 				// If not then insert text node with `linkHref` attribute in place of caret.
-				} else if ( document.schema.check( { name: '$text', attributes: 'linkHref', inside: parent.name } ) ) {
+				else if ( document.schema.check( { name: '$text', attributes: 'linkHref', inside: parent.name } ) ) {
 					const node = new Text( href, { linkHref: href } );
 
 					batch.insert( position, node );

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

@@ -4,7 +4,7 @@
  */
 
 import Command from '../core/command/command.js';
-import findLinkRange from './findLinkRange.js';
+import findLinkRange from './findlinkrange.js';
 
 /**
  * The unlink command. It is used by the {@link Link.Link link feature}.