blob: e90f956195de47cde0a1c7fe001cc04e4835930e [file] [log] [blame]
Patrick Hulcea087f622018-05-18 00:37:53 +00001// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Tim van der Lippe132b4ba2020-09-02 16:00:48 +01005// @ts-nocheck
6// TODO(crbug.com/1011811): Enable TypeScript compiler checks
7
Paul Lewisdaac1062020-03-05 14:37:10 +00008import * as Common from '../common/common.js';
9import * as Components from '../components/components.js';
10import * as HostModule from '../host/host.js';
Simon Zünd2c704cd2020-06-04 11:08:35 +020011import * as Platform from '../platform/platform.js';
Tim van der Lippe5df64b22020-09-11 13:04:24 +010012import * as Root from '../root/root.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000013import * as SDK from '../sdk/sdk.js';
Paul Lewisca569a52020-09-09 17:11:51 +010014import * as ThemeSupport from '../theme_support/theme_support.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000015import * as Timeline from '../timeline/timeline.js';
16import * as UI from '../ui/ui.js';
Tim van der Lippe696c9262020-08-26 15:39:32 +010017import * as Workspace from '../workspace/workspace.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000018
Tim van der Lippe1e10f852020-10-30 14:35:01 +000019import * as ReportRenderer from './LighthouseReporterTypes.js'; // eslint-disable-line no-unused-vars
20
Paul Lewiscf2ef222019-11-22 14:55:35 +000021const MaxLengthForLinks = 40;
22
Patrick Hulcea087f622018-05-18 00:37:53 +000023/**
24 * @override
25 */
Tim van der Lippe1e10f852020-10-30 14:35:01 +000026export class LighthouseReportRenderer extends self.ReportRenderer {
Patrick Hulcea087f622018-05-18 00:37:53 +000027 /**
Paul Irish8f1e33d2018-05-31 02:29:50 +000028 * @param {!Element} el Parent element to render the report into.
29 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Patrick Hulcea087f622018-05-18 00:37:53 +000030 */
Paul Irish8f1e33d2018-05-31 02:29:50 +000031 static addViewTraceButton(el, artifacts) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000032 if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) {
Paul Irish8f1e33d2018-05-31 02:29:50 +000033 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000034 }
Patrick Hulcea087f622018-05-18 00:37:53 +000035
Adam Rainef46d1582020-08-17 20:24:32 +000036 const simulated = artifacts.settings.throttlingMethod === 'simulate';
cjamcl@google.com21d2d222019-08-09 01:58:17 +000037 const container = el.querySelector('.lh-audit-group');
Paul Irishbf204682020-05-13 16:11:37 -070038 const disclaimerEl = container.querySelector('.lh-metrics__disclaimer');
39 // If it was a PWA-only run, we'd have a trace but no perf category to add the button to
40 if (!disclaimerEl) {
cjamcl@google.com21d2d222019-08-09 01:58:17 +000041 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000042 }
cjamcl@google.com21d2d222019-08-09 01:58:17 +000043
Paul Irish8f1e33d2018-05-31 02:29:50 +000044 const defaultPassTrace = artifacts.traces.defaultPass;
Adam Rainef46d1582020-08-17 20:24:32 +000045 const label = simulated ? Common.UIString.UIString('View Original Trace') : Common.UIString.UIString('View Trace');
46 const timelineButton = UI.UIUtils.createTextButton(label, onViewTraceClick, 'view-trace');
47 if (simulated) {
48 timelineButton.title = Common.UIString.UIString(
49 'The performance metrics above are simulated and won\'t match the timings found in this trace. Disable simulated throttling in "Lighthouse Settings" if you want the timings to match.');
50 }
Paul Irishbf204682020-05-13 16:11:37 -070051 container.insertBefore(timelineButton, disclaimerEl.nextSibling);
Paul Irish8f1e33d2018-05-31 02:29:50 +000052
53 async function onViewTraceClick() {
Paul Lewisdaac1062020-03-05 14:37:10 +000054 HostModule.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace);
Tim van der Lippe80d82652020-08-27 14:53:44 +010055 await UI.InspectorView.InspectorView.instance().showPanel('timeline');
Paul Lewisdaac1062020-03-05 14:37:10 +000056 Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
Paul Irish8f1e33d2018-05-31 02:29:50 +000057 }
Patrick Hulcea087f622018-05-18 00:37:53 +000058 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000059
60 /**
61 * @param {!Element} el
62 */
63 static async linkifyNodeDetails(el) {
Paul Lewisdaac1062020-03-05 14:37:10 +000064 const mainTarget = SDK.SDKModel.TargetManager.instance().mainTarget();
65 const domModel = mainTarget.model(SDK.DOMModel.DOMModel);
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000066
67 for (const origElement of el.getElementsByClassName('lh-node')) {
Tim van der Lippe1e10f852020-10-30 14:35:01 +000068 /** @type {!ReportRenderer.NodeDetailsJSON} */
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000069 const detailsItem = origElement.dataset;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000070 if (!detailsItem.path) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000071 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000072 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000073
74 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
75
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000076 if (!nodeId) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000077 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000078 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000079 const node = domModel.nodeForId(nodeId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000080 if (!node) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000081 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000082 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000083
Paul Lewisdaac1062020-03-05 14:37:10 +000084 const element = await Common.Linkifier.Linkifier.linkify(node, {tooltip: detailsItem.snippet});
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000085 origElement.title = '';
86 origElement.textContent = '';
87 origElement.appendChild(element);
88 }
89 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +000090
91 /**
92 * @param {!Element} el
93 */
Connor Clark0403a422019-11-18 18:03:18 -080094 static async linkifySourceLocationDetails(el) {
95 for (const origElement of el.getElementsByClassName('lh-source-location')) {
Tim van der Lippe1e10f852020-10-30 14:35:01 +000096 /** @type {!ReportRenderer.SourceLocationDetailsJSON} */
Connor Clark0403a422019-11-18 18:03:18 -080097 const detailsItem = origElement.dataset;
98 if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) {
99 continue;
100 }
101 const url = detailsItem.sourceUrl;
102 const line = Number(detailsItem.sourceLine);
103 const column = Number(detailsItem.sourceColumn);
Paul Lewisdaac1062020-03-05 14:37:10 +0000104 const element = await Components.Linkifier.Linkifier.linkifyURL(
105 url, {lineNumber: line, column, maxLength: MaxLengthForLinks});
Connor Clark0403a422019-11-18 18:03:18 -0800106 origElement.title = '';
107 origElement.textContent = '';
108 origElement.appendChild(element);
109 }
110 }
111
112 /**
113 * @param {!Element} el
114 */
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000115 static handleDarkMode(el) {
Paul Lewisca569a52020-09-09 17:11:51 +0100116 if (ThemeSupport.ThemeSupport.instance().themeName() === 'dark') {
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000117 el.classList.add('dark');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000118 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000119 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000120}
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000121
122/**
123 * @override
124 */
Tim van der Lippe1e10f852020-10-30 14:35:01 +0000125export class LighthouseReportUIFeatures extends self.ReportUIFeatures {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000126 /**
Connor Clark99508362019-08-20 19:52:23 +0000127 * @param {!DOM} dom
128 */
129 constructor(dom) {
130 super(dom);
131 this._beforePrint = null;
132 this._afterPrint = null;
133 }
134
135 /**
136 * @param {?function()} beforePrint
137 */
138 setBeforePrint(beforePrint) {
139 this._beforePrint = beforePrint;
140 }
141
142 /**
143 * @param {?function()} afterPrint
144 */
145 setAfterPrint(afterPrint) {
146 this._afterPrint = afterPrint;
147 }
148
149 /**
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000150 * Returns the html that recreates this report.
151 * @return {string}
152 * @protected
153 */
154 getReportHtml() {
155 this.resetUIState();
156 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
157 }
158
159 /**
160 * Downloads a file (blob) using the system dialog prompt.
161 * @param {!Blob|!File} blob The file to save.
162 */
163 async _saveFile(blob) {
Paul Lewisdaac1062020-03-05 14:37:10 +0000164 const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000165 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
Simon Zünd2c704cd2020-06-04 11:08:35 +0200166 const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime));
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000167 const ext = blob.type.match('json') ? '.json' : '.html';
168 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
169 const text = await blob.text();
Tim van der Lippe696c9262020-08-26 15:39:32 +0100170 Workspace.FileManager.FileManager.instance().save(basename, text, true /* forceSaveAs */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000171 }
172
173 async _print() {
174 const document = this.getDocument();
175 const clonedReport = document.querySelector('.lh-root').cloneNode(true /* deep */);
176 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
177 const style = printWindow.document.createElement('style');
Tim van der Lippe5df64b22020-09-11 13:04:24 +0100178 style.textContent = Root.Runtime.cachedResources.get('third_party/lighthouse/report-assets/report.css');
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000179 printWindow.document.head.appendChild(style);
180 printWindow.document.body.replaceWith(clonedReport);
181 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
Connor Clark2bc3be22020-02-14 14:34:19 -0800182 await LighthouseReportRenderer.linkifyNodeDetails(clonedReport);
Connor Clark99508362019-08-20 19:52:23 +0000183
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000184 if (this._beforePrint) {
Connor Clark99508362019-08-20 19:52:23 +0000185 this._beforePrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000186 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000187 printWindow.focus();
188 printWindow.print();
189 printWindow.close();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000190 if (this._afterPrint) {
Connor Clark99508362019-08-20 19:52:23 +0000191 this._afterPrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000192 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000193 }
194
195 /**
196 * @suppress {visibility}
197 * @return {!Document}
198 */
199 getDocument() {
200 return this._document;
201 }
202
203 /**
204 * @suppress {visibility}
205 */
206 resetUIState() {
207 this._resetUIState();
208 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000209}