emptyelement.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/emptyelement
  7. */
  8. import Element from './element';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import Node from './node';
  11. /**
  12. * Empty element class. It is used to represent elements that cannot contain any child nodes (for example `<img>` elements).
  13. *
  14. * To create a new empty element use the
  15. * {@link module:engine/view/downcastwriter~DowncastWriter#createEmptyElement `downcastWriter#createEmptyElement()`} method.
  16. *
  17. * @extends module:engine/view/element~Element
  18. */
  19. export default class EmptyElement extends Element {
  20. /**
  21. * Creates new instance of EmptyElement.
  22. *
  23. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-emptyelement-cannot-add` when third parameter is passed,
  24. * to inform that usage of EmptyElement is incorrect (adding child nodes to EmptyElement is forbidden).
  25. *
  26. * @see module:engine/view/downcastwriter~DowncastWriter#createEmptyElement
  27. * @protected
  28. * @param {module:engine/view/document~Document} document The document instance to which this element belongs.
  29. * @param {String} name Node name.
  30. * @param {Object|Iterable} [attrs] Collection of attributes.
  31. * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
  32. * A list of nodes to be inserted into created element.
  33. */
  34. constructor( document, name, attrs, children ) {
  35. super( document, name, attrs, children );
  36. /**
  37. * Returns `null` because filler is not needed for EmptyElements.
  38. *
  39. * @method #getFillerOffset
  40. * @returns {null} Always returns null.
  41. */
  42. this.getFillerOffset = getFillerOffset;
  43. }
  44. /**
  45. * Checks whether this object is of the given.
  46. *
  47. * emptyElement.is( 'emptyElement' ); // -> true
  48. * emptyElement.is( 'element' ); // -> true
  49. * emptyElement.is( 'node' ); // -> true
  50. * emptyElement.is( 'view:emptyElement' ); // -> true
  51. * emptyElement.is( 'view:element' ); // -> true
  52. * emptyElement.is( 'view:node' ); // -> true
  53. *
  54. * emptyElement.is( 'model:element' ); // -> false
  55. * emptyElement.is( 'documentFragment' ); // -> false
  56. *
  57. * Assuming that the object being checked is an empty element, you can also check its
  58. * {@link module:engine/view/emptyelement~EmptyElement#name name}:
  59. *
  60. * emptyElement.is( 'element', 'img' ); // -> true if this is a img element
  61. * emptyElement.is( 'emptyElement', 'img' ); // -> same as above
  62. * text.is( 'element', 'img' ); -> false
  63. *
  64. * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
  65. *
  66. * @param {String} type Type to check.
  67. * @param {String} [name] Element name.
  68. * @returns {Boolean}
  69. */
  70. is( type, name = null ) {
  71. if ( !name ) {
  72. return type === 'emptyElement' || type === 'view:emptyElement' ||
  73. // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
  74. type === 'element' || type === 'view:element' ||
  75. type === 'node' || type === 'view:node';
  76. } else {
  77. return name === this.name && (
  78. type === 'emptyElement' || type === 'view:emptyElement' ||
  79. type === 'element' || type === 'view:element'
  80. );
  81. }
  82. }
  83. /**
  84. * Overrides {@link module:engine/view/element~Element#_insertChild} method.
  85. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-emptyelement-cannot-add` to prevent
  86. * adding any child nodes to EmptyElement.
  87. *
  88. * @protected
  89. */
  90. _insertChild( index, nodes ) {
  91. if ( nodes && ( nodes instanceof Node || Array.from( nodes ).length > 0 ) ) {
  92. /**
  93. * Cannot add children to {@link module:engine/view/emptyelement~EmptyElement}.
  94. *
  95. * @error view-emptyelement-cannot-add
  96. */
  97. throw new CKEditorError(
  98. 'view-emptyelement-cannot-add',
  99. [ this, nodes ]
  100. );
  101. }
  102. }
  103. }
  104. // Returns `null` because block filler is not needed for EmptyElements.
  105. //
  106. // @returns {null}
  107. function getFillerOffset() {
  108. return null;
  109. }