textproxy.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @license Copyright (c) 2003-2019, 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/view/textproxy
  7. */
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. /**
  10. * TextProxy is a wrapper for substring of {@link module:engine/view/text~Text}. Instance of this class is created by
  11. * {@link module:engine/view/treewalker~TreeWalker} when only a part of {@link module:engine/view/text~Text} needs to be returned.
  12. *
  13. * `TextProxy` has an API similar to {@link module:engine/view/text~Text Text} and allows to do most of the common tasks performed
  14. * on view nodes.
  15. *
  16. * **Note:** Some `TextProxy` instances may represent whole text node, not just a part of it.
  17. * See {@link module:engine/view/textproxy~TextProxy#isPartial}.
  18. *
  19. * **Note:** `TextProxy` is a readonly interface.
  20. *
  21. * **Note:** `TextProxy` instances are created on the fly basing on the current state of parent {@link module:engine/view/text~Text}.
  22. * Because of this it is highly unrecommended to store references to `TextProxy instances because they might get
  23. * invalidated due to operations on Document. Also TextProxy is not a {@link module:engine/view/node~Node} so it can not be
  24. * inserted as a child of {@link module:engine/view/element~Element}.
  25. *
  26. * `TextProxy` instances are created by {@link module:engine/view/treewalker~TreeWalker view tree walker}. You should not need to create
  27. * an instance of this class by your own.
  28. */
  29. export default class TextProxy {
  30. /**
  31. * Creates a text proxy.
  32. *
  33. * @protected
  34. * @param {module:engine/view/text~Text} textNode Text node which part is represented by this text proxy.
  35. * @param {Number} offsetInText Offset in {@link module:engine/view/textproxy~TextProxy#textNode text node}
  36. * from which the text proxy starts.
  37. * @param {Number} length Text proxy length, that is how many text node's characters, starting from `offsetInText` it represents.
  38. * @constructor
  39. */
  40. constructor( textNode, offsetInText, length ) {
  41. /**
  42. * Reference to the {@link module:engine/view/text~Text} element which TextProxy is a substring.
  43. *
  44. * @readonly
  45. * @member {module:engine/view/text~Text} module:engine/view/textproxy~TextProxy#textNode
  46. */
  47. this.textNode = textNode;
  48. if ( offsetInText < 0 || offsetInText > textNode.data.length ) {
  49. /**
  50. * Given offsetInText value is incorrect.
  51. *
  52. * @error view-textproxy-wrong-offsetintext
  53. */
  54. throw new CKEditorError( 'view-textproxy-wrong-offsetintext: Given offsetInText value is incorrect.', this );
  55. }
  56. if ( length < 0 || offsetInText + length > textNode.data.length ) {
  57. /**
  58. * Given length value is incorrect.
  59. *
  60. * @error view-textproxy-wrong-length
  61. */
  62. throw new CKEditorError( 'view-textproxy-wrong-length: Given length value is incorrect.', this );
  63. }
  64. /**
  65. * Text data represented by this text proxy.
  66. *
  67. * @readonly
  68. * @member {String} module:engine/view/textproxy~TextProxy#data
  69. */
  70. this.data = textNode.data.substring( offsetInText, offsetInText + length );
  71. /**
  72. * Offset in the `textNode` where this `TextProxy` instance starts.
  73. *
  74. * @readonly
  75. * @member {Number} module:engine/view/textproxy~TextProxy#offsetInText
  76. */
  77. this.offsetInText = offsetInText;
  78. }
  79. /**
  80. * Offset size of this node.
  81. *
  82. * @readonly
  83. * @type {Number}
  84. */
  85. get offsetSize() {
  86. return this.data.length;
  87. }
  88. /**
  89. * Flag indicating whether `TextProxy` instance covers only part of the original {@link module:engine/view/text~Text text node}
  90. * (`true`) or the whole text node (`false`).
  91. *
  92. * This is `false` when text proxy starts at the very beginning of {@link module:engine/view/textproxy~TextProxy#textNode textNode}
  93. * ({@link module:engine/view/textproxy~TextProxy#offsetInText offsetInText} equals `0`) and text proxy sizes is equal to
  94. * text node size.
  95. *
  96. * @readonly
  97. * @type {Boolean}
  98. */
  99. get isPartial() {
  100. return this.data.length !== this.textNode.data.length;
  101. }
  102. /**
  103. * Parent of this text proxy, which is same as parent of text node represented by this text proxy.
  104. *
  105. * @readonly
  106. * @type {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment|null}
  107. */
  108. get parent() {
  109. return this.textNode.parent;
  110. }
  111. /**
  112. * Root of this text proxy, which is same as root of text node represented by this text proxy.
  113. *
  114. * @readonly
  115. * @type {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
  116. */
  117. get root() {
  118. return this.textNode.root;
  119. }
  120. /**
  121. * {@link module:engine/view/document~Document View document} that owns this text proxy, or `null` if the text proxy is inside
  122. * {@link module:engine/view/documentfragment~DocumentFragment document fragment}.
  123. *
  124. * @readonly
  125. * @type {module:engine/view/document~Document|null}
  126. */
  127. get document() {
  128. return this.textNode.document;
  129. }
  130. /**
  131. * Checks whether given view tree object is of given type following the convention set by
  132. * {@link module:engine/view/node~Node#is `Node#is()`}.
  133. *
  134. * @param {String} type
  135. * @returns {Boolean}
  136. */
  137. is( type ) {
  138. return type == 'textProxy' || type == 'view:textProxy';
  139. }
  140. /**
  141. * Returns ancestors array of this text proxy.
  142. *
  143. * @param {Object} options Options object.
  144. * @param {Boolean} [options.includeSelf=false] When set to `true` {#textNode} will be also included in parent's array.
  145. * @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from text proxy parent to
  146. * root element, otherwise root element will be the first item in the array.
  147. * @returns {Array} Array with ancestors.
  148. */
  149. getAncestors( options = { includeSelf: false, parentFirst: false } ) {
  150. const ancestors = [];
  151. let parent = options.includeSelf ? this.textNode : this.parent;
  152. while ( parent !== null ) {
  153. ancestors[ options.parentFirst ? 'push' : 'unshift' ]( parent );
  154. parent = parent.parent;
  155. }
  156. return ancestors;
  157. }
  158. }