Skip to content

Commit

Permalink
core(iframe-elements): Include new IFrameElements gatherer (#8979)
Browse files Browse the repository at this point in the history
  • Loading branch information
jburger424 authored and paulirish committed Nov 6, 2019
1 parent 9956612 commit 5a04b17
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 9 deletions.
3 changes: 3 additions & 0 deletions lighthouse-cli/test/cli/__snapshots__/index-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,9 @@ Object {
Object {
"path": "script-elements",
},
Object {
"path": "iframe-elements",
},
Object {
"path": "dobetterweb/appcache",
},
Expand Down
6 changes: 2 additions & 4 deletions lighthouse-cli/test/fixtures/oopif.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<title>Where is my iframe?</title>
</head>
<body>
<h1>Hello frames</h1>
<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>
<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>
<iframe id="outer-iframe" src="http://localhost:10200/online-only.html" style="position: fixed"></iframe>
</body>
</html>
5 changes: 0 additions & 5 deletions lighthouse-cli/test/smokehouse/oopif-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@
*/
module.exports = {
extends: 'lighthouse:default',
settings: {
onlyAudits: [
'network-requests',
],
},
};
22 changes: 22 additions & 0 deletions lighthouse-cli/test/smokehouse/oopif-expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,27 @@ module.exports = [
},
},
},
artifacts: {
IFrameElements: [
{
id: 'oopif',
src: 'https://www.paulirish.com/2012/why-moving-elements-with-translate-is-better-than-posabs-topleft/',
clientRect: {
width: '>0',
height: '>0',
},
isPositionFixed: false,
},
{
id: 'outer-iframe',
src: 'http://localhost:10200/online-only.html',
clientRect: {
width: '>0',
height: '>0',
},
isPositionFixed: true,
},
],
},
},
];
1 change: 1 addition & 0 deletions lighthouse-core/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const defaultConfig = {
'link-elements',
'meta-elements',
'script-elements',
'iframe-elements',
'dobetterweb/appcache',
'dobetterweb/doctype',
'dobetterweb/domstats',
Expand Down
55 changes: 55 additions & 0 deletions lighthouse-core/gather/gatherers/iframe-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @license Copyright 2019 Google Inc. All Rights Reserved.
* 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
* 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.
*/
'use strict';

const Gatherer = require('./gatherer.js');
const pageFunctions = require('../../lib/page-functions.js');

/* eslint-env browser, node */

/**
* @return {LH.Artifacts['IFrameElements']}
*/
/* istanbul ignore next */
function collectIFrameElements() {
// @ts-ignore - put into scope via stringification
const iFrameElements = getElementsInDocument('iframe'); // eslint-disable-line no-undef
return iFrameElements.map(/** @param {HTMLIFrameElement} node */ (node) => {
const clientRect = node.getBoundingClientRect();
const {top, bottom, left, right, width, height} = clientRect;
return {
id: node.id,
src: node.src,
clientRect: {top, bottom, left, right, width, height},
// @ts-ignore - put into scope via stringification
isPositionFixed: isPositionFixed(node), // eslint-disable-line no-undef
};
});
}

class IFrameElements extends Gatherer {
/**
* @param {LH.Gatherer.PassContext} passContext
* @return {Promise<LH.Artifacts['IFrameElements']>}
* @override
*/
async afterPass(passContext) {
const driver = passContext.driver;

const expression = `(() => {
${pageFunctions.getOuterHTMLSnippetString};
${pageFunctions.getElementsInDocumentString};
${pageFunctions.isPositionFixedString};
return (${collectIFrameElements})();
})()`;

/** @type {LH.Artifacts['IFrameElements']} */
const iframeElements = await driver.evaluateAsync(expression, {useIsolation: true});
return iframeElements;
}
}

module.exports = IFrameElements;
38 changes: 38 additions & 0 deletions lighthouse-core/lib/page-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,43 @@ function getNodeSelector(node) {
return parts.join(' > ');
}

/**
* This function checks if an element or an ancestor of an element is `position:fixed`.
* In addition we ensure that the element is capable of behaving as a `position:fixed`
* element, checking that it lives within a scrollable ancestor.
* @param {HTMLElement} element
* @return {boolean}
*/
/* istanbul ignore next */
function isPositionFixed(element) {
/**
* @param {HTMLElement} element
* @param {string} attr
* @return {string}
*/
function getStyleAttrValue(element, attr) {
// Check style before computedStyle as computedStyle is expensive.
return element.style[attr] || window.getComputedStyle(element)[attr];
}

// Position fixed/sticky has no effect in case when document does not scroll.
const htmlEl = document.querySelector('html');
if (htmlEl.scrollHeight <= htmlEl.clientHeight ||
!['scroll', 'auto', 'visible'].includes(getStyleAttrValue(htmlEl, 'overflowY'))) {
return false;
}

let currentEl = element;
while (currentEl) {
const position = getStyleAttrValue(currentEl, 'position');
if ((position === 'fixed' || position === 'sticky')) {
return true;
}
currentEl = currentEl.parentElement;
}
return false;
}

/**
* Generate a human-readable label for the given element, based on end-user facing
* strings like the innerText or alt attribute.
Expand Down Expand Up @@ -280,4 +317,5 @@ module.exports = {
getNodeSelector: getNodeSelector,
getNodeLabel: getNodeLabel,
getNodeLabelString: getNodeLabel.toString(),
isPositionFixedString: isPositionFixed.toString(),
};
20 changes: 20 additions & 0 deletions types/artifacts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ declare global {
export interface PublicGathererArtifacts {
/** Console deprecation and intervention warnings logged by Chrome during page load. */
ConsoleMessages: Crdp.Log.EntryAddedEvent[];
/** All the iframe elements in the page.*/
IFrameElements: Artifacts.IFrameElement[];
/** 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. */
ImageElements: Artifacts.ImageElement[];
/** All the link elements on the page or equivalently declared in `Link` headers. @see https://html.spec.whatwg.org/multipage/links.html */
Expand Down Expand Up @@ -184,6 +186,24 @@ declare global {
params: {name: string; value: string}[];
}

export interface IFrameElement {
/** The `id` attribute of the iframe. */
id: string,
/** The `src` attribute of the iframe. */
src: string,
/** The iframe's ClientRect. @see https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect */
clientRect: {
top: number;
bottom: number;
left: number;
right: number;
width: number;
height: number;
},
/** If the iframe or an ancestor of the iframe is fixed in position. */
isPositionFixed: boolean,
}

/** @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#Attributes */
export interface LinkElement {
/** The `rel` attribute of the link, normalized to lower case. @see https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types */
Expand Down

0 comments on commit 5a04b17

Please sign in to comment.