text.js 1.4 KB

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