|
@@ -5,11 +5,11 @@
|
|
|
|
|
|
|
|
'use strict';
|
|
'use strict';
|
|
|
|
|
|
|
|
-CKEDITOR.define( [ 'document/operation', 'ckeditorerror', 'utils' ], function( Operation, CKEditorError, utils ) {
|
|
|
|
|
|
|
+CKEDITOR.define( [ 'document/operation', 'ckeditorerror' ], function( Operation, CKEditorError ) {
|
|
|
/**
|
|
/**
|
|
|
* Operation to change nodes attribute. Using this class you can add, remove or change value of the attribute.
|
|
* Operation to change nodes attribute. Using this class you can add, remove or change value of the attribute.
|
|
|
*
|
|
*
|
|
|
- * @class document.Operation
|
|
|
|
|
|
|
+ * @class document.ChangeOperation
|
|
|
*/
|
|
*/
|
|
|
class ChangeOperation extends Operation {
|
|
class ChangeOperation extends Operation {
|
|
|
/**
|
|
/**
|
|
@@ -24,22 +24,22 @@ CKEDITOR.define( [ 'document/operation', 'ckeditorerror', 'utils' ], function( O
|
|
|
* If both new and old attributes are set the operation will change the attribute value. Node that both new and
|
|
* If both new and old attributes are set the operation will change the attribute value. Node that both new and
|
|
|
* old attributes have to have the same key and the old attribute must be present in all nodes in ranges.
|
|
* old attributes have to have the same key and the old attribute must be present in all nodes in ranges.
|
|
|
*
|
|
*
|
|
|
- * @param {Array|document.Range} ranges Range or array of ranges on which operation should be applied.
|
|
|
|
|
- * @param {document.Attribute|null} oldAttr Old attribute to change. Null if operation inserts new attribute.
|
|
|
|
|
- * @param {document.Attribute|null} newAttr New attribute. Null if operation removes attribute.
|
|
|
|
|
|
|
+ * @param {document.Range} range Range on which operation should be applied.
|
|
|
|
|
+ * @param {document.Attribute|null} oldAttr Attribute to be removed. Null if operation just inserts a new attribute.
|
|
|
|
|
+ * @param {document.Attribute|null} newAttr Attribute to be added. Null if operation just removes an attribute.
|
|
|
* @param {Number} baseVersion {@link document.Document#version} on which operation can be applied.
|
|
* @param {Number} baseVersion {@link document.Document#version} on which operation can be applied.
|
|
|
* @constructor
|
|
* @constructor
|
|
|
*/
|
|
*/
|
|
|
- constructor( ranges, oldAttr, newAttr, baseVersion ) {
|
|
|
|
|
|
|
+ constructor( range, oldAttr, newAttr, baseVersion ) {
|
|
|
super( baseVersion );
|
|
super( baseVersion );
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Array of ranges on which operation should be applied.
|
|
|
|
|
|
|
+ * Range on which operation should be applied.
|
|
|
*
|
|
*
|
|
|
* @readonly
|
|
* @readonly
|
|
|
- * @type {Array}
|
|
|
|
|
|
|
+ * @type {document.Range}
|
|
|
*/
|
|
*/
|
|
|
- this.ranges = utils.isArray( ranges ) ? ranges : [ ranges ];
|
|
|
|
|
|
|
+ this.range = range;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Old attribute to change. Null if operation inserts new attribute.
|
|
* Old attribute to change. Null if operation inserts new attribute.
|
|
@@ -64,91 +64,68 @@ CKEDITOR.define( [ 'document/operation', 'ckeditorerror', 'utils' ], function( O
|
|
|
* @protected
|
|
* @protected
|
|
|
*/
|
|
*/
|
|
|
_execute() {
|
|
_execute() {
|
|
|
- var ranges = this.ranges;
|
|
|
|
|
var oldAttr = this.oldAttr;
|
|
var oldAttr = this.oldAttr;
|
|
|
var newAttr = this.newAttr;
|
|
var newAttr = this.newAttr;
|
|
|
|
|
+ var value;
|
|
|
|
|
|
|
|
- var value, range;
|
|
|
|
|
|
|
+ if ( oldAttr !== null && newAttr !== null && oldAttr.key != newAttr.key ) {
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Old and new attributes should have the same keys.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @error operation-change-different-keys
|
|
|
|
|
+ * @param {document.ChangeOperation} changeOperation
|
|
|
|
|
+ * @param {document.Attribute} oldAttr
|
|
|
|
|
+ * @param {document.Attribute} newAttr
|
|
|
|
|
+ */
|
|
|
|
|
+ throw new CKEditorError(
|
|
|
|
|
+ 'operation-change-different-keys: Old and new attributes should have the same keys.',
|
|
|
|
|
+ { changeOperation: this, oldAttr: oldAttr, newAttr: newAttr } );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // Remove.
|
|
|
|
|
- if ( newAttr === null ) {
|
|
|
|
|
- for ( range of ranges ) {
|
|
|
|
|
- for ( value of range ) {
|
|
|
|
|
- if ( !value.node.hasAttr( oldAttr ) ) {
|
|
|
|
|
- /**
|
|
|
|
|
- * The attribute which should be removed does not exists.
|
|
|
|
|
- *
|
|
|
|
|
- * @error operation-change-no-attr-to-remove
|
|
|
|
|
- * @param {document.ChangeOperation} changeOperation
|
|
|
|
|
- * @param {document.Node} node
|
|
|
|
|
- * @param {document.Attribute} attr
|
|
|
|
|
- */
|
|
|
|
|
- throw new CKEditorError(
|
|
|
|
|
- 'operation-change-no-attr-to-remove: The attribute which should be removed does not exists.',
|
|
|
|
|
- { changeOperation: this, node: value.node, attr: oldAttr } );
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // Remove or change.
|
|
|
|
|
+ if ( oldAttr !== null ) {
|
|
|
|
|
+ for ( value of this.range ) {
|
|
|
|
|
+ if ( !value.node.hasAttr( oldAttr ) ) {
|
|
|
|
|
+ /**
|
|
|
|
|
+ * The attribute which should be removed does not exists for given node.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @error operation-change-no-attr-to-remove
|
|
|
|
|
+ * @param {document.ChangeOperation} changeOperation
|
|
|
|
|
+ * @param {document.Node} node
|
|
|
|
|
+ * @param {document.Attribute} attr
|
|
|
|
|
+ */
|
|
|
|
|
+ throw new CKEditorError(
|
|
|
|
|
+ 'operation-change-no-attr-to-remove: The attribute which should be removed does not exists for given node.',
|
|
|
|
|
+ { changeOperation: this, node: value.node, attr: oldAttr } );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ // It looks like this condition should be in the if "above" but in that case we won't check if
|
|
|
|
|
+ // the whole range has attribute oldAttr. If we want to be super-clean, we should check it even
|
|
|
|
|
+ // if we don't apply any changes.
|
|
|
|
|
+ if ( newAttr === null ) {
|
|
|
value.node.removeAttr( oldAttr.key );
|
|
value.node.removeAttr( oldAttr.key );
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- // Insert.
|
|
|
|
|
- else if ( oldAttr === null ) {
|
|
|
|
|
- for ( range of ranges ) {
|
|
|
|
|
- for ( value of range ) {
|
|
|
|
|
- if ( value.node.hasAttr( newAttr.key ) ) {
|
|
|
|
|
- /**
|
|
|
|
|
- * The attribute with given key already exists.
|
|
|
|
|
- *
|
|
|
|
|
- * @error operation-change-attr-exists
|
|
|
|
|
- * @param {document.ChangeOperation} changeOperation
|
|
|
|
|
- * @param {document.Node} node
|
|
|
|
|
- * @param {document.Attribute} attr
|
|
|
|
|
- */
|
|
|
|
|
- throw new CKEditorError(
|
|
|
|
|
- 'operation-change-attr-exists: The attribute with given key already exists.',
|
|
|
|
|
- { changeOperation: this, node: value.node, attr: newAttr } );
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- value.node.setAttr( newAttr );
|
|
|
|
|
|
|
+ // Insert or change.
|
|
|
|
|
+ if ( newAttr !== null ) {
|
|
|
|
|
+ for ( value of this.range ) {
|
|
|
|
|
+ if ( value.node.hasAttr( newAttr.key ) ) {
|
|
|
|
|
+ /**
|
|
|
|
|
+ * The attribute with given key already exists for given node.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @error operation-change-attr-exists
|
|
|
|
|
+ * @param {document.ChangeOperation} changeOperation
|
|
|
|
|
+ * @param {document.Node} node
|
|
|
|
|
+ * @param {document.Attribute} attr
|
|
|
|
|
+ */
|
|
|
|
|
+ throw new CKEditorError(
|
|
|
|
|
+ 'operation-change-attr-exists: The attribute with given key already exists.',
|
|
|
|
|
+ { changeOperation: this, node: value.node, attr: newAttr } );
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // Change.
|
|
|
|
|
- else {
|
|
|
|
|
- if ( oldAttr.key != newAttr.key ) {
|
|
|
|
|
- /**
|
|
|
|
|
- * Old and new attributes should have the same keys.
|
|
|
|
|
- *
|
|
|
|
|
- * @error operation-change-different-keys
|
|
|
|
|
- * @param {document.ChangeOperation} changeOperation
|
|
|
|
|
- * @param {document.Attribute} oldAttr
|
|
|
|
|
- * @param {document.Attribute} newAttr
|
|
|
|
|
- */
|
|
|
|
|
- throw new CKEditorError(
|
|
|
|
|
- 'operation-change-different-keys: Old and new attributes should have the same keys.',
|
|
|
|
|
- { changeOperation: this, oldAttr: oldAttr, newAttr: newAttr } );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- for ( range of ranges ) {
|
|
|
|
|
- for ( value of range ) {
|
|
|
|
|
- if ( !value.node.hasAttr( oldAttr ) ) {
|
|
|
|
|
- /**
|
|
|
|
|
- * The attribute which should be changed does not exists.
|
|
|
|
|
- *
|
|
|
|
|
- * @error operation-change-no-attr-to-change
|
|
|
|
|
- * @param {document.ChangeOperation} changeOperation
|
|
|
|
|
- * @param {document.Node} node
|
|
|
|
|
- * @param {document.Attribute} oldAttr
|
|
|
|
|
- * @param {document.Attribute} newAttr
|
|
|
|
|
- */
|
|
|
|
|
- throw new CKEditorError(
|
|
|
|
|
- 'operation-change-no-attr-to-change: The attribute which should be changed does not exists.',
|
|
|
|
|
- { changeOperation: this, node: value.node, oldAttr: oldAttr, newAttr: newAttr } );
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- value.node.setAttr( newAttr );
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ value.node.setAttr( newAttr );
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -159,9 +136,9 @@ CKEDITOR.define( [ 'document/operation', 'ckeditorerror', 'utils' ], function( O
|
|
|
* @returns {document.ChangeOperation} Reverse operation.
|
|
* @returns {document.ChangeOperation} Reverse operation.
|
|
|
*/
|
|
*/
|
|
|
reverseOperation() {
|
|
reverseOperation() {
|
|
|
- return new ChangeOperation( this.ranges, this.newAttr, this.oldAttr, this.baseVersion + 1 );
|
|
|
|
|
|
|
+ return new ChangeOperation( this.range, this.newAttr, this.oldAttr, this.baseVersion + 1 );
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return ChangeOperation;
|
|
return ChangeOperation;
|
|
|
-} );
|
|
|
|
|
|
|
+} );
|