emptyelement.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * @see module:engine/view/writer~Writer#createEmptyElement
  22. * @protected
  23. * @param {String} name Node name.
  24. * @param {Object|Iterable} [attributes] Collection of attributes.
  25. */
  26. constructor( name, attributes, children ) {
  27. super( name, attributes, children );
  28. /**
  29. * Returns `null` because filler is not needed for EmptyElements.
  30. *
  31. * @method #getFillerOffset
  32. * @returns {null} Always returns null.
  33. */
  34. this.getFillerOffset = getFillerOffset;
  35. }
  36. /**
  37. * @inheritDoc
  38. */
  39. is( type, name = null ) {
  40. if ( !name ) {
  41. return type == 'emptyElement' || super.is( type );
  42. } else {
  43. return ( type == 'emptyElement' && name == this.name ) || super.is( type, name );
  44. }
  45. }
  46. /**
  47. * Overrides {@link module:engine/view/element~Element#_insertChild} method.
  48. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-emptyelement-cannot-add` to prevent
  49. * adding any child nodes to EmptyElement.
  50. *
  51. * @protected
  52. */
  53. _insertChild( index, nodes ) {
  54. if ( nodes && ( nodes instanceof Node || Array.from( nodes ).length > 0 ) ) {
  55. /**
  56. * Cannot add children to {@link module:engine/view/emptyelement~EmptyElement}.
  57. *
  58. * @error view-emptyelement-cannot-add
  59. */
  60. throw new CKEditorError( 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.' );
  61. }
  62. }
  63. }
  64. // Returns `null` because block filler is not needed for EmptyElements.
  65. //
  66. // @returns {null}
  67. function getFillerOffset() {
  68. return null;
  69. }