text.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/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. * @param {String} type Type to check when `name` parameter is present.
  73. * Otherwise, it acts like the `name` parameter.
  74. * @returns {Boolean}
  75. */
  76. is( type ) {
  77. return type == 'text' || type == 'model:text' || super.is( type );
  78. }
  79. /**
  80. * Converts `Text` instance to plain object and returns it.
  81. *
  82. * @returns {Object} `Text` instance converted to plain object.
  83. */
  84. toJSON() {
  85. const json = super.toJSON();
  86. json.data = this.data;
  87. return json;
  88. }
  89. /**
  90. * Creates a copy of this text node and returns it. Created text node has same text data and attributes as original text node.
  91. *
  92. * @protected
  93. * @returns {module:engine/model/text~Text} `Text` instance created using given plain object.
  94. */
  95. _clone() {
  96. return new Text( this.data, this.getAttributes() );
  97. }
  98. /**
  99. * Creates a `Text` instance from given plain object (i.e. parsed JSON string).
  100. *
  101. * @param {Object} json Plain object to be converted to `Text`.
  102. * @returns {module:engine/model/text~Text} `Text` instance created using given plain object.
  103. */
  104. static fromJSON( json ) {
  105. return new Text( json.data, json.attributes );
  106. }
  107. // @if CK_DEBUG_ENGINE // toString() {
  108. // @if CK_DEBUG_ENGINE // return `#${ this.data }`;
  109. // @if CK_DEBUG_ENGINE // }
  110. // @if CK_DEBUG_ENGINE // logExtended() {
  111. // @if CK_DEBUG_ENGINE // console.log( `ModelText: ${ this }, attrs: ${ convertMapToStringifiedObject( this.getAttributes() ) }` );
  112. // @if CK_DEBUG_ENGINE // }
  113. // @if CK_DEBUG_ENGINE // log() {
  114. // @if CK_DEBUG_ENGINE // console.log( 'ModelText: ' + this );
  115. // @if CK_DEBUG_ENGINE // }
  116. }