textproxy.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. * **Note:** Until version 20.0.0 this method wasn't accepting `'$textProxy'` type. The legacy `'textProxt'` type is still
  163. * accepted for backward compatibility.
  164. *
  165. * @param {String} type Type to check.
  166. * @returns {Boolean}
  167. */
  168. is( type ) {
  169. return type === '$textProxy' || type === 'model:$textProxy' ||
  170. // This are legacy values kept for backward compatibility.
  171. type === 'textProxy' || type === 'model:textProxy';
  172. }
  173. /**
  174. * Gets path to this text proxy.
  175. *
  176. * @see module:engine/model/node~Node#getPath
  177. * @returns {Array.<Number>}
  178. */
  179. getPath() {
  180. const path = this.textNode.getPath();
  181. if ( path.length > 0 ) {
  182. path[ path.length - 1 ] += this.offsetInText;
  183. }
  184. return path;
  185. }
  186. /**
  187. * Returns ancestors array of this text proxy.
  188. *
  189. * @param {Object} options Options object.
  190. * @param {Boolean} [options.includeSelf=false] When set to `true` this text proxy will be also included in parent's array.
  191. * @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from text proxy parent to root element,
  192. * otherwise root element will be the first item in the array.
  193. * @returns {Array} Array with ancestors.
  194. */
  195. getAncestors( options = { includeSelf: false, parentFirst: false } ) {
  196. const ancestors = [];
  197. let parent = options.includeSelf ? this : this.parent;
  198. while ( parent ) {
  199. ancestors[ options.parentFirst ? 'push' : 'unshift' ]( parent );
  200. parent = parent.parent;
  201. }
  202. return ancestors;
  203. }
  204. /**
  205. * Checks if this text proxy has an attribute for given key.
  206. *
  207. * @param {String} key Key of attribute to check.
  208. * @returns {Boolean} `true` if attribute with given key is set on text proxy, `false` otherwise.
  209. */
  210. hasAttribute( key ) {
  211. return this.textNode.hasAttribute( key );
  212. }
  213. /**
  214. * Gets an attribute value for given key or `undefined` if that attribute is not set on text proxy.
  215. *
  216. * @param {String} key Key of attribute to look for.
  217. * @returns {*} Attribute value or `undefined`.
  218. */
  219. getAttribute( key ) {
  220. return this.textNode.getAttribute( key );
  221. }
  222. /**
  223. * Returns iterator that iterates over this node's attributes. Attributes are returned as arrays containing two
  224. * items. First one is attribute key and second is attribute value.
  225. *
  226. * This format is accepted by native `Map` object and also can be passed in `Node` constructor.
  227. *
  228. * @returns {Iterable.<*>}
  229. */
  230. getAttributes() {
  231. return this.textNode.getAttributes();
  232. }
  233. /**
  234. * Returns iterator that iterates over this node's attribute keys.
  235. *
  236. * @returns {Iterable.<String>}
  237. */
  238. getAttributeKeys() {
  239. return this.textNode.getAttributeKeys();
  240. }
  241. // @if CK_DEBUG_ENGINE // toString() {
  242. // @if CK_DEBUG_ENGINE // return `#${ this.data }`;
  243. // @if CK_DEBUG_ENGINE // }
  244. // @if CK_DEBUG_ENGINE // log() {
  245. // @if CK_DEBUG_ENGINE // console.log( 'ModelTextProxy: ' + this );
  246. // @if CK_DEBUG_ENGINE // }
  247. // @if CK_DEBUG_ENGINE // logExtended() {
  248. // @if CK_DEBUG_ENGINE // console.log( `ModelTextProxy: ${ this }, ` +
  249. // @if CK_DEBUG_ENGINE // `attrs: ${ convertMapToStringifiedObject( this.getAttributes() ) }` );
  250. // @if CK_DEBUG_ENGINE // }
  251. }