From 5a04b1734a3bea2f6b649cdb8b0cc7f7811f1f66 Mon Sep 17 00:00:00 2001 From: Jon Burger Date: Tue, 17 Sep 2019 21:46:45 -0700 Subject: [PATCH] core(iframe-elements): Include new IFrameElements gatherer (#8979) --- .../test/cli/__snapshots__/index-test.js.snap | 3 + lighthouse-cli/test/fixtures/oopif.html | 6 +- .../test/smokehouse/oopif-config.js | 5 -- .../test/smokehouse/oopif-expectations.js | 22 ++++++++ lighthouse-core/config/default-config.js | 1 + .../gather/gatherers/iframe-elements.js | 55 +++++++++++++++++++ lighthouse-core/lib/page-functions.js | 38 +++++++++++++ types/artifacts.d.ts | 20 +++++++ 8 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 lighthouse-core/gather/gatherers/iframe-elements.js diff --git a/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap b/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap index 5932fa49c441..3f92a647b8ba 100644 --- a/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap +++ b/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap @@ -1144,6 +1144,9 @@ Object { Object { "path": "script-elements", }, + Object { + "path": "iframe-elements", + }, Object { "path": "dobetterweb/appcache", }, diff --git a/lighthouse-cli/test/fixtures/oopif.html b/lighthouse-cli/test/fixtures/oopif.html index df3cb0c1b9cf..d3bdb265cd21 100644 --- a/lighthouse-cli/test/fixtures/oopif.html +++ b/lighthouse-cli/test/fixtures/oopif.html @@ -1,10 +1,8 @@ - - Where is my iframe? -

Hello frames

- + + diff --git a/lighthouse-cli/test/smokehouse/oopif-config.js b/lighthouse-cli/test/smokehouse/oopif-config.js index ebc5fa22bfe6..bc069999c34f 100644 --- a/lighthouse-cli/test/smokehouse/oopif-config.js +++ b/lighthouse-cli/test/smokehouse/oopif-config.js @@ -10,9 +10,4 @@ */ module.exports = { extends: 'lighthouse:default', - settings: { - onlyAudits: [ - 'network-requests', - ], - }, }; diff --git a/lighthouse-cli/test/smokehouse/oopif-expectations.js b/lighthouse-cli/test/smokehouse/oopif-expectations.js index a73509e061d7..5c624c9999e4 100644 --- a/lighthouse-cli/test/smokehouse/oopif-expectations.js +++ b/lighthouse-cli/test/smokehouse/oopif-expectations.js @@ -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, + }, + ], + }, }, ]; diff --git a/lighthouse-core/config/default-config.js b/lighthouse-core/config/default-config.js index 085e1f0dddce..ce9800a5f9f9 100644 --- a/lighthouse-core/config/default-config.js +++ b/lighthouse-core/config/default-config.js @@ -135,6 +135,7 @@ const defaultConfig = { 'link-elements', 'meta-elements', 'script-elements', + 'iframe-elements', 'dobetterweb/appcache', 'dobetterweb/doctype', 'dobetterweb/domstats', diff --git a/lighthouse-core/gather/gatherers/iframe-elements.js b/lighthouse-core/gather/gatherers/iframe-elements.js new file mode 100644 index 000000000000..97aea7d0bdff --- /dev/null +++ b/lighthouse-core/gather/gatherers/iframe-elements.js @@ -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} + * @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; diff --git a/lighthouse-core/lib/page-functions.js b/lighthouse-core/lib/page-functions.js index e6073668f79a..d54fc4070ebe 100644 --- a/lighthouse-core/lib/page-functions.js +++ b/lighthouse-core/lib/page-functions.js @@ -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. @@ -280,4 +317,5 @@ module.exports = { getNodeSelector: getNodeSelector, getNodeLabel: getNodeLabel, getNodeLabelString: getNodeLabel.toString(), + isPositionFixedString: isPositionFixed.toString(), }; diff --git a/types/artifacts.d.ts b/types/artifacts.d.ts index 9e93814da045..50b188531672 100644 --- a/types/artifacts.d.ts +++ b/types/artifacts.d.ts @@ -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 */ @@ -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 */