8
0

text.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @license Copyright (c) 2003-2017, 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. * @param {String} data Text.
  19. */
  20. constructor( data ) {
  21. super();
  22. /**
  23. * The text content.
  24. *
  25. * Setting the data fires the {@link module:engine/view/node~Node#event:change:text change event}.
  26. *
  27. * @private
  28. * @member {String} module:engine/view/text~Text#_data
  29. */
  30. this._data = data;
  31. }
  32. /**
  33. * Clones this node.
  34. *
  35. * @returns {module:engine/view/text~Text} Text node that is a clone of this node.
  36. */
  37. clone() {
  38. return new Text( this.data );
  39. }
  40. /**
  41. * The text content.
  42. *
  43. * Setting the data fires the {@link module:engine/view/node~Node#event:change:text change event}.
  44. */
  45. get data() {
  46. return this._data;
  47. }
  48. set data( data ) {
  49. this._fireChange( 'text', this );
  50. this._data = data;
  51. }
  52. /**
  53. * Checks if this text node is similar to other text node.
  54. * Both nodes should have the same data to be considered as similar.
  55. *
  56. * @param {module:engine/view/text~Text} otherNode Node to check if it is same as this node.
  57. * @returns {Boolean}
  58. */
  59. isSimilar( otherNode ) {
  60. if ( !( otherNode instanceof Text ) ) {
  61. return false;
  62. }
  63. return this === otherNode || this.data === otherNode.data;
  64. }
  65. }