8
0

text.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * @param {String} type
  55. * @returns {Boolean}
  56. */
  57. is( type ) {
  58. return type === 'text' || type === 'view:text' ||
  59. // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
  60. type === 'node' || type === 'view:node';
  61. }
  62. /**
  63. * The text content.
  64. *
  65. * @readonly
  66. * @type {String}
  67. */
  68. get data() {
  69. return this._textData;
  70. }
  71. /**
  72. * This getter is required when using the addition assignment operator on protected property:
  73. *
  74. * const foo = downcastWriter.createText( 'foo' );
  75. * const bar = downcastWriter.createText( 'bar' );
  76. *
  77. * foo._data += bar.data; // executes: `foo._data = foo._data + bar.data`
  78. * console.log( foo.data ); // prints: 'foobar'
  79. *
  80. * If the protected getter didn't exist, `foo._data` will return `undefined` and result of the merge will be invalid.
  81. *
  82. * @protected
  83. * @type {String}
  84. */
  85. get _data() {
  86. return this.data;
  87. }
  88. /**
  89. * Sets data and fires the {@link module:engine/view/node~Node#event:change:text change event}.
  90. *
  91. * @protected
  92. * @fires change:text
  93. * @param {String} data New data for the text node.
  94. */
  95. set _data( data ) {
  96. this._fireChange( 'text', this );
  97. this._textData = data;
  98. }
  99. /**
  100. * Checks if this text node is similar to other text node.
  101. * Both nodes should have the same data to be considered as similar.
  102. *
  103. * @param {module:engine/view/text~Text} otherNode Node to check if it is same as this node.
  104. * @returns {Boolean}
  105. */
  106. isSimilar( otherNode ) {
  107. if ( !( otherNode instanceof Text ) ) {
  108. return false;
  109. }
  110. return this === otherNode || this.data === otherNode.data;
  111. }
  112. /**
  113. * Clones this node.
  114. *
  115. * @protected
  116. * @returns {module:engine/view/text~Text} Text node that is a clone of this node.
  117. */
  118. _clone() {
  119. return new Text( this.document, this.data );
  120. }
  121. // @if CK_DEBUG_ENGINE // toString() {
  122. // @if CK_DEBUG_ENGINE // return `#${ this.data }`;
  123. // @if CK_DEBUG_ENGINE // }
  124. // @if CK_DEBUG_ENGINE // log() {
  125. // @if CK_DEBUG_ENGINE // console.log( 'ViewText: ' + this );
  126. // @if CK_DEBUG_ENGINE // }
  127. // @if CK_DEBUG_ENGINE // logExtended() {
  128. // @if CK_DEBUG_ENGINE // console.log( 'ViewText: ' + this );
  129. // @if CK_DEBUG_ENGINE // }
  130. }