text.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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. * Checks whether this object is of the given type.
  41. *
  42. * text.is( 'text' ); // -> true
  43. * text.is( 'node' ); // -> true
  44. * text.is( 'view:text' ); // -> true
  45. * text.is( 'view:node' ); // -> true
  46. *
  47. * text.is( 'model:text' ); // -> false
  48. * text.is( 'element' ); // -> false
  49. * text.is( 'range' ); // -> false
  50. *
  51. * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
  52. *
  53. * @param {String} type
  54. * @returns {Boolean}
  55. */
  56. is( type ) {
  57. return type == 'text' || type == 'view:text' || super.is( type );
  58. }
  59. /**
  60. * The text content.
  61. *
  62. * @readonly
  63. * @type {String}
  64. */
  65. get data() {
  66. return this._textData;
  67. }
  68. /**
  69. * This getter is required when using the addition assignment operator on protected property:
  70. *
  71. * const foo = downcastWriter.createText( 'foo' );
  72. * const bar = downcastWriter.createText( 'bar' );
  73. *
  74. * foo._data += bar.data; // executes: `foo._data = foo._data + bar.data`
  75. * console.log( foo.data ); // prints: 'foobar'
  76. *
  77. * If the protected getter didn't exist, `foo._data` will return `undefined` and result of the merge will be invalid.
  78. *
  79. * @protected
  80. * @type {String}
  81. */
  82. get _data() {
  83. return this.data;
  84. }
  85. /**
  86. * Sets data and fires the {@link module:engine/view/node~Node#event:change:text change event}.
  87. *
  88. * @protected
  89. * @fires change:text
  90. * @param {String} data New data for the text node.
  91. */
  92. set _data( data ) {
  93. this._fireChange( 'text', this );
  94. this._textData = data;
  95. }
  96. /**
  97. * Checks if this text node is similar to other text node.
  98. * Both nodes should have the same data to be considered as similar.
  99. *
  100. * @param {module:engine/view/text~Text} otherNode Node to check if it is same as this node.
  101. * @returns {Boolean}
  102. */
  103. isSimilar( otherNode ) {
  104. if ( !( otherNode instanceof Text ) ) {
  105. return false;
  106. }
  107. return this === otherNode || this.data === otherNode.data;
  108. }
  109. /**
  110. * Clones this node.
  111. *
  112. * @protected
  113. * @returns {module:engine/view/text~Text} Text node that is a clone of this node.
  114. */
  115. _clone() {
  116. return new Text( this.data );
  117. }
  118. }