8
0

characterproxy.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Node from './node.js';
  7. /**
  8. * A proxy object representing one character stored in the tree data model. It looks and behaves like
  9. * normal node, but is a read-only structure. This is a representation of the data. Manipulating it won't affect
  10. * the actual nodes in tree model.
  11. *
  12. * Keep in mind that CharacterProxy is static, meaning that it won't change when tree model changes. For example, if you
  13. * have a {treeModel.Element element} `myEl` containing text `foobar` and then assign `let b = myEl.getChild( 3 )` and
  14. * then remove all nodes from `myEl`, `b` will still have character `b`, parent `myEl` and offset `3`.
  15. *
  16. * CharacterProxy is created on the fly basing on tree model. It is not an explicit node in a tree model but
  17. * rather represents it. Because of this, it is not advised to store or compare instances of CharacterProxy class.
  18. * If you want to keep live reference to a point in a text, you should use {@link core.treeModel.LivePosition}.
  19. *
  20. * You should never create an instance of this class by your own. When passing parameters to constructors,
  21. * use string literals or {@link core.treeModel.Text} instead.
  22. *
  23. * @memberOf core.treeModel
  24. * @extends core.treeModel.Node
  25. */
  26. export default class CharacterProxy extends Node {
  27. /**
  28. * Creates character node proxy.
  29. *
  30. * @param {core.treeModel.NodeListText} nodeListText Reference to a text object in a node list containing this character.
  31. * @param {Number} index Index of the character in `nodeListText`.
  32. * @protected
  33. */
  34. constructor( nodeListText, index ) {
  35. super( nodeListText._attrs );
  36. /**
  37. * Reference to a text object in a node list containing this character.
  38. *
  39. * @protected
  40. * @readonly
  41. * @member {core.treeModel.NodeListText} core.treeModel.CharacterProxy#_nodeListText
  42. */
  43. this._nodeListText = nodeListText;
  44. /**
  45. * Index of the character in `nodeListText`.
  46. *
  47. * @protected
  48. * @readonly
  49. * @member {Number} core.treeModel.CharacterProxy#_index
  50. */
  51. this._index = index;
  52. /**
  53. * Character represented by this proxy.
  54. *
  55. * @protected
  56. * @readonly
  57. * @member {String} core.treeModel.CharacterProxy#character
  58. */
  59. this.character = nodeListText.text.substr( index, 1 );
  60. /**
  61. * @inheritdoc
  62. */
  63. this.parent = this._nodeListText.parent;
  64. }
  65. }