text.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * @private
  32. * @member {String} module:engine/view/text~Text#_data
  33. */
  34. this._data = data;
  35. }
  36. /**
  37. * Clones this node.
  38. *
  39. * @protected
  40. * @returns {module:engine/view/text~Text} Text node that is a clone of this node.
  41. */
  42. _clone() {
  43. return new Text( this.data );
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. is( type ) {
  49. return type == 'text';
  50. }
  51. /**
  52. * The text content.
  53. *
  54. * Setting the data fires the {@link module:engine/view/node~Node#event:change:text change event}.
  55. */
  56. get data() {
  57. return this._data;
  58. }
  59. set data( data ) {
  60. this._fireChange( 'text', this );
  61. this._data = data;
  62. }
  63. /**
  64. * Checks if this text node is similar to other text node.
  65. * Both nodes should have the same data to be considered as similar.
  66. *
  67. * @param {module:engine/view/text~Text} otherNode Node to check if it is same as this node.
  68. * @returns {Boolean}
  69. */
  70. isSimilar( otherNode ) {
  71. if ( !( otherNode instanceof Text ) ) {
  72. return false;
  73. }
  74. return this === otherNode || this.data === otherNode.data;
  75. }
  76. }