Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(iframe-elements): Include new IFrameElements gatherer #8979

Merged
merged 23 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
89d81ef
Bring over IFrameElements gatherer and add types.
jburger424 May 16, 2019
499c599
Move page functions to `.../lib/page-functions.js`
jburger424 May 17, 2019
4abd4dc
Update default config to include IFrameElements gatherer in defaultPass.
jburger424 May 17, 2019
3ee62ee
Resolve no-undef lint errors.
jburger424 May 17, 2019
ce2e280
Address comments.
jburger424 May 17, 2019
2642fa9
Update `index-test.js.snap` snapshot.
jburger424 May 21, 2019
76e3e1d
Address numerous comments.
jburger424 May 21, 2019
e07610e
Merge branch 'master' into iframe-elements-gatherer
jburger424 May 21, 2019
aa8e553
Merge branch 'master' into iframe-elements-gatherer
jburger424 Jul 1, 2019
861e3ed
Address comments and update snapshot.
jburger424 Jul 1, 2019
0c8ba7f
Recurse frameTree.
jburger424 Jul 1, 2019
c9887c6
Fix lint errors.
jburger424 Jul 1, 2019
0ca3b25
Declare type for framesByDomId map.
jburger424 Jul 1, 2019
d2223fd
Address comments.
jburger424 Jul 2, 2019
371bcc9
Merge branch 'master' into iframe-elements-gatherer
jburger424 Jul 2, 2019
a1a6f21
Resolve comments.
jburger424 Jul 12, 2019
a4bacac
Address comments.
jburger424 Jul 15, 2019
54f9793
Update oopif to ensure functionality of IFrameElements gatherer.
jburger424 Jul 16, 2019
13e6f66
Address comments.
jburger424 Jul 16, 2019
e674bc9
Remove `frame` object from IFrameElement.
jburger424 Sep 12, 2019
4121d17
Merge branch 'master' into iframe-elements-gatherer
jburger424 Sep 12, 2019
8d9354e
Remove recursive iframe gathering functionality from _findAllElements…
jburger424 Sep 12, 2019
82ddb3b
Remove unused `inner-iframe` from online-only fixture.
jburger424 Sep 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions lighthouse-core/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const defaultConfig = {
'link-elements',
'meta-elements',
'script-elements',
'iframe-elements',
'dobetterweb/appcache',
'dobetterweb/doctype',
'dobetterweb/domstats',
Expand Down
83 changes: 83 additions & 0 deletions lighthouse-core/gather/gatherers/iframe-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @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']}
*/
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
/* 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();
// Marking 1x1 as non-visible to ignore tracking pixels.
jburger424 marked this conversation as resolved.
Show resolved Hide resolved

const {top, bottom, left, right, width, height} = clientRect;
return {
id: node.id,
src: node.src,
clientRect: {top, bottom, left, right, width, height},
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
pixelArea: width * height,
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
// @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 {frameTree} = await driver.sendCommand('Page.getFrameTree');
const toVisit = [frameTree];
/** @type {Map<string | undefined, LH.Crdp.Page.Frame | null>} */
const framesByDomId = new Map();
jburger424 marked this conversation as resolved.
Show resolved Hide resolved

while (toVisit.length) {
const tempFrameTree = toVisit.shift();
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
if (tempFrameTree) {
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
for (const childFrameTree of tempFrameTree.childFrames || []) {
if (childFrameTree.childFrames) {
toVisit.concat(childFrameTree.childFrames);
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
}
if (framesByDomId.has(childFrameTree.frame.name)) {
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
// DOM ID collision, mark it as null.
framesByDomId.set(childFrameTree.frame.name, null);
} else {
framesByDomId.set(childFrameTree.frame.name, childFrameTree.frame);
}
}
}
}

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

/** @type {LH.Artifacts['IFrameElements']} */
const iframeElements = await driver.evaluateAsync(expression, {useIsolation: true});
for (const el of iframeElements) {
el.frame = framesByDomId.get(el.id);
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
}
return iframeElements;
}
}

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

/**
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
* 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) {
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
/**
* @param {HTMLElement} element
* @return {boolean}
*/
function hasScrollableAncestor(element) {
let currentEl = element.parentElement;
while (currentEl) {
if (currentEl.scrollHeight > currentEl.clientHeight) {
const yScroll = getStyleAttrValue(currentEl, 'overflowY');
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
if (yScroll) {
return true;
}
}
currentEl = currentEl.parentElement;
}
return false;
}

/**
* @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];
}

let currentEl = element;
while (currentEl) {
const position = getStyleAttrValue(currentEl, 'position');
// Only truly fixed if an ancestor is scrollable.
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
if ((position === 'fixed' || position === 'sticky') && hasScrollableAncestor(currentEl)) {
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
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 +329,5 @@ module.exports = {
getNodeSelector: getNodeSelector,
getNodeLabel: getNodeLabel,
getNodeLabelString: getNodeLabel.toString(),
isPositionFixedString: isPositionFixed.toString(),
};
24 changes: 24 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 @@ -182,6 +184,28 @@ 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;
},
/** The pixel area iframe. */
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
pixelArea: number,
/** If the iframe or an ancestor of the iframe is fixed in position. */
isPositionFixed: boolean,
/** The Frame of the iframe. @see https://chromedevtools.github.io/devtools-protocol/tot/Page#type-Frame */
frame?: Crdp.Page.Frame | null,
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
}

/** @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