emptyelement.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Element from './element.js';
  6. import CKEditorError from '../../utils/ckeditorerror.js';
  7. /**
  8. * EmptyElement class. It is used to represent elements that cannot contain any child nodes.
  9. */
  10. export default class EmptyElement extends Element {
  11. /**
  12. * Creates new instance of EmptyElement.
  13. *
  14. * Throws {@link utils.CKEditorError CKEditorError} `view-emptyelement-cannot-add` when third parameter is passed,
  15. * to inform that usage of EmptyElement is incorrect (adding child nodes to EmptyElement is forbidden).
  16. *
  17. * @param {String} name Node name.
  18. * @param {Object|Iterable} [attributes] Collection of attributes.
  19. */
  20. constructor( name, attributes ) {
  21. super( name, attributes );
  22. if ( arguments.length > 2 ) {
  23. throwCannotAdd();
  24. }
  25. }
  26. /**
  27. * Clones provided element. Overrides {@link engine.view.Element#clone} method, as it's forbidden to pass child
  28. * nodes to EmptyElement's constructor.
  29. *
  30. * @returns {envine.view.EmptyElement} Clone of this element.
  31. */
  32. clone() {
  33. const cloned = new this.constructor( this.name, this._attrs );
  34. // Classes and styles are cloned separately - this solution is faster than adding them back to attributes and
  35. // parse once again in constructor.
  36. cloned._classes = new Set( this._classes );
  37. cloned._styles = new Map( this._styles );
  38. return cloned;
  39. }
  40. /**
  41. * Overrides {@link engine.view.Element#appendChildren} method.
  42. * Throws {@link utils.CKEditorError CKEditorError} `view-emptyelement-cannot-add` to prevent adding any child nodes
  43. * to EmptyElement.
  44. */
  45. appendChildren() {
  46. throwCannotAdd();
  47. }
  48. /**
  49. * Overrides {@link engine.view.Element#insertChildren} method.
  50. * Throws {@link utils.CKEditorError CKEditorError} `view-emptyelement-cannot-add` to prevent adding any child nodes
  51. * to EmptyElement.
  52. */
  53. insertChildren() {
  54. throwCannotAdd();
  55. }
  56. /**
  57. * Returns `null` because block filler is not needed.
  58. *
  59. * @returns {null}
  60. */
  61. getFillerOffset() {
  62. return null;
  63. }
  64. }
  65. function throwCannotAdd() {
  66. /**
  67. * Cannot add children to {@link engine.view.EmptyElement}.
  68. *
  69. * @error view-emptyelement-cannot-add
  70. */
  71. throw new CKEditorError( 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.' );
  72. }