8
0

text.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. * The constructor of this class shouldn't be used directly. To create new Text instances
  13. * use the {@link module:engine/view/downcastwriter~DowncastWriter#createText `DowncastWriter#createText()`}
  14. * method when working on data downcasted from the model or the
  15. * {@link module:engine/view/upcastwriter~UpcastWriter#createText `UpcastWriter#createText()`}
  16. * method when working on non-semantic views.
  17. *
  18. * @extends module:engine/view/node~Node
  19. */
  20. export default class Text extends Node {
  21. /**
  22. * Creates a tree view text node.
  23. *
  24. * @protected
  25. * @param {String} data The text's data.
  26. */
  27. constructor( data ) {
  28. super();
  29. /**
  30. * The text content.
  31. *
  32. * Setting the data fires the {@link module:engine/view/node~Node#event:change:text change event}.
  33. *
  34. * @protected
  35. * @member {String} module:engine/view/text~Text#_textData
  36. */
  37. this._textData = data;
  38. }
  39. /**
  40. * @inheritDoc
  41. */
  42. is( type ) {
  43. return type == 'text' || super.is( type );
  44. }
  45. /**
  46. * The text content.
  47. *
  48. * @readonly
  49. * @type {String}
  50. */
  51. get data() {
  52. return this._textData;
  53. }
  54. /**
  55. * This getter is required when using the addition assignment operator on protected property:
  56. *
  57. * const foo = downcastWriter.createText( 'foo' );
  58. * const bar = downcastWriter.createText( 'bar' );
  59. *
  60. * foo._data += bar.data; // executes: `foo._data = foo._data + bar.data`
  61. * console.log( foo.data ); // prints: 'foobar'
  62. *
  63. * If the protected getter didn't exist, `foo._data` will return `undefined` and result of the merge will be invalid.
  64. *
  65. * @protected
  66. * @type {String}
  67. */
  68. get _data() {
  69. return this.data;
  70. }
  71. /**
  72. * Sets data and fires the {@link module:engine/view/node~Node#event:change:text change event}.
  73. *
  74. * @protected
  75. * @fires change:text
  76. * @param {String} data New data for the text node.
  77. */
  78. set _data( data ) {
  79. this._fireChange( 'text', this );
  80. this._textData = data;
  81. }
  82. /**
  83. * Checks if this text node is similar to other text node.
  84. * Both nodes should have the same data to be considered as similar.
  85. *
  86. * @param {module:engine/view/text~Text} otherNode Node to check if it is same as this node.
  87. * @returns {Boolean}
  88. */
  89. isSimilar( otherNode ) {
  90. if ( !( otherNode instanceof Text ) ) {
  91. return false;
  92. }
  93. return this === otherNode || this.data === otherNode.data;
  94. }
  95. /**
  96. * Clones this node.
  97. *
  98. * @protected
  99. * @returns {module:engine/view/text~Text} Text node that is a clone of this node.
  100. */
  101. _clone() {
  102. return new Text( this.data );
  103. }
  104. }