8
0

emptyelement.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. * EmptyElement class. It is used to represent elements that cannot contain any child nodes.
  13. */
  14. export default class EmptyElement extends Element {
  15. /**
  16. * Creates new instance of EmptyElement.
  17. *
  18. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-emptyelement-cannot-add` when third parameter is passed,
  19. * to inform that usage of EmptyElement is incorrect (adding child nodes to EmptyElement is forbidden).
  20. *
  21. * @param {String} name Node name.
  22. * @param {Object|Iterable} [attributes] Collection of attributes.
  23. */
  24. constructor( name, attributes, children ) {
  25. super( name, attributes, children );
  26. /**
  27. * Returns `null` because filler is not needed for EmptyElements.
  28. *
  29. * @method #getFillerOffset
  30. * @returns {null} Always returns null.
  31. */
  32. this.getFillerOffset = getFillerOffset;
  33. }
  34. /**
  35. * @inheritDoc
  36. */
  37. is( type, name = null ) {
  38. if ( !name ) {
  39. return type == 'emptyElement' || super.is( type );
  40. } else {
  41. return ( type == 'emptyElement' && name == this.name ) || super.is( type, name );
  42. }
  43. }
  44. /**
  45. * Overrides {@link module:engine/view/element~Element#insertChildren} method.
  46. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-emptyelement-cannot-add` to prevent
  47. * adding any child nodes to EmptyElement.
  48. */
  49. insertChildren( index, nodes ) {
  50. if ( nodes && ( nodes instanceof Node || Array.from( nodes ).length > 0 ) ) {
  51. /**
  52. * Cannot add children to {@link module:engine/view/emptyelement~EmptyElement}.
  53. *
  54. * @error view-emptyelement-cannot-add
  55. */
  56. throw new CKEditorError( 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.' );
  57. }
  58. }
  59. }
  60. // Returns `null` because block filler is not needed for EmptyElements.
  61. //
  62. // @returns {null}
  63. function getFillerOffset() {
  64. return null;
  65. }