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

report: do not show element screenshot if out of bounds #11538

Merged
merged 3 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 1 addition & 1 deletion lighthouse-core/report/html/renderer/details-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ class DetailsRenderer {
item.boundingRect,
maxThumbnailSize
);
element.prepend(elementScreenshot);
elementScreenshot && element.prepend(elementScreenshot);
connorjclark marked this conversation as resolved.
Show resolved Hide resolved

return element;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
/** @typedef {LH.Artifacts.Rect} Rect */
/** @typedef {{width: number, height: number}} Size */

/**
* Returns whether rect2 is contained entirely within rect1;
Copy link
Member

@brendankenny brendankenny Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns whether rect2 is contained entirely within rect1

should it not have a screenshot even if it's partially in bounds? e.g. what if just a few pixels are over the edge? Alternative would be to clip to bounds (and maybe return null if some percentage threshold isn't shown or something).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh, I'm inclined to keep it like this as it's simpler and this seems like an edge case (hehe).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough :)

* @param {LH.Artifacts.Rect} rect1
* @param {LH.Artifacts.Rect} rect2
* @return {boolean}
*/
// We sometimes run this as a part of a gatherer script injected into the page, so prevent
// renaming the function for code coverage.
/* istanbul ignore next */
connorjclark marked this conversation as resolved.
Show resolved Hide resolved
function rectContains(rect1, rect2) {
return rect2.top >= rect1.top &&
rect2.right <= rect1.right &&
rect2.bottom <= rect1.bottom &&
rect2.left >= rect1.left;
}

/**
* @param {number} value
* @param {number} min
Expand Down Expand Up @@ -153,13 +169,16 @@ class ElementScreenshotRenderer {
top: Number(el.dataset['rectTop']),
bottom: Number(el.dataset['rectTop']) + Number(el.dataset['rectHeight']),
};
overlay.appendChild(ElementScreenshotRenderer.render(
const screenshotElement = ElementScreenshotRenderer.render(
dom,
templateContext,
fullPageScreenshot,
elementRectSC,
maxLightboxSize
));
);
if (!screenshotElement) return;

overlay.appendChild(screenshotElement);
overlay.addEventListener('click', () => {
overlay.remove();
});
Expand Down Expand Up @@ -188,14 +207,28 @@ class ElementScreenshotRenderer {
/**
* Renders an element with surrounding context from the full page screenshot.
* Used to render both the thumbnail preview in details tables and the full-page screenshot in the lightbox.
* Returns null if element rect is outside screenshot bounds.
* @param {DOM} dom
* @param {ParentNode} templateContext
* @param {LH.Audit.Details.FullPageScreenshot} fullPageScreenshot
* @param {LH.Artifacts.Rect} elementRectSC Region of screenshot to highlight.
* @param {Size} maxRenderSizeDC e.g. maxThumbnailSize or maxLightboxSize.
* @return {Element}
* @return {Element|null}
*/
static render(dom, templateContext, fullPageScreenshot, elementRectSC, maxRenderSizeDC) {
const fullPageScreenshotRect = {
left: 0,
top: 0,
right: fullPageScreenshot.width,
bottom: fullPageScreenshot.height,
width: fullPageScreenshot.width,
height: fullPageScreenshot.height,
};
if (!rectContains(fullPageScreenshotRect, elementRectSC)) {
connorjclark marked this conversation as resolved.
Show resolved Hide resolved
// Element is out of bounds of screenshot.
return null;
}

const tmpl = dom.cloneTemplate('#tmpl-lh-element-screenshot', templateContext);
const containerEl = dom.find('.lh-element-screenshot', tmpl);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ const TEMPLATE_FILE = fs.readFileSync(
'utf8'
);

/**
* @param {{left: number, top: number, width: number, height:number}} opts
* @returns {LH.Artifacts.Rect}
*/
function makeRect(opts) {
return {
...opts,
right: opts.left + opts.width,
bottom: opts.top + opts.height,
};
}

describe('ElementScreenshotRenderer', () => {
let dom;

Expand All @@ -38,17 +50,17 @@ describe('ElementScreenshotRenderer', () => {
global.Util = undefined;
});

it('renders screenshot', () => {
it.only('renders screenshot', () => {
connorjclark marked this conversation as resolved.
Show resolved Hide resolved
const fullPageScreenshot = {
width: 1000,
height: 1000,
};
const elementRectSC = {
const elementRectSC = makeRect({
left: 50,
top: 50,
width: 200,
height: 300,
};
});
const renderContainerSizeDC = {
width: 500,
height: 500,
Expand Down Expand Up @@ -81,6 +93,30 @@ describe('ElementScreenshotRenderer', () => {
/* eslint-enable max-len */
});

it('returns null if element is out of bounds', () => {
const fullPageScreenshot = {
width: 1000,
height: 1000,
};
const elementRectSC = makeRect({
left: 50,
top: 5000,
width: 200,
height: 300,
});
const renderContainerSizeDC = {
width: 500,
height: 500,
};
expect(ElementScreenshotRenderer.render(
dom,
dom.document(),
fullPageScreenshot,
elementRectSC,
renderContainerSizeDC
)).toBe(null);
});

describe('getScreenshotPositions', () => {
it('centers the screenshot on the highlighted area', () => {
expect(
Expand Down