8
0

containerelement.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @license Copyright (c) 2003-2020, 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/containerelement
  7. */
  8. import Element from './element';
  9. /**
  10. * Containers are elements which define document structure. They define boundaries for
  11. * {@link module:engine/view/attributeelement~AttributeElement attributes}. They are mostly used for block elements like `<p>` or `<div>`.
  12. *
  13. * Editing engine does not define a fixed HTML DTD. This is why a feature developer needs to choose between various
  14. * types (container element, {@link module:engine/view/attributeelement~AttributeElement attribute element},
  15. * {@link module:engine/view/emptyelement~EmptyElement empty element}, etc) when developing a feature.
  16. *
  17. * The container element should be your default choice when writing a converter, unless:
  18. *
  19. * * this element represents a model text attribute (then use {@link module:engine/view/attributeelement~AttributeElement}),
  20. * * this is an empty element like `<img>` (then use {@link module:engine/view/emptyelement~EmptyElement}),
  21. * * this is a root element,
  22. * * this is a nested editable element (then use {@link module:engine/view/editableelement~EditableElement}).
  23. *
  24. * To create a new container element instance use the
  25. * {@link module:engine/view/downcastwriter~DowncastWriter#createContainerElement `DowncastWriter#createContainerElement()`}
  26. * method.
  27. *
  28. * @extends module:engine/view/element~Element
  29. */
  30. export default class ContainerElement extends Element {
  31. /**
  32. * Creates a container element.
  33. *
  34. * @see module:engine/view/downcastwriter~DowncastWriter#createContainerElement
  35. * @see module:engine/view/element~Element
  36. * @protected
  37. * @param {module:engine/view/document~Document} document The document instance to which this element belongs.
  38. * @param {String} name Node name.
  39. * @param {Object|Iterable} [attrs] Collection of attributes.
  40. * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
  41. * A list of nodes to be inserted into created element.
  42. */
  43. constructor( document, name, attrs, children ) {
  44. super( document, name, attrs, children );
  45. /**
  46. * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
  47. *
  48. * @method #getFillerOffset
  49. * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
  50. */
  51. this.getFillerOffset = getFillerOffset;
  52. }
  53. /**
  54. * Checks whether this object is of the given.
  55. *
  56. * containerElement.is( 'containerElement' ); // -> true
  57. * containerElement.is( 'element' ); // -> true
  58. * containerElement.is( 'node' ); // -> true
  59. * containerElement.is( 'view:containerElement' ); // -> true
  60. * containerElement.is( 'view:element' ); // -> true
  61. * containerElement.is( 'view:node' ); // -> true
  62. *
  63. * containerElement.is( 'model:element' ); // -> false
  64. * containerElement.is( 'documentFragment' ); // -> false
  65. *
  66. * Assuming that the object being checked is a container element, you can also check its
  67. * {@link module:engine/view/containerelement~ContainerElement#name name}:
  68. *
  69. * containerElement.is( 'div' ); // -> true if this is a div container element
  70. * containerElement.is( 'contaienrElement', 'div' ); // -> same as above
  71. * text.is( 'div' ); -> false
  72. *
  73. * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
  74. *
  75. * @param {String} type Type to check when `name` parameter is present.
  76. * Otherwise, it acts like the `name` parameter.
  77. * @param {String} [name] Element name.
  78. * @returns {Boolean}
  79. */
  80. is( type, name = null ) {
  81. if ( !name ) {
  82. return type === 'containerElement' || type === 'view:containerElement' ||
  83. // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
  84. type === this.name || type === 'view:' + this.name ||
  85. type === 'element' || type === 'view:element' ||
  86. type === 'node' || type === 'view:node';
  87. } else {
  88. return name === this.name && (
  89. type === 'containerElement' || type === 'view:containerElement' ||
  90. // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
  91. type === 'element' || type === 'view:element'
  92. );
  93. }
  94. }
  95. }
  96. /**
  97. * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
  98. *
  99. * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
  100. */
  101. export function getFillerOffset() {
  102. const children = [ ...this.getChildren() ];
  103. const lastChild = children[ this.childCount - 1 ];
  104. // Block filler is required after a `<br>` if it's the last element in its container. See #1422.
  105. if ( lastChild && lastChild.is( 'element', 'br' ) ) {
  106. return this.childCount;
  107. }
  108. for ( const child of children ) {
  109. // If there's any non-UI element – don't render the bogus.
  110. if ( !child.is( 'uiElement' ) ) {
  111. return null;
  112. }
  113. }
  114. // If there are only UI elements – render the bogus at the end of the element.
  115. return this.childCount;
  116. }