text.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/view/text
  7. */
  8. import Node from './node';
  9. /**
  10. * Tree view text node.
  11. *
  12. * @extends module:engine/view/node~Node
  13. */
  14. export default class Text extends Node {
  15. /**
  16. * Creates a tree view text node.
  17. *
  18. * **Note:** Constructor of this class shouldn't be used directly in the code.
  19. * Use the {@link module:engine/view/writer~Writer#createText} method instead.
  20. *
  21. * @protected
  22. * @param {String} data Text.
  23. */
  24. constructor( data ) {
  25. super();
  26. /**
  27. * The text content.
  28. *
  29. * Setting the data fires the {@link module:engine/view/node~Node#event:change:text change event}.
  30. *
  31. * @protected
  32. * @member {String} module:engine/view/text~Text#_textData
  33. */
  34. this._textData = data;
  35. }
  36. /**
  37. * @inheritDoc
  38. */
  39. is( type ) {
  40. return type == 'text' || super.is( type );
  41. }
  42. /**
  43. * The text content.
  44. *
  45. * @returns {String}
  46. */
  47. get data() {
  48. return this._textData;
  49. }
  50. /**
  51. * This getter is required when using the addition assignment operator on protected property:
  52. *
  53. * const foo = new Text( 'foo' );
  54. * const bar = new Text( 'bar' );
  55. *
  56. * foo._data += bar.data; // executes: `foo._data = foo._data + bar.data`
  57. * console.log( foo.data ); // prints: 'foobar'
  58. *
  59. * If the protected getter didn't exist, `foo._data` will return `undefined` and result of the merge will be invalid.
  60. *
  61. * @protected
  62. * @returns {String}
  63. */
  64. get _data() {
  65. return this.data;
  66. }
  67. /**
  68. * Sets data and fires the {@link module:engine/view/node~Node#event:change:text change event}.
  69. *
  70. * @protected
  71. * @fires change:text
  72. * @param {String} data New data for the text node.
  73. */
  74. set _data( data ) {
  75. this._fireChange( 'text', this );
  76. this._textData = data;
  77. }
  78. /**
  79. * Checks if this text node is similar to other text node.
  80. * Both nodes should have the same data to be considered as similar.
  81. *
  82. * @param {module:engine/view/text~Text} otherNode Node to check if it is same as this node.
  83. * @returns {Boolean}
  84. */
  85. isSimilar( otherNode ) {
  86. if ( !( otherNode instanceof Text ) ) {
  87. return false;
  88. }
  89. return this === otherNode || this.data === otherNode.data;
  90. }
  91. /**
  92. * Clones this node.
  93. *
  94. * @protected
  95. * @returns {module:engine/view/text~Text} Text node that is a clone of this node.
  96. */
  97. _clone() {
  98. return new Text( this.data );
  99. }
  100. }