8
0

rooteditableelement.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @license Copyright (c) 2003-2020, 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/view/rooteditableelement
  7. */
  8. import EditableElement from './editableelement';
  9. const rootNameSymbol = Symbol( 'rootName' );
  10. /**
  11. * Class representing a single root in the data view. A root can be either {@link ~RootEditableElement#isReadOnly editable or read-only},
  12. * but in both cases it is called "an editable". Roots can contain other {@link module:engine/view/editableelement~EditableElement
  13. * editable elements} making them "nested editables".
  14. *
  15. * @extends module:engine/view/editableelement~EditableElement
  16. */
  17. export default class RootEditableElement extends EditableElement {
  18. /**
  19. * Creates root editable element.
  20. *
  21. * @param {module:engine/view/document~Document} document The document instance to which this element belongs.
  22. * @param {String} name Node name.
  23. */
  24. constructor( document, name ) {
  25. super( document, name );
  26. /**
  27. * Name of this root inside {@link module:engine/view/document~Document} that is an owner of this root. If no
  28. * other name is set, `main` name is used.
  29. *
  30. * @readonly
  31. * @member {String}
  32. */
  33. this.rootName = 'main';
  34. }
  35. /**
  36. * Checks whether this object is of the given.
  37. *
  38. * rootEditableElement.is( 'rootElement' ); // -> true
  39. * rootEditableElement.is( 'editableElement' ); // -> true
  40. * rootEditableElement.is( 'element' ); // -> true
  41. * rootEditableElement.is( 'node' ); // -> true
  42. * rootEditableElement.is( 'view:editableElement' ); // -> true
  43. * rootEditableElement.is( 'view:element' ); // -> true
  44. * rootEditableElement.is( 'view:node' ); // -> true
  45. *
  46. * rootEditableElement.is( 'model:element' ); // -> false
  47. * rootEditableElement.is( 'documentFragment' ); // -> false
  48. *
  49. * Assuming that the object being checked is a root editable element, you can also check its
  50. * {@link module:engine/view/rooteditableelement~RootEditableElement#name name}:
  51. *
  52. * rootEditableElement.is( 'div' ); // -> true if this is a div root editable element
  53. * rootEditableElement.is( 'rootElement', 'div' ); // -> same as above
  54. * text.is( 'div' ); -> false
  55. *
  56. * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
  57. *
  58. * @param {String} type Type to check when `name` parameter is present.
  59. * Otherwise, it acts like the `name` parameter.
  60. * @param {String} [name] Element name.
  61. * @returns {Boolean}
  62. */
  63. is( type, name = null ) {
  64. if ( !name ) {
  65. return type === 'rootElement' || type === 'view:rootElement' ||
  66. // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
  67. type === 'editableElement' || type === 'view:editableElement' ||
  68. type === 'containerElement' || type === 'view:containerElement' ||
  69. type === this.name || type === 'view:' + this.name ||
  70. type === 'element' || type === 'view:element' ||
  71. type === 'node' || type === 'view:node';
  72. } else {
  73. return name === this.name && (
  74. type === 'rootElement' || type === 'view:rootElement' ||
  75. // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
  76. type === 'editableElement' || type === 'view:editableElement' ||
  77. type === 'containerElement' || type === 'view:containerElement' ||
  78. type === 'element' || type === 'view:element'
  79. );
  80. }
  81. }
  82. get rootName() {
  83. return this.getCustomProperty( rootNameSymbol );
  84. }
  85. set rootName( rootName ) {
  86. this._setCustomProperty( rootNameSymbol, rootName );
  87. }
  88. /**
  89. * Overrides old element name and sets new one.
  90. * This is needed because view roots are created before they are attached to the DOM.
  91. * The name of the root element is temporary at this stage. It has to be changed when the
  92. * view root element is attached to the DOM element.
  93. *
  94. * @protected
  95. * @param {String} name The new name of element.
  96. */
  97. set _name( name ) {
  98. this.name = name;
  99. }
  100. }