containerelement.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. */
  38. constructor( name, attrs, children ) {
  39. super( name, attrs, children );
  40. /**
  41. * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
  42. *
  43. * @method #getFillerOffset
  44. * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
  45. */
  46. this.getFillerOffset = getFillerOffset;
  47. }
  48. /**
  49. * Checks whether this object is of the given.
  50. *
  51. * containerElement.is( 'containerElement' ); // -> true
  52. * containerElement.is( 'element' ); // -> true
  53. * containerElement.is( 'node' ); // -> true
  54. * containerElement.is( 'view:containerElement' ); // -> true
  55. * containerElement.is( 'view:element' ); // -> true
  56. * containerElement.is( 'view:node' ); // -> true
  57. *
  58. * containerElement.is( 'model:element' ); // -> false
  59. * containerElement.is( 'documentFragment' ); // -> false
  60. *
  61. * Assuming that the object being checked is a container element, you can also check its
  62. * {@link module:engine/view/containerelement~ContainerElement#name name}:
  63. *
  64. * containerElement.is( 'div' ); // -> true if this is a div container element
  65. * containerElement.is( 'contaienrElement', 'div' ); // -> same as above
  66. * text.is( 'div' ); -> false
  67. *
  68. * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
  69. *
  70. * @param {String} type Type to check when `name` parameter is present.
  71. * Otherwise, it acts like the `name` parameter.
  72. * @param {String} [name] Element name.
  73. * @returns {Boolean}
  74. */
  75. is( type, name = null ) {
  76. const cutType = type && type.replace( /^view:/, '' );
  77. if ( !name ) {
  78. return cutType == 'containerElement' || super.is( type );
  79. } else {
  80. return ( cutType == 'containerElement' && name == this.name ) || super.is( type, name );
  81. }
  82. }
  83. }
  84. /**
  85. * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
  86. *
  87. * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
  88. */
  89. export function getFillerOffset() {
  90. const children = [ ...this.getChildren() ];
  91. const lastChild = children[ this.childCount - 1 ];
  92. // Block filler is required after a `<br>` if it's the last element in its container. See #1422.
  93. if ( lastChild && lastChild.is( 'element', 'br' ) ) {
  94. return this.childCount;
  95. }
  96. for ( const child of children ) {
  97. // If there's any non-UI element – don't render the bogus.
  98. if ( !child.is( 'uiElement' ) ) {
  99. return null;
  100. }
  101. }
  102. // If there are only UI elements – render the bogus at the end of the element.
  103. return this.childCount;
  104. }