getContainingBlockElement method
Implementation
Element? getContainingBlockElement({CSSPositionType? positionType}) {
Element? containingBlockElement;
positionType ??= renderStyle.position;
switch (positionType) {
case CSSPositionType.relative:
case CSSPositionType.static:
case CSSPositionType.sticky:
containingBlockElement = parentElement;
break;
case CSSPositionType.absolute:
Element viewportElement = ownerDocument.documentElement!;
// If the element has 'position: absolute', the containing block is established by the nearest ancestor with
// a 'position' of 'absolute', 'relative' or 'fixed', in the following way:
// 1. In the case that the ancestor is an inline element, the containing block is the bounding box around
// the padding boxes of the first and the last inline boxes generated for that element.
// In CSS 2.1, if the inline element is split across multiple lines, the containing block is undefined.
// 2. Otherwise, the containing block is formed by the padding edge of the ancestor.
containingBlockElement = _findContainingBlock(this, viewportElement);
break;
case CSSPositionType.fixed:
Element viewportElement = ownerDocument.documentElement!;
// For fixed positioning, the containing block is the viewport unless an
// ancestor establishes a fixed-position containing block (e.g. transform).
// https://drafts.csswg.org/css-position/#fixedpos-containing-block
containingBlockElement =
_findFixedContainingBlock(this, viewportElement) ?? viewportElement;
break;
}
return containingBlockElement;
}