textproxy.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  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. * **Note:** `TextProxy` instances are created on the fly basing on the current state of parent {@link engine.view.Text}.
  11. * Because of this it is highly unrecommended to store references to `TextProxy` instances because they might get
  12. * invalidated due to operations on the document. Also, `TextProxy` is not a {@link engine.view.Node} so it cannot be
  13. * inserted as a child of {@link engine.view.Element}.
  14. *
  15. * You should never create an instance of this class by your own.
  16. *
  17. * @memberOf engine.view
  18. */
  19. export default class TextProxy {
  20. /**
  21. * Creates a tree view text proxy.
  22. *
  23. * @param {engine.view.Text} textNode Text node which text proxy is a substring.
  24. * @param {Number} startOffset Offset from beginning of {#textNode} and first character of {#data}.
  25. * @param {Number} [length] Length of substring. If is not set then the end offset is at the end of {#textNode}.
  26. */
  27. constructor( textNode, startOffset, length ) {
  28. /**
  29. * Element that is a parent of this text proxy.
  30. *
  31. * @readonly
  32. * @member {engine.view.Element|engine.view.DocumentFragment|null} engine.view.Node#parent
  33. */
  34. this.parent = textNode.parent;
  35. /**
  36. * Reference to the {@link engine.view.Text} element which TextProxy is a substring.
  37. *
  38. * @readonly
  39. * @member {engine.view.Text} engine.view.TextProxy#textNode
  40. */
  41. this.textNode = textNode;
  42. /**
  43. * Index of the substring in the `textParent`.
  44. *
  45. * @readonly
  46. * @member {Number} engine.view.TextProxy#index
  47. */
  48. this.index = startOffset;
  49. /**
  50. * The text content.
  51. *
  52. * @readonly
  53. * @member {String} engine.view.TextProxy#data
  54. */
  55. this.data = textNode.data.substring(
  56. startOffset,
  57. startOffset + ( length || textNode.data.length - startOffset )
  58. );
  59. }
  60. /**
  61. * Gets {@link engine.view.Document} reference, from the {@link engine.view.Node#getRoot root} of
  62. * {#textNode} or returns null if the root has no reference to the {@link engine.view.Document}.
  63. *
  64. * @returns {engine.view.Document|null} View Document of the text proxy or null.
  65. */
  66. getDocument() {
  67. return this.textNode.getDocument();
  68. }
  69. /**
  70. * Gets the top parent for the {#textNode}. If there is no parent {#textNode} is the root.
  71. *
  72. * @returns {engine.view.Node}
  73. */
  74. getRoot() {
  75. return this.textNode.getRoot();
  76. }
  77. /**
  78. * Returns ancestors array of this text proxy.
  79. *
  80. * @param {Object} options Options object.
  81. * @param {Boolean} [options.includeNode=false] When set to `true` {#textNode} will be also included in parent's array.
  82. * @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from text proxy parent to
  83. * root element, otherwise root element will be the first item in the array.
  84. * @returns {Array} Array with ancestors.
  85. */
  86. getAncestors( options = { includeNode: false, parentFirst: false } ) {
  87. const ancestors = [];
  88. let parent = options.includeNode ? this.textNode : this.parent;
  89. while ( parent !== null ) {
  90. ancestors[ options.parentFirst ? 'push' : 'unshift' ]( parent );
  91. parent = parent.parent;
  92. }
  93. return ancestors;
  94. }
  95. }