text.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. * @returns {String}
  53. */
  54. get data() {
  55. return this._data;
  56. }
  57. /**
  58. * @inheritDoc
  59. */
  60. is( type ) {
  61. return type == 'text';
  62. }
  63. /**
  64. * Converts `Text` instance to plain object and returns it.
  65. *
  66. * @returns {Object} `Text` instance converted to plain object.
  67. */
  68. toJSON() {
  69. const json = super.toJSON();
  70. json.data = this.data;
  71. return json;
  72. }
  73. /**
  74. * Creates a copy of this text node and returns it. Created text node has same text data and attributes as original text node.
  75. *
  76. * @protected
  77. * @returns {module:engine/model/text~Text} `Text` instance created using given plain object.
  78. */
  79. _clone() {
  80. return new Text( this.data, this.getAttributes() );
  81. }
  82. /**
  83. * Creates a `Text` instance from given plain object (i.e. parsed JSON string).
  84. *
  85. * @param {Object} json Plain object to be converted to `Text`.
  86. * @returns {module:engine/model/text~Text} `Text` instance created using given plain object.
  87. */
  88. static fromJSON( json ) {
  89. return new Text( json.data, json.attributes );
  90. }
  91. }