rootelement.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/rootelement
  7. */
  8. import Element from './element';
  9. /**
  10. * Type of {@link module:engine/model/element~Element} that is a root of a model tree.
  11. * @extends module:engine/model/element~Element
  12. */
  13. export default class RootElement extends Element {
  14. /**
  15. * Creates root element.
  16. *
  17. * @param {module:engine/model/document~Document} doc Document that is an owner of this root.
  18. * @param {String} name Node name.
  19. * @param {String} [rootName='main'] Unique root name used to identify this root
  20. * element by {@link module:engine/model/document~Document}.
  21. */
  22. constructor( doc, name, rootName = 'main' ) {
  23. super( name );
  24. /**
  25. * Document that is an owner of this root.
  26. *
  27. * @private
  28. * @member {module:engine/model/document~Document}
  29. */
  30. this._doc = doc;
  31. /**
  32. * Unique root name used to identify this root element by {@link module:engine/model/document~Document}.
  33. *
  34. * @readonly
  35. * @member {String}
  36. */
  37. this.rootName = rootName;
  38. }
  39. /**
  40. * {@link module:engine/model/document~Document Document} that owns this root element.
  41. *
  42. * In contrary, to {@link module:engine/model/node~Node node}, root element always have a `document`.
  43. *
  44. * @readonly
  45. * @type {module:engine/model/document~Document|null}
  46. */
  47. get document() {
  48. return this._doc;
  49. }
  50. /**
  51. * Checks whether this object is of the given.
  52. *
  53. * rootElement.is( 'rootElement' ); // -> true
  54. * rootElement.is( 'element' ); // -> true
  55. * rootElement.is( 'node' ); // -> true
  56. * rootElement.is( 'model:rootElement' ); // -> true
  57. * rootElement.is( 'model:element' ); // -> true
  58. * rootElement.is( 'model:node' ); // -> true
  59. *
  60. * rootElement.is( 'view:element' ); // -> false
  61. * rootElement.is( 'documentFragment' ); // -> false
  62. *
  63. * Assuming that the object being checked is an element, you can also check its
  64. * {@link module:engine/model/element~Element#name name}:
  65. *
  66. * rootElement.is( '$root' ); // -> true if this is a $root element
  67. * rootElement.is( 'rootElement', '$root' ); // -> same as above
  68. * text.is( '$root' ); -> 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. * @param {String} [name] Element name.
  75. * @returns {Boolean}
  76. */
  77. is( type, name ) {
  78. const cutType = type.replace( 'model:', '' );
  79. if ( !name ) {
  80. return cutType == 'rootElement' || super.is( type );
  81. } else {
  82. return ( cutType == 'rootElement' && name == this.name ) || super.is( type, name );
  83. }
  84. }
  85. /**
  86. * Converts `RootElement` instance to `String` containing it's name.
  87. *
  88. * @returns {String} `RootElement` instance converted to `String`.
  89. */
  90. toJSON() {
  91. return this.rootName;
  92. }
  93. }