emptyelement.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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( 'img' ); // -> true if this is a img element
  61. * emptyElement.is( 'emptyElement', 'img' ); // -> same as above
  62. * text.is( '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 when `name` parameter is present.
  67. * Otherwise, it acts like the `name` parameter.
  68. * @param {String} [name] Element name.
  69. * @returns {Boolean}
  70. */
  71. is( type, name = null ) {
  72. if ( !name ) {
  73. return type === 'emptyElement' || type === 'view:emptyElement' ||
  74. // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
  75. type === this.name || type === 'view:' + this.name ||
  76. type === 'element' || type === 'view:element' ||
  77. type === 'node' || type === 'view:node';
  78. } else {
  79. return name === this.name && (
  80. type === 'emptyElement' || type === 'view:emptyElement' ||
  81. type === 'element' || type === 'view:element'
  82. );
  83. }
  84. }
  85. /**
  86. * Overrides {@link module:engine/view/element~Element#_insertChild} method.
  87. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-emptyelement-cannot-add` to prevent
  88. * adding any child nodes to EmptyElement.
  89. *
  90. * @protected
  91. */
  92. _insertChild( index, nodes ) {
  93. if ( nodes && ( nodes instanceof Node || Array.from( nodes ).length > 0 ) ) {
  94. /**
  95. * Cannot add children to {@link module:engine/view/emptyelement~EmptyElement}.
  96. *
  97. * @error view-emptyelement-cannot-add
  98. */
  99. throw new CKEditorError(
  100. 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.',
  101. [ this, nodes ]
  102. );
  103. }
  104. }
  105. }
  106. // Returns `null` because block filler is not needed for EmptyElements.
  107. //
  108. // @returns {null}
  109. function getFillerOffset() {
  110. return null;
  111. }