textproxy.js 4.0 KB

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