textproxy.js 8.2 KB

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