8
0

text.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 should not be used directly. To create a new text node instance
  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 {module:engine/view/document~Document} document The document instance to which this text node belongs.
  26. * @param {String} data The text's data.
  27. */
  28. constructor( document, data ) {
  29. super( document );
  30. /**
  31. * The text content.
  32. *
  33. * Setting the data fires the {@link module:engine/view/node~Node#event:change:text change event}.
  34. *
  35. * @protected
  36. * @member {String} module:engine/view/text~Text#_textData
  37. */
  38. this._textData = data;
  39. }
  40. /**
  41. * Checks whether this object is of the given type.
  42. *
  43. * text.is( '$text' ); // -> true
  44. * text.is( 'node' ); // -> true
  45. * text.is( 'view:$text' ); // -> true
  46. * text.is( 'view:node' ); // -> true
  47. *
  48. * text.is( 'model:$text' ); // -> false
  49. * text.is( 'element' ); // -> false
  50. * text.is( 'range' ); // -> false
  51. *
  52. * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
  53. *
  54. * **Note:** Until version 20.0.0 this method wasn't accepting `'$text'` type. The legacy `'text'` type is still
  55. * accepted for backward compatibility.
  56. *
  57. * @param {String} type Type to check.
  58. * @returns {Boolean}
  59. */
  60. is( type ) {
  61. return type === '$text' || type === 'view:$text' ||
  62. // This are legacy values kept for backward compatibility.
  63. type === 'text' || type === 'view:text' ||
  64. // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
  65. type === 'node' || type === 'view:node';
  66. }
  67. /**
  68. * The text content.
  69. *
  70. * @readonly
  71. * @type {String}
  72. */
  73. get data() {
  74. return this._textData;
  75. }
  76. /**
  77. * The `_data` property is controlled by a getter and a setter.
  78. *
  79. * The getter is required when using the addition assignment operator on protected property:
  80. *
  81. * const foo = downcastWriter.createText( 'foo' );
  82. * const bar = downcastWriter.createText( 'bar' );
  83. *
  84. * foo._data += bar.data; // executes: `foo._data = foo._data + bar.data`
  85. * console.log( foo.data ); // prints: 'foobar'
  86. *
  87. * If the protected getter didn't exist, `foo._data` will return `undefined` and result of the merge will be invalid.
  88. *
  89. * The setter sets data and fires the {@link module:engine/view/node~Node#event:change:text change event}.
  90. *
  91. * @protected
  92. * @type {String}
  93. */
  94. get _data() {
  95. return this.data;
  96. }
  97. set _data( data ) {
  98. this._fireChange( 'text', this );
  99. this._textData = data;
  100. }
  101. /**
  102. * Checks if this text node is similar to other text node.
  103. * Both nodes should have the same data to be considered as similar.
  104. *
  105. * @param {module:engine/view/text~Text} otherNode Node to check if it is same as this node.
  106. * @returns {Boolean}
  107. */
  108. isSimilar( otherNode ) {
  109. if ( !( otherNode instanceof Text ) ) {
  110. return false;
  111. }
  112. return this === otherNode || this.data === otherNode.data;
  113. }
  114. /**
  115. * Clones this node.
  116. *
  117. * @protected
  118. * @returns {module:engine/view/text~Text} Text node that is a clone of this node.
  119. */
  120. _clone() {
  121. return new Text( this.document, this.data );
  122. }
  123. // @if CK_DEBUG_ENGINE // toString() {
  124. // @if CK_DEBUG_ENGINE // return `#${ this.data }`;
  125. // @if CK_DEBUG_ENGINE // }
  126. // @if CK_DEBUG_ENGINE // log() {
  127. // @if CK_DEBUG_ENGINE // console.log( 'ViewText: ' + this );
  128. // @if CK_DEBUG_ENGINE // }
  129. // @if CK_DEBUG_ENGINE // logExtended() {
  130. // @if CK_DEBUG_ENGINE // console.log( 'ViewText: ' + this );
  131. // @if CK_DEBUG_ENGINE // }
  132. }