blob: 224e4c4e6e2f56cd2ca53a57386ee90e6274fb6f [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
Paul Lewisdaac1062020-03-05 14:37:10 +00005import * as Common from '../common/common.js';
6import * as Components from '../components/components.js';
7import * as HostModule from '../host/host.js';
Simon Zünd2c704cd2020-06-04 11:08:35 +02008import * as Platform from '../platform/platform.js';
Paul Lewisdaac1062020-03-05 14:37:10 +00009import * as SDK from '../sdk/sdk.js';
10import * as Timeline from '../timeline/timeline.js';
11import * as UI from '../ui/ui.js';
12
Paul Lewiscf2ef222019-11-22 14:55:35 +000013const MaxLengthForLinks = 40;
14
Patrick Hulcea087f622018-05-18 00:37:53 +000015/**
16 * @override
17 */
Connor Clark2bc3be22020-02-14 14:34:19 -080018export class LighthouseReportRenderer extends ReportRenderer {
Patrick Hulcea087f622018-05-18 00:37:53 +000019 /**
Paul Irish8f1e33d2018-05-31 02:29:50 +000020 * @param {!Element} el Parent element to render the report into.
21 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Patrick Hulcea087f622018-05-18 00:37:53 +000022 */
Paul Irish8f1e33d2018-05-31 02:29:50 +000023 static addViewTraceButton(el, artifacts) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000024 if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) {
Paul Irish8f1e33d2018-05-31 02:29:50 +000025 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000026 }
Patrick Hulcea087f622018-05-18 00:37:53 +000027
cjamcl@google.com21d2d222019-08-09 01:58:17 +000028 const container = el.querySelector('.lh-audit-group');
Paul Irishbf204682020-05-13 16:11:37 -070029 const disclaimerEl = container.querySelector('.lh-metrics__disclaimer');
30 // If it was a PWA-only run, we'd have a trace but no perf category to add the button to
31 if (!disclaimerEl) {
cjamcl@google.com21d2d222019-08-09 01:58:17 +000032 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000033 }
cjamcl@google.com21d2d222019-08-09 01:58:17 +000034
Paul Irish8f1e33d2018-05-31 02:29:50 +000035 const defaultPassTrace = artifacts.traces.defaultPass;
Paul Lewisdaac1062020-03-05 14:37:10 +000036 const timelineButton =
37 UI.UIUtils.createTextButton(Common.UIString.UIString('View Trace'), onViewTraceClick, 'view-trace');
Paul Irishbf204682020-05-13 16:11:37 -070038 container.insertBefore(timelineButton, disclaimerEl.nextSibling);
Paul Irish8f1e33d2018-05-31 02:29:50 +000039
40 async function onViewTraceClick() {
Paul Lewisdaac1062020-03-05 14:37:10 +000041 HostModule.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace);
Paul Lewis0a7c6b62020-01-23 16:16:22 +000042 await self.UI.inspectorView.showPanel('timeline');
Paul Lewisdaac1062020-03-05 14:37:10 +000043 Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
Paul Irish8f1e33d2018-05-31 02:29:50 +000044 }
Patrick Hulcea087f622018-05-18 00:37:53 +000045 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000046
47 /**
48 * @param {!Element} el
49 */
50 static async linkifyNodeDetails(el) {
Paul Lewisdaac1062020-03-05 14:37:10 +000051 const mainTarget = SDK.SDKModel.TargetManager.instance().mainTarget();
52 const domModel = mainTarget.model(SDK.DOMModel.DOMModel);
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000053
54 for (const origElement of el.getElementsByClassName('lh-node')) {
55 /** @type {!DetailsRenderer.NodeDetailsJSON} */
56 const detailsItem = origElement.dataset;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000057 if (!detailsItem.path) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000058 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000059 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000060
61 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
62
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000063 if (!nodeId) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000064 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000065 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000066 const node = domModel.nodeForId(nodeId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000067 if (!node) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000068 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000069 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000070
Paul Lewisdaac1062020-03-05 14:37:10 +000071 const element = await Common.Linkifier.Linkifier.linkify(node, {tooltip: detailsItem.snippet});
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000072 origElement.title = '';
73 origElement.textContent = '';
74 origElement.appendChild(element);
75 }
76 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +000077
78 /**
79 * @param {!Element} el
80 */
Connor Clark0403a422019-11-18 18:03:18 -080081 static async linkifySourceLocationDetails(el) {
82 for (const origElement of el.getElementsByClassName('lh-source-location')) {
83 /** @type {!DetailsRenderer.SourceLocationDetailsJSON} */
84 const detailsItem = origElement.dataset;
85 if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) {
86 continue;
87 }
88 const url = detailsItem.sourceUrl;
89 const line = Number(detailsItem.sourceLine);
90 const column = Number(detailsItem.sourceColumn);
Paul Lewisdaac1062020-03-05 14:37:10 +000091 const element = await Components.Linkifier.Linkifier.linkifyURL(
92 url, {lineNumber: line, column, maxLength: MaxLengthForLinks});
Connor Clark0403a422019-11-18 18:03:18 -080093 origElement.title = '';
94 origElement.textContent = '';
95 origElement.appendChild(element);
96 }
97 }
98
99 /**
100 * @param {!Element} el
101 */
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000102 static handleDarkMode(el) {
Paul Lewis93d8e2c2020-01-24 16:34:55 +0000103 if (self.UI.themeSupport.themeName() === 'dark') {
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000104 el.classList.add('dark');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000105 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000106 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000107}
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000108
109/**
110 * @override
111 */
Connor Clark2bc3be22020-02-14 14:34:19 -0800112export class LighthouseReportUIFeatures extends ReportUIFeatures {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000113 /**
Connor Clark99508362019-08-20 19:52:23 +0000114 * @param {!DOM} dom
115 */
116 constructor(dom) {
117 super(dom);
118 this._beforePrint = null;
119 this._afterPrint = null;
120 }
121
122 /**
123 * @param {?function()} beforePrint
124 */
125 setBeforePrint(beforePrint) {
126 this._beforePrint = beforePrint;
127 }
128
129 /**
130 * @param {?function()} afterPrint
131 */
132 setAfterPrint(afterPrint) {
133 this._afterPrint = afterPrint;
134 }
135
136 /**
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000137 * Returns the html that recreates this report.
138 * @return {string}
139 * @protected
140 */
141 getReportHtml() {
142 this.resetUIState();
143 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
144 }
145
146 /**
147 * Downloads a file (blob) using the system dialog prompt.
148 * @param {!Blob|!File} blob The file to save.
149 */
150 async _saveFile(blob) {
Paul Lewisdaac1062020-03-05 14:37:10 +0000151 const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000152 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
Simon Zünd2c704cd2020-06-04 11:08:35 +0200153 const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime));
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000154 const ext = blob.type.match('json') ? '.json' : '.html';
155 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
156 const text = await blob.text();
Paul Lewisdff48e42020-01-24 11:46:48 +0000157 self.Workspace.fileManager.save(basename, text, true /* forceSaveAs */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000158 }
159
160 async _print() {
161 const document = this.getDocument();
162 const clonedReport = document.querySelector('.lh-root').cloneNode(true /* deep */);
163 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
164 const style = printWindow.document.createElement('style');
Tim van der Lippe6d51bf02020-03-18 12:15:14 +0000165 style.textContent = self.Runtime.cachedResources['third_party/lighthouse/report-assets/report.css'];
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000166 printWindow.document.head.appendChild(style);
167 printWindow.document.body.replaceWith(clonedReport);
168 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
Connor Clark2bc3be22020-02-14 14:34:19 -0800169 await LighthouseReportRenderer.linkifyNodeDetails(clonedReport);
Connor Clark99508362019-08-20 19:52:23 +0000170
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000171 if (this._beforePrint) {
Connor Clark99508362019-08-20 19:52:23 +0000172 this._beforePrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000173 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000174 printWindow.focus();
175 printWindow.print();
176 printWindow.close();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000177 if (this._afterPrint) {
Connor Clark99508362019-08-20 19:52:23 +0000178 this._afterPrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000179 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000180 }
181
182 /**
183 * @suppress {visibility}
184 * @return {!Document}
185 */
186 getDocument() {
187 return this._document;
188 }
189
190 /**
191 * @suppress {visibility}
192 */
193 resetUIState() {
194 this._resetUIState();
195 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000196}