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 18 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-cli/test/fixtures/online-only.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
There was nothing special about this site,
nothing was left to be seen,
not even a kite.
<iframe id="inner-iframe"></iframe>
</body>
</html>
8 changes: 3 additions & 5 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>
</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',
],
},
};
43 changes: 43 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,48 @@ 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,
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
},
{
id: 'outer-iframe',
src: 'http://localhost:10200/online-only.html',
clientRect: {
width: '>0',
height: '>0',
},
isPositionFixed: true,
frame: {
name: 'outer-iframe',
url: 'http://localhost:10200/online-only.html',
securityOrigin: 'http://localhost:10200',
mimeType: 'text/html',
},
},
{
id: 'inner-iframe',
src: '',
clientRect: {
width: '>0',
height: '>0',
},
isPositionFixed: false,
frame: {
name: 'inner-iframe',
url: 'about:blank',
securityOrigin: '://',
mimeType: 'text/html',
},
},
],
},
},
];
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
80 changes: 80 additions & 0 deletions lighthouse-core/gather/gatherers/iframe-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @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();
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
// @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');
let toVisit = [frameTree];
/** @type {Map<string | undefined, LH.Crdp.Page.Frame | undefined>} */
const framesByDomId = new Map();
jburger424 marked this conversation as resolved.
Show resolved Hide resolved

while (toVisit.length) {
const frameTree = toVisit.shift();
// Should never be undefined, but needed for tsc.
if (!frameTree) continue;
if (framesByDomId.has(frameTree.frame.name)) {
// DOM ID collision, mark as undefined.
framesByDomId.set(frameTree.frame.name, undefined);
} else {
framesByDomId.set(frameTree.frame.name, frameTree.frame);
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
}

// Add children to queue.
if (frameTree.childFrames) {
toVisit = toVisit.concat(frameTree.childFrames);
}
}

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;
46 changes: 46 additions & 0 deletions lighthouse-core/lib/page-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ function getElementsInDocument(selector) {
if (el.shadowRoot) {
_findAllElements(el.shadowRoot.querySelectorAll('*'));
}
// If the element has a contentWindow (IFrame), dig deeper.
// Try/Catch needed in case of cross-origin frame.
try {
if (el.contentWindow) {
_findAllElements(el.contentWindow.document.querySelectorAll('*'));
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (e) {}
}
};
_findAllElements(document.querySelectorAll('*'));
Expand Down Expand Up @@ -225,6 +232,44 @@ 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
* @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.
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
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');
// Only truly fixed if an ancestor is scrollable.
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
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 +325,5 @@ module.exports = {
getNodeSelector: getNodeSelector,
getNodeLabel: getNodeLabel,
getNodeLabelString: getNodeLabel.toString(),
isPositionFixedString: isPositionFixed.toString(),
};
22 changes: 22 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,26 @@ 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,
/** The Frame of the iframe, undefined if cross-origin. @see https://chromedevtools.github.io/devtools-protocol/tot/Page#type-Frame */
jburger424 marked this conversation as resolved.
Show resolved Hide resolved
frame?: Crdp.Page.Frame,
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