text.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * @inheritDoc
  42. */
  43. is( type ) {
  44. return type == 'text';
  45. }
  46. /**
  47. * The text content.
  48. *
  49. * Setting the data fires the {@link module:engine/view/node~Node#event:change:text change event}.
  50. */
  51. get data() {
  52. return this._data;
  53. }
  54. set data( data ) {
  55. this._fireChange( 'text', this );
  56. this._data = data;
  57. }
  58. /**
  59. * Checks if this text node is similar to other text node.
  60. * Both nodes should have the same data to be considered as similar.
  61. *
  62. * @param {module:engine/view/text~Text} otherNode Node to check if it is same as this node.
  63. * @returns {Boolean}
  64. */
  65. isSimilar( otherNode ) {
  66. if ( !( otherNode instanceof Text ) ) {
  67. return false;
  68. }
  69. return this === otherNode || this.data === otherNode.data;
  70. }
  71. }