Explorar el Código

Added small improvements to model/writer.js.

Maciej Bukowski hace 7 años
padre
commit
32aaa1c978

+ 21 - 19
packages/ckeditor5-engine/src/model/writer.js

@@ -825,6 +825,8 @@ export default class Writer {
 	 * 		// Removes all ranges.
 	 *		writer.setSelection( null );
 	 *
+	 * Throws `writer-incorrect-use` error when the writer is used outside the `change()` block.
+	 *
 	 * @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection|
 	 * module:engine/model/position~Position|module:engine/model/element~Element|
 	 * Iterable.<module:engine/model/range~Range>|module:engine/model/range~Range|null} selectable
@@ -871,27 +873,9 @@ export default class Writer {
 	}
 
 	/**
-	 * @private
-	 * @param {String} key
-	 * @param {*} value
-	 */
-	_setSelectionAttribute( key, value ) {
-		const selection = this.model.document.selection;
-
-		// Store attribute in parent element if the selection is collapsed in an empty node.
-		if ( selection.isCollapsed && selection.anchor.parent.isEmpty ) {
-			const storeKey = DocumentSelection._getStoreAttributeKey( key );
-
-			this.setAttribute( storeKey, value, selection.anchor.parent );
-		}
-
-		selection._setAttribute( key, value );
-	}
-
-	/**
 	 * Removes an attribute with given key from the selection.
 	 *
-	 * @param {String|Iterable.<String>} keyOrIterableOfKeys Key of the attribute to remove.
+	 * @param {String|Iterable.<String>} keyOrIterableOfKeys Key of the attribute to remove or an iterable of attribute keys to remove.
 	 */
 	removeSelectionAttribute( keyOrIterableOfKeys ) {
 		this._assertWriterUsedCorrectly();
@@ -908,6 +892,24 @@ export default class Writer {
 	/**
 	 * @private
 	 * @param {String} key Key of the attribute to remove.
+	 * @param {*} value Attribute value.
+	 */
+	_setSelectionAttribute( key, value ) {
+		const selection = this.model.document.selection;
+
+		// Store attribute in parent element if the selection is collapsed in an empty node.
+		if ( selection.isCollapsed && selection.anchor.parent.isEmpty ) {
+			const storeKey = DocumentSelection._getStoreAttributeKey( key );
+
+			this.setAttribute( storeKey, value, selection.anchor.parent );
+		}
+
+		selection._setAttribute( key, value );
+	}
+
+	/**
+	 * @private
+	 * @param {String} key Key of the attribute to remove.
 	 */
 	_removeSelectionAttribute( key ) {
 		const selection = this.model.document.selection;

+ 1 - 1
packages/ckeditor5-engine/src/view/writer.js

@@ -496,7 +496,7 @@ export function wrap( range, attribute, viewSelection = null ) {
 
 		// If wrapping position is equal to view selection, move view selection inside wrapping attribute element.
 		if ( viewSelection && viewSelection.isCollapsed && viewSelection.getFirstPosition().isEqual( range.start ) ) {
-			viewSelection.setTo( new Range( position ) );
+			viewSelection.setTo( position );
 		}
 
 		return new Range( position );