8
0

text.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/model/text
  7. */
  8. import Node from './node';
  9. // @if CK_DEBUG_ENGINE // const { convertMapToStringifiedObject } = require( '../dev-utils/utils' );
  10. /**
  11. * Model text node. Type of {@link module:engine/model/node~Node node} that contains {@link module:engine/model/text~Text#data text data}.
  12. *
  13. * **Important:** see {@link module:engine/model/node~Node} to read about restrictions using `Text` and `Node` API.
  14. *
  15. * **Note:** keep in mind that `Text` instances might indirectly got removed from model tree when model is changed.
  16. * This happens when {@link module:engine/model/writer~Writer model writer} is used to change model and the text node is merged with
  17. * another text node. Then, both text nodes are removed and a new text node is inserted into the model. Because of
  18. * this behavior, keeping references to `Text` is not recommended. Instead, consider creating
  19. * {@link module:engine/model/liveposition~LivePosition live position} placed before the text node.
  20. *
  21. * @extends module:engine/model/node~Node
  22. */
  23. export default class Text extends Node {
  24. /**
  25. * Creates a text node.
  26. *
  27. * **Note:** Constructor of this class shouldn't be used directly in the code.
  28. * Use the {@link module:engine/model/writer~Writer#createText} method instead.
  29. *
  30. * @protected
  31. * @param {String} data Node's text.
  32. * @param {Object} [attrs] Node's attributes. See {@link module:utils/tomap~toMap} for a list of accepted values.
  33. */
  34. constructor( data, attrs ) {
  35. super( attrs );
  36. /**
  37. * Text data contained in this text node.
  38. *
  39. * @protected
  40. * @type {String}
  41. */
  42. this._data = data || '';
  43. }
  44. /**
  45. * @inheritDoc
  46. */
  47. get offsetSize() {
  48. return this.data.length;
  49. }
  50. /**
  51. * Returns a text data contained in the node.
  52. *
  53. * @readonly
  54. * @type {String}
  55. */
  56. get data() {
  57. return this._data;
  58. }
  59. /**
  60. * Checks whether this object is of the given.
  61. *
  62. * text.is( '$text' ); // -> true
  63. * text.is( 'node' ); // -> true
  64. * text.is( 'model:$text' ); // -> true
  65. * text.is( 'model:node' ); // -> true
  66. *
  67. * text.is( 'view:$text' ); // -> false
  68. * text.is( 'documentSelection' ); // -> false
  69. *
  70. * {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
  71. *
  72. * **Note:** Until version 20.0.0 this method wasn't accepting `'$text'` type. The legacy `'text'` type is still
  73. * accepted for backward compatibility.
  74. *
  75. * @param {String} type Type to check.
  76. * @returns {Boolean}
  77. */
  78. is( type ) {
  79. return type === '$text' || type === 'model:$text' ||
  80. // This are legacy values kept for backward compatibility.
  81. type === 'text' || type === 'model:text' ||
  82. // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
  83. type === 'node' || type === 'model:node';
  84. }
  85. /**
  86. * Converts `Text` instance to plain object and returns it.
  87. *
  88. * @returns {Object} `Text` instance converted to plain object.
  89. */
  90. toJSON() {
  91. const json = super.toJSON();
  92. json.data = this.data;
  93. return json;
  94. }
  95. /**
  96. * Creates a copy of this text node and returns it. Created text node has same text data and attributes as original text node.
  97. *
  98. * @protected
  99. * @returns {module:engine/model/text~Text} `Text` instance created using given plain object.
  100. */
  101. _clone() {
  102. return new Text( this.data, this.getAttributes() );
  103. }
  104. /**
  105. * Creates a `Text` instance from given plain object (i.e. parsed JSON string).
  106. *
  107. * @param {Object} json Plain object to be converted to `Text`.
  108. * @returns {module:engine/model/text~Text} `Text` instance created using given plain object.
  109. */
  110. static fromJSON( json ) {
  111. return new Text( json.data, json.attributes );
  112. }
  113. // @if CK_DEBUG_ENGINE // toString() {
  114. // @if CK_DEBUG_ENGINE // return `#${ this.data }`;
  115. // @if CK_DEBUG_ENGINE // }
  116. // @if CK_DEBUG_ENGINE // logExtended() {
  117. // @if CK_DEBUG_ENGINE // console.log( `ModelText: ${ this }, attrs: ${ convertMapToStringifiedObject( this.getAttributes() ) }` );
  118. // @if CK_DEBUG_ENGINE // }
  119. // @if CK_DEBUG_ENGINE // log() {
  120. // @if CK_DEBUG_ENGINE // console.log( 'ModelText: ' + this );
  121. // @if CK_DEBUG_ENGINE // }
  122. }