textproxy.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module engine/model/textproxy
  7. */
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. // @if CK_DEBUG_ENGINE // const { convertMapToStringifiedObject } = require( '../dev-utils/utils' );
  10. /**
  11. * `TextProxy` represents a part of {@link module:engine/model/text~Text text node}.
  12. *
  13. * Since {@link module:engine/model/position~Position positions} can be placed between characters of a text node,
  14. * {@link module:engine/model/range~Range ranges} may contain only parts of text nodes. When {@link module:engine/model/range~Range#getItems
  15. * getting items}
  16. * contained in such range, we need to represent a part of that text node, since returning the whole text node would be incorrect.
  17. * `TextProxy` solves this issue.
  18. *
  19. * `TextProxy` has an API similar to {@link module:engine/model/text~Text Text} and allows to do most of the common tasks performed
  20. * on model nodes.
  21. *
  22. * **Note:** Some `TextProxy` instances may represent whole text node, not just a part of it.
  23. * See {@link module:engine/model/textproxy~TextProxy#isPartial}.
  24. *
  25. * **Note:** `TextProxy` is not an instance of {@link module:engine/model/node~Node node}. Keep this in mind when using it as a
  26. * parameter of methods.
  27. *
  28. * **Note:** `TextProxy` is a readonly interface. If you want to perform changes on model data represented by a `TextProxy`
  29. * use {@link module:engine/model/writer~Writer model writer API}.
  30. *
  31. * **Note:** `TextProxy` instances are created on the fly, basing on the current state of model. Because of this, it is
  32. * highly unrecommended to store references to `TextProxy` instances. `TextProxy` instances are not refreshed when
  33. * model changes, so they might get invalidated. Instead, consider creating {@link module:engine/model/liveposition~LivePosition live
  34. * position}.
  35. *
  36. * `TextProxy` instances are created by {@link module:engine/model/treewalker~TreeWalker model tree walker}. You should not need to create
  37. * an instance of this class by your own.
  38. */
  39. export default class TextProxy {
  40. /**
  41. * Creates a text proxy.
  42. *
  43. * @protected
  44. * @param {module:engine/model/text~Text} textNode Text node which part is represented by this text proxy.
  45. * @param {Number} offsetInText Offset in {@link module:engine/model/textproxy~TextProxy#textNode text node} from which the text proxy
  46. * starts.
  47. * @param {Number} length Text proxy length, that is how many text node's characters, starting from `offsetInText` it represents.
  48. * @constructor
  49. */
  50. constructor( textNode, offsetInText, length ) {
  51. /**
  52. * Text node which part is represented by this text proxy.
  53. *
  54. * @readonly
  55. * @member {module:engine/model/text~Text}
  56. */
  57. this.textNode = textNode;
  58. if ( offsetInText < 0 || offsetInText > textNode.offsetSize ) {
  59. /**
  60. * Given `offsetInText` value is incorrect.
  61. *
  62. * @error model-textproxy-wrong-offsetintext
  63. */
  64. throw new CKEditorError( 'model-textproxy-wrong-offsetintext: Given offsetInText value is incorrect.', this );
  65. }
  66. if ( length < 0 || offsetInText + length > textNode.offsetSize ) {
  67. /**
  68. * Given `length` value is incorrect.
  69. *
  70. * @error model-textproxy-wrong-length
  71. */
  72. throw new CKEditorError( 'model-textproxy-wrong-length: Given length value is incorrect.', this );
  73. }
  74. /**
  75. * Text data represented by this text proxy.
  76. *
  77. * @readonly
  78. * @member {String}
  79. */
  80. this.data = textNode.data.substring( offsetInText, offsetInText + length );
  81. /**
  82. * Offset in {@link module:engine/model/textproxy~TextProxy#textNode text node} from which the text proxy starts.
  83. *
  84. * @readonly
  85. * @member {Number}
  86. */
  87. this.offsetInText = offsetInText;
  88. }
  89. /**
  90. * Offset at which this text proxy starts in it's parent.
  91. *
  92. * @see module:engine/model/node~Node#startOffset
  93. * @readonly
  94. * @type {Number}
  95. */
  96. get startOffset() {
  97. return this.textNode.startOffset !== null ? this.textNode.startOffset + this.offsetInText : null;
  98. }
  99. /**
  100. * Offset size of this text proxy. Equal to the number of characters represented by the text proxy.
  101. *
  102. * @see module:engine/model/node~Node#offsetSize
  103. * @readonly
  104. * @type {Number}
  105. */
  106. get offsetSize() {
  107. return this.data.length;
  108. }
  109. /**
  110. * Offset at which this text proxy ends in it's parent.
  111. *
  112. * @see module:engine/model/node~Node#endOffset
  113. * @readonly
  114. * @type {Number}
  115. */
  116. get endOffset() {
  117. return this.startOffset !== null ? this.startOffset + this.offsetSize : null;
  118. }
  119. /**
  120. * Flag indicating whether `TextProxy` instance covers only part of the original {@link module:engine/model/text~Text text node}
  121. * (`true`) or the whole text node (`false`).
  122. *
  123. * This is `false` when text proxy starts at the very beginning of {@link module:engine/model/textproxy~TextProxy#textNode textNode}
  124. * ({@link module:engine/model/textproxy~TextProxy#offsetInText offsetInText} equals `0`) and text proxy sizes is equal to
  125. * text node size.
  126. *
  127. * @readonly
  128. * @type {Boolean}
  129. */
  130. get isPartial() {
  131. return this.offsetSize !== this.textNode.offsetSize;
  132. }
  133. /**
  134. * Parent of this text proxy, which is same as parent of text node represented by this text proxy.
  135. *
  136. * @readonly
  137. * @type {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment|null}
  138. */
  139. get parent() {
  140. return this.textNode.parent;
  141. }
  142. /**
  143. * Root of this text proxy, which is same as root of text node represented by this text proxy.
  144. *
  145. * @readonly
  146. * @type {module:engine/model/node~Node|module:engine/model/documentfragment~DocumentFragment}
  147. */
  148. get root() {
  149. return this.textNode.root;
  150. }
  151. /**
  152. * Checks whether this object is of the given.
  153. *
  154. * textProxy.is( 'textProxy' ); // -> true
  155. * textProxy.is( 'model:textProxy' ); // -> true
  156. *
  157. * textProxy.is( 'view:textProxy' ); // -> false
  158. * textProxy.is( 'range' ); // -> false
  159. *
  160. * {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
  161. *
  162. * @param {String} type
  163. * @returns {Boolean}
  164. */
  165. is( type ) {
  166. return type == 'textProxy' || type == 'model:textProxy';
  167. }
  168. /**
  169. * Gets path to this text proxy.
  170. *
  171. * @see module:engine/model/node~Node#getPath
  172. * @returns {Array.<Number>}
  173. */
  174. getPath() {
  175. const path = this.textNode.getPath();
  176. if ( path.length > 0 ) {
  177. path[ path.length - 1 ] += this.offsetInText;
  178. }
  179. return path;
  180. }
  181. /**
  182. * Returns ancestors array of this text proxy.
  183. *
  184. * @param {Object} options Options object.
  185. * @param {Boolean} [options.includeSelf=false] When set to `true` this text proxy will be also included in parent's array.
  186. * @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from text proxy parent to root element,
  187. * otherwise root element will be the first item in the array.
  188. * @returns {Array} Array with ancestors.
  189. */
  190. getAncestors( options = { includeSelf: false, parentFirst: false } ) {
  191. const ancestors = [];
  192. let parent = options.includeSelf ? this : this.parent;
  193. while ( parent ) {
  194. ancestors[ options.parentFirst ? 'push' : 'unshift' ]( parent );
  195. parent = parent.parent;
  196. }
  197. return ancestors;
  198. }
  199. /**
  200. * Checks if this text proxy has an attribute for given key.
  201. *
  202. * @param {String} key Key of attribute to check.
  203. * @returns {Boolean} `true` if attribute with given key is set on text proxy, `false` otherwise.
  204. */
  205. hasAttribute( key ) {
  206. return this.textNode.hasAttribute( key );
  207. }
  208. /**
  209. * Gets an attribute value for given key or `undefined` if that attribute is not set on text proxy.
  210. *
  211. * @param {String} key Key of attribute to look for.
  212. * @returns {*} Attribute value or `undefined`.
  213. */
  214. getAttribute( key ) {
  215. return this.textNode.getAttribute( key );
  216. }
  217. /**
  218. * Returns iterator that iterates over this node's attributes. Attributes are returned as arrays containing two
  219. * items. First one is attribute key and second is attribute value.
  220. *
  221. * This format is accepted by native `Map` object and also can be passed in `Node` constructor.
  222. *
  223. * @returns {Iterable.<*>}
  224. */
  225. getAttributes() {
  226. return this.textNode.getAttributes();
  227. }
  228. /**
  229. * Returns iterator that iterates over this node's attribute keys.
  230. *
  231. * @returns {Iterable.<String>}
  232. */
  233. getAttributeKeys() {
  234. return this.textNode.getAttributeKeys();
  235. }
  236. // @if CK_DEBUG_ENGINE // toString() {
  237. // @if CK_DEBUG_ENGINE // return `#${ this.data }`;
  238. // @if CK_DEBUG_ENGINE // }
  239. // @if CK_DEBUG_ENGINE // log() {
  240. // @if CK_DEBUG_ENGINE // console.log( 'ModelTextProxy: ' + this );
  241. // @if CK_DEBUG_ENGINE // }
  242. // @if CK_DEBUG_ENGINE // logExtended() {
  243. // @if CK_DEBUG_ENGINE // console.log( `ModelTextProxy: ${ this }, ` +
  244. // @if CK_DEBUG_ENGINE // `attrs: ${ convertMapToStringifiedObject( this.getAttributes() ) }` );
  245. // @if CK_DEBUG_ENGINE // }
  246. }