8
0

emptyelement.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @license Copyright (c) 2003-2017, 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. * Overrides {@link module:engine/view/element~Element#insertChildren} method.
  36. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-emptyelement-cannot-add` to prevent adding any child nodes
  37. * to EmptyElement.
  38. */
  39. insertChildren( index, nodes ) {
  40. if ( nodes && ( nodes instanceof Node || Array.from( nodes ).length > 0 ) ) {
  41. /**
  42. * Cannot add children to {@link module:engine/view/emptyelement~EmptyElement}.
  43. *
  44. * @error view-emptyelement-cannot-add
  45. */
  46. throw new CKEditorError( 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.' );
  47. }
  48. }
  49. }
  50. // Returns `null` because block filler is not needed for EmptyElements.
  51. //
  52. // @returns {null}
  53. function getFillerOffset() {
  54. return null;
  55. }