text.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /**
  10. * Model text node. Type of {@link module:engine/model/node~Node node} that contains {@link module:engine/model/text~Text#data text data}.
  11. *
  12. * **Important:** see {@link module:engine/model/node~Node} to read about restrictions using `Text` and `Node` API.
  13. *
  14. * **Note:** keep in mind that `Text` instances might indirectly got removed from model tree when model is changed.
  15. * This happens when {@link module:engine/model/writer~Writer model writer} is used to change model and the text node is merged with
  16. * another text node. Then, both text nodes are removed and a new text node is inserted into the model. Because of
  17. * this behavior, keeping references to `Text` is not recommended. Instead, consider creating
  18. * {@link module:engine/model/liveposition~LivePosition live position} placed before the text node.
  19. *
  20. * @extends module:engine/model/node~Node
  21. */
  22. export default class Text extends Node {
  23. /**
  24. * Creates a text node.
  25. *
  26. * **Note:** Constructor of this class shouldn't be used directly in the code.
  27. * Use the {@link module:engine/model/writer~Writer#createText} method instead.
  28. *
  29. * @protected
  30. * @param {String} data Node's text.
  31. * @param {Object} [attrs] Node's attributes. See {@link module:utils/tomap~toMap} for a list of accepted values.
  32. */
  33. constructor( data, attrs ) {
  34. super( attrs );
  35. /**
  36. * Text data contained in this text node.
  37. *
  38. * @protected
  39. * @type {String}
  40. */
  41. this._data = data || '';
  42. }
  43. /**
  44. * @inheritDoc
  45. */
  46. get offsetSize() {
  47. return this.data.length;
  48. }
  49. /**
  50. * Returns a text data contained in the node.
  51. *
  52. * @readonly
  53. * @type {String}
  54. */
  55. get data() {
  56. return this._data;
  57. }
  58. /**
  59. * @inheritDoc
  60. */
  61. is( type ) {
  62. return type == 'text' || super.is( type );
  63. }
  64. /**
  65. * Converts `Text` instance to plain object and returns it.
  66. *
  67. * @returns {Object} `Text` instance converted to plain object.
  68. */
  69. toJSON() {
  70. const json = super.toJSON();
  71. json.data = this.data;
  72. return json;
  73. }
  74. /**
  75. * Creates a copy of this text node and returns it. Created text node has same text data and attributes as original text node.
  76. *
  77. * @protected
  78. * @returns {module:engine/model/text~Text} `Text` instance created using given plain object.
  79. */
  80. _clone() {
  81. return new Text( this.data, this.getAttributes() );
  82. }
  83. /**
  84. * Creates a `Text` instance from given plain object (i.e. parsed JSON string).
  85. *
  86. * @param {Object} json Plain object to be converted to `Text`.
  87. * @returns {module:engine/model/text~Text} `Text` instance created using given plain object.
  88. */
  89. static fromJSON( json ) {
  90. return new Text( json.data, json.attributes );
  91. }
  92. }