textproxy.js 5.0 KB

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