emptyelement.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/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 {String} name Node name.
  29. * @param {Object|Iterable} [attributes] Collection of attributes.
  30. */
  31. constructor( name, attributes, children ) {
  32. super( name, attributes, children );
  33. /**
  34. * Returns `null` because filler is not needed for EmptyElements.
  35. *
  36. * @method #getFillerOffset
  37. * @returns {null} Always returns null.
  38. */
  39. this.getFillerOffset = getFillerOffset;
  40. }
  41. /**
  42. * Checks whether this object is of the given.
  43. *
  44. * emptyElement.is( 'emptyElement' ); // -> true
  45. * emptyElement.is( 'element' ); // -> true
  46. * emptyElement.is( 'node' ); // -> true
  47. * emptyElement.is( 'view:emptyElement' ); // -> true
  48. * emptyElement.is( 'view:element' ); // -> true
  49. * emptyElement.is( 'view:node' ); // -> true
  50. *
  51. * emptyElement.is( 'model:element' ); // -> false
  52. * emptyElement.is( 'documentFragment' ); // -> false
  53. *
  54. * Assuming that the object being checked is an empty element, you can also check its
  55. * {@link module:engine/view/emptyelement~EmptyElement#name name}:
  56. *
  57. * emptyElement.is( 'img' ); // -> true if this is a img element
  58. * emptyElement.is( 'emptyElement', 'img' ); // -> same as above
  59. * text.is( 'img' ); -> false
  60. *
  61. * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
  62. *
  63. * @param {String} type Type to check when `name` parameter is present.
  64. * Otherwise, it acts like the `name` parameter.
  65. * @param {String} [name] Element name.
  66. * @returns {Boolean}
  67. */
  68. is( type, name = null ) {
  69. const cutType = type.replace( /^view:/, '' );
  70. if ( !name ) {
  71. return cutType == 'emptyElement' || super.is( type );
  72. } else {
  73. return ( cutType == 'emptyElement' && name == this.name ) || super.is( type, name );
  74. }
  75. }
  76. /**
  77. * Overrides {@link module:engine/view/element~Element#_insertChild} method.
  78. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-emptyelement-cannot-add` to prevent
  79. * adding any child nodes to EmptyElement.
  80. *
  81. * @protected
  82. */
  83. _insertChild( index, nodes ) {
  84. if ( nodes && ( nodes instanceof Node || Array.from( nodes ).length > 0 ) ) {
  85. /**
  86. * Cannot add children to {@link module:engine/view/emptyelement~EmptyElement}.
  87. *
  88. * @error view-emptyelement-cannot-add
  89. */
  90. throw new CKEditorError(
  91. 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.',
  92. [ this, nodes ]
  93. );
  94. }
  95. }
  96. }
  97. // Returns `null` because block filler is not needed for EmptyElements.
  98. //
  99. // @returns {null}
  100. function getFillerOffset() {
  101. return null;
  102. }