Skip to content

Commit 5a04b17

Browse files
jburger424paulirish
authored andcommitted
core(iframe-elements): Include new IFrameElements gatherer (#8979)
1 parent 9956612 commit 5a04b17

File tree

8 files changed

+141
-9
lines changed

8 files changed

+141
-9
lines changed

lighthouse-cli/test/cli/__snapshots__/index-test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,9 @@ Object {
11441144
Object {
11451145
"path": "script-elements",
11461146
},
1147+
Object {
1148+
"path": "iframe-elements",
1149+
},
11471150
Object {
11481151
"path": "dobetterweb/appcache",
11491152
},
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<title>Where is my iframe?</title>
5-
</head>
63
<body>
74
<h1>Hello frames</h1>
8-
<iframe name="oopif" src="https://www.paulirish.com/2012/why-moving-elements-with-translate-is-better-than-posabs-topleft/" style="width: 100vw; height: 100vh;"></iframe>
5+
<iframe id="oopif" name="oopif" src="https://www.paulirish.com/2012/why-moving-elements-with-translate-is-better-than-posabs-topleft/" style="width: 100vw; height: 110vh;"></iframe>
6+
<iframe id="outer-iframe" src="http://localhost:10200/online-only.html" style="position: fixed"></iframe>
97
</body>
108
</html>

lighthouse-cli/test/smokehouse/oopif-config.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,4 @@
1010
*/
1111
module.exports = {
1212
extends: 'lighthouse:default',
13-
settings: {
14-
onlyAudits: [
15-
'network-requests',
16-
],
17-
},
1813
};

lighthouse-cli/test/smokehouse/oopif-expectations.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,27 @@ module.exports = [
2727
},
2828
},
2929
},
30+
artifacts: {
31+
IFrameElements: [
32+
{
33+
id: 'oopif',
34+
src: 'https://www.paulirish.com/2012/why-moving-elements-with-translate-is-better-than-posabs-topleft/',
35+
clientRect: {
36+
width: '>0',
37+
height: '>0',
38+
},
39+
isPositionFixed: false,
40+
},
41+
{
42+
id: 'outer-iframe',
43+
src: 'http://localhost:10200/online-only.html',
44+
clientRect: {
45+
width: '>0',
46+
height: '>0',
47+
},
48+
isPositionFixed: true,
49+
},
50+
],
51+
},
3052
},
3153
];

lighthouse-core/config/default-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ const defaultConfig = {
135135
'link-elements',
136136
'meta-elements',
137137
'script-elements',
138+
'iframe-elements',
138139
'dobetterweb/appcache',
139140
'dobetterweb/doctype',
140141
'dobetterweb/domstats',
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Copyright 2019 Google Inc. All Rights Reserved.
3+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
4+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
5+
*/
6+
'use strict';
7+
8+
const Gatherer = require('./gatherer.js');
9+
const pageFunctions = require('../../lib/page-functions.js');
10+
11+
/* eslint-env browser, node */
12+
13+
/**
14+
* @return {LH.Artifacts['IFrameElements']}
15+
*/
16+
/* istanbul ignore next */
17+
function collectIFrameElements() {
18+
// @ts-ignore - put into scope via stringification
19+
const iFrameElements = getElementsInDocument('iframe'); // eslint-disable-line no-undef
20+
return iFrameElements.map(/** @param {HTMLIFrameElement} node */ (node) => {
21+
const clientRect = node.getBoundingClientRect();
22+
const {top, bottom, left, right, width, height} = clientRect;
23+
return {
24+
id: node.id,
25+
src: node.src,
26+
clientRect: {top, bottom, left, right, width, height},
27+
// @ts-ignore - put into scope via stringification
28+
isPositionFixed: isPositionFixed(node), // eslint-disable-line no-undef
29+
};
30+
});
31+
}
32+
33+
class IFrameElements extends Gatherer {
34+
/**
35+
* @param {LH.Gatherer.PassContext} passContext
36+
* @return {Promise<LH.Artifacts['IFrameElements']>}
37+
* @override
38+
*/
39+
async afterPass(passContext) {
40+
const driver = passContext.driver;
41+
42+
const expression = `(() => {
43+
${pageFunctions.getOuterHTMLSnippetString};
44+
${pageFunctions.getElementsInDocumentString};
45+
${pageFunctions.isPositionFixedString};
46+
return (${collectIFrameElements})();
47+
})()`;
48+
49+
/** @type {LH.Artifacts['IFrameElements']} */
50+
const iframeElements = await driver.evaluateAsync(expression, {useIsolation: true});
51+
return iframeElements;
52+
}
53+
}
54+
55+
module.exports = IFrameElements;

lighthouse-core/lib/page-functions.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,43 @@ function getNodeSelector(node) {
225225
return parts.join(' > ');
226226
}
227227

228+
/**
229+
* This function checks if an element or an ancestor of an element is `position:fixed`.
230+
* In addition we ensure that the element is capable of behaving as a `position:fixed`
231+
* element, checking that it lives within a scrollable ancestor.
232+
* @param {HTMLElement} element
233+
* @return {boolean}
234+
*/
235+
/* istanbul ignore next */
236+
function isPositionFixed(element) {
237+
/**
238+
* @param {HTMLElement} element
239+
* @param {string} attr
240+
* @return {string}
241+
*/
242+
function getStyleAttrValue(element, attr) {
243+
// Check style before computedStyle as computedStyle is expensive.
244+
return element.style[attr] || window.getComputedStyle(element)[attr];
245+
}
246+
247+
// Position fixed/sticky has no effect in case when document does not scroll.
248+
const htmlEl = document.querySelector('html');
249+
if (htmlEl.scrollHeight <= htmlEl.clientHeight ||
250+
!['scroll', 'auto', 'visible'].includes(getStyleAttrValue(htmlEl, 'overflowY'))) {
251+
return false;
252+
}
253+
254+
let currentEl = element;
255+
while (currentEl) {
256+
const position = getStyleAttrValue(currentEl, 'position');
257+
if ((position === 'fixed' || position === 'sticky')) {
258+
return true;
259+
}
260+
currentEl = currentEl.parentElement;
261+
}
262+
return false;
263+
}
264+
228265
/**
229266
* Generate a human-readable label for the given element, based on end-user facing
230267
* strings like the innerText or alt attribute.
@@ -280,4 +317,5 @@ module.exports = {
280317
getNodeSelector: getNodeSelector,
281318
getNodeLabel: getNodeLabel,
282319
getNodeLabelString: getNodeLabel.toString(),
320+
isPositionFixedString: isPositionFixed.toString(),
283321
};

types/artifacts.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ declare global {
6161
export interface PublicGathererArtifacts {
6262
/** Console deprecation and intervention warnings logged by Chrome during page load. */
6363
ConsoleMessages: Crdp.Log.EntryAddedEvent[];
64+
/** All the iframe elements in the page.*/
65+
IFrameElements: Artifacts.IFrameElement[];
6466
/** Information on size and loading for all the images in the page. Natural size information for `picture` and CSS images is only available if the image was one of the largest 50 images. */
6567
ImageElements: Artifacts.ImageElement[];
6668
/** All the link elements on the page or equivalently declared in `Link` headers. @see https://html.spec.whatwg.org/multipage/links.html */
@@ -184,6 +186,24 @@ declare global {
184186
params: {name: string; value: string}[];
185187
}
186188

189+
export interface IFrameElement {
190+
/** The `id` attribute of the iframe. */
191+
id: string,
192+
/** The `src` attribute of the iframe. */
193+
src: string,
194+
/** The iframe's ClientRect. @see https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect */
195+
clientRect: {
196+
top: number;
197+
bottom: number;
198+
left: number;
199+
right: number;
200+
width: number;
201+
height: number;
202+
},
203+
/** If the iframe or an ancestor of the iframe is fixed in position. */
204+
isPositionFixed: boolean,
205+
}
206+
187207
/** @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#Attributes */
188208
export interface LinkElement {
189209
/** The `rel` attribute of the link, normalized to lower case. @see https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types */

0 commit comments

Comments
 (0)