Bladeren bron

Changed: model.TextProxy and view.TextProxy throws for incorrect initialization values.

Szymon Cofalik 9 jaren geleden
bovenliggende
commit
bd8caf75d0
2 gewijzigde bestanden met toevoegingen van 53 en 6 verwijderingen
  1. 23 4
      packages/ckeditor5-engine/src/model/textproxy.js
  2. 30 2
      packages/ckeditor5-engine/src/view/textproxy.js

+ 23 - 4
packages/ckeditor5-engine/src/model/textproxy.js

@@ -3,6 +3,8 @@
  * For licensing, see LICENSE.md.
  */
 
+import CKEditorError from '../../utils/ckeditorerror.js';
+
 /**
  * `TextProxy` represents a part of {@link engine.model.Text text node}.
  *
@@ -15,11 +17,12 @@
  * on model nodes.
  *
  * **Note:** Some `TextProxy` instances may represent whole text node, not just a part of it.
+ * See {@link engine.model.TextProxy#isPartial}.
  *
  * **Note:** `TextProxy` is not an instance of {@link engine.model.Node node}. Keep this in mind when using it as a
  * parameter of methods.
  *
- * **Note:** `TextProxy` is readonly interface. If you want to perform changes on model data represented by a `TextProxy`
+ * **Note:** `TextProxy` is a readonly interface. If you want to perform changes on model data represented by a `TextProxy`
  * use {@link engine.model.writer model writer API}.
  *
  * **Note:** `TextProxy` instances are created on the fly, basing on the current state of model. Because of this, it is
@@ -50,6 +53,23 @@ export default class TextProxy {
 		 */
 		this.textNode = textNode;
 
+		if ( offsetInText < 0 || offsetInText > textNode.offsetSize ) {
+			/**
+			 * Given offsetInText value is incorrect.
+			 *
+			 * @error model-textproxy-wrong-offsetintext
+			 */
+			throw new CKEditorError( 'model-textproxy-wrong-offsetintext: Given offsetInText value is incorrect.' );
+		}
+
+		if ( length < 0 || offsetInText + length > textNode.offsetSize ) {
+			/**
+			 * Given length value is incorrect.
+			 *
+			 * @error model-textproxy-wrong-length
+			 */
+			throw new CKEditorError( 'model-textproxy-wrong-length: Given length value is incorrect.' );
+		}
 		/**
 		 * Text data represented by this text proxy.
 		 *
@@ -112,7 +132,7 @@ export default class TextProxy {
 	 * @type {Boolean}
 	 */
 	get isPartial() {
-		return this.offsetInText !== 0 || this.offsetSize !== this.textNode.offsetSize;
+		return this.offsetSize !== this.textNode.offsetSize;
 	}
 
 	/**
@@ -150,8 +170,7 @@ export default class TextProxy {
 	 * Gets path to this text proxy.
 	 *
 	 * @see engine.model.Node#getPath
-	 * @readonly
-	 * @type {Array.<Number>}
+	 * @returns {Array.<Number>}
 	 */
 	getPath() {
 		const path = this.textNode.getPath();

+ 30 - 2
packages/ckeditor5-engine/src/view/textproxy.js

@@ -3,16 +3,27 @@
  * For licensing, see LICENSE.md.
  */
 
+import CKEditorError from '../../utils/ckeditorerror.js';
+
 /**
  * TextProxy is a wrapper for substring of {@link engine.view.Text}. Instance of this class is created by
  * {@link engine.view.TreeWalker} when only a part of {@link engine.view.Text} needs to be returned.
  *
+ * `TextProxy` has an API similar to {@link engine.view.Text Text} and allows to do most of the common tasks performed
+ * on view nodes.
+ *
+ * **Note:** Some `TextProxy` instances may represent whole text node, not just a part of it.
+ * See {@link engine.view.TextProxy#isPartial}.
+ *
+ * **Note:** `TextProxy` is a readonly interface.
+ *
  * **Note:** `TextProxy` instances are created on the fly basing on the current state of parent {@link engine.view.Text}.
  * Because of this it is highly unrecommended to store references to `TextProxy instances because they might get
  * invalidated due to operations on Document. Also TextProxy is not a {@link engine.view.Node} so it can not be
  * inserted as a child of {@link engine.view.Element}.
  *
- * You should never create an instance of this class by your own.
+ * `TextProxy` instances are created by {@link engine.view.TreeWalker view tree walker}. You should not need to create
+ * an instance of this class by your own.
  *
  * @memberOf engine.view
  */
@@ -35,6 +46,23 @@ export default class TextProxy {
 		 */
 		this.textNode = textNode;
 
+		if ( offsetInText < 0 || offsetInText > textNode.data.length ) {
+			/**
+			 * Given offsetInText value is incorrect.
+			 *
+			 * @error view-textproxy-wrong-offsetintext
+			 */
+			throw new CKEditorError( 'view-textproxy-wrong-offsetintext: Given offsetInText value is incorrect.' );
+		}
+
+		if ( length < 0 || offsetInText + length > textNode.data.length ) {
+			/**
+			 * Given length value is incorrect.
+			 *
+			 * @error view-textproxy-wrong-length
+			 */
+			throw new CKEditorError( 'view-textproxy-wrong-length: Given length value is incorrect.' );
+		}
 		/**
 		 * Text data represented by this text proxy.
 		 *
@@ -64,7 +92,7 @@ export default class TextProxy {
 	 * @type {Boolean}
 	 */
 	get isPartial() {
-		return this.offsetInText !== 0 || this.data.length !== this.textNode.data.length;
+		return this.data.length !== this.textNode.data.length;
 	}
 
 	/**