8
0

rootelement.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/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. * @inheritDoc
  52. */
  53. is( type, name ) {
  54. if ( !name ) {
  55. return type == 'rootElement' || super.is( type );
  56. } else {
  57. return ( type == 'rootElement' && name == this.name ) || super.is( type, name );
  58. }
  59. }
  60. /**
  61. * Converts `RootElement` instance to `String` containing it's name.
  62. *
  63. * @returns {String} `RootElement` instance converted to `String`.
  64. */
  65. toJSON() {
  66. return this.rootName;
  67. }
  68. }