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 all 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);
if (elementScreenshot) element.prepend(elementScreenshot);

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

/**
* @param {LH.Audit.Details.FullPageScreenshot} fullPageScreenshot
* @param {LH.Artifacts.Rect} rect
* @return {boolean}
*/
function screenshotContainsRect(fullPageScreenshot, rect) {
return rect.top >= 0 &&
rect.right <= fullPageScreenshot.width &&
rect.bottom <= fullPageScreenshot.height &&
rect.left >= 0;
}

/**
* @param {number} value
* @param {number} min
Expand Down Expand Up @@ -153,13 +165,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 +203,19 @@ 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) {
if (!screenshotContainsRect(fullPageScreenshot, elementRectSC)) {
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 @@ -43,12 +55,12 @@ describe('ElementScreenshotRenderer', () => {
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