MCPcopy
hub / github.com/ampproject/amphtml / applyStaticLayout

Function applyStaticLayout

src/core/static-layout.js:100–202  ·  view source on GitHub ↗
(element)

Source from the content-addressed store, hash-verified

98 * @return {Layout_Enum}
99 */
100export function applyStaticLayout(element) {
101 // Check if the layout has already been done by server-side rendering or
102 // client-side rendering and the element was cloned. The document may be
103 // visible to the user if the boilerplate was removed so please take care in
104 // making changes here.
105 const completedLayoutAttr = element.getAttribute('i-amphtml-layout');
106 if (completedLayoutAttr) {
107 const layout = parseLayout(completedLayoutAttr);
108 devAssert(layout);
109
110 if (
111 (layout == Layout_Enum.RESPONSIVE || layout == Layout_Enum.INTRINSIC) &&
112 element.firstElementChild
113 ) {
114 // Find sizer, but assume that it might not have been parsed yet.
115 element.sizerElement =
116 /** @type {?HTMLElement} */ (
117 element.querySelector('i-amphtml-sizer')
118 ) || undefined;
119 element.sizerElement?.setAttribute('slot', 'i-amphtml-svc');
120 } else if (layout == Layout_Enum.NODISPLAY) {
121 toggle(element, false);
122 }
123 return layout;
124 }
125
126 // If the layout was already done by server-side rendering (SSR), then the
127 // code below will not run. Any changes below will necessitate a change to SSR
128 // and must be coordinated with caches that implement SSR. See bit.ly/amp-ssr.
129 const {height, layout, width} = getEffectiveLayoutInternal(element);
130
131 // Apply UI.
132 element.classList.add(getLayoutClass(layout));
133 if (isLayoutSizeDefined(layout)) {
134 element.classList.add('i-amphtml-layout-size-defined');
135 }
136 if (layout == Layout_Enum.NODISPLAY) {
137 // CSS defines layout=nodisplay automatically with `display:none`. Thus
138 // no additional styling is needed.
139 toggle(element, false);
140 } else if (layout == Layout_Enum.FIXED) {
141 setStyles(element, {
142 width: devAssertString(width),
143 height: devAssertString(height),
144 });
145 } else if (layout == Layout_Enum.FIXED_HEIGHT) {
146 setStyle(element, 'height', devAssertString(height));
147 } else if (layout == Layout_Enum.RESPONSIVE) {
148 const sizer = element.ownerDocument.createElement('i-amphtml-sizer');
149 sizer.setAttribute('slot', 'i-amphtml-svc');
150 const heightNumeral = getLengthNumeral(height);
151 const widthNumeral = getLengthNumeral(width);
152 devAssertNumber(heightNumeral);
153 devAssertNumber(widthNumeral);
154 setStyles(sizer, {
155 paddingTop: (heightNumeral / widthNumeral) * 100 + '%',
156 });
157 element.insertBefore(sizer, element.firstChild);

Callers 4

test-layout.jsFile · 0.90
connectedCallbackMethod · 0.90
wrapFunction · 0.90

Calls 15

parseLayoutFunction · 0.90
devAssertFunction · 0.90
toggleFunction · 0.90
getLayoutClassFunction · 0.90
isLayoutSizeDefinedFunction · 0.90
setStylesFunction · 0.90
devAssertStringFunction · 0.90
setStyleFunction · 0.90
getLengthNumeralFunction · 0.90
devAssertNumberFunction · 0.90
htmlForFunction · 0.90
devAssertElementFunction · 0.90

Tested by

no test coverage detected