diff --git a/lighthouse-core/report/html/renderer/report-renderer.js b/lighthouse-core/report/html/renderer/report-renderer.js index c6011e3e23f9..1892a8ccb1e9 100644 --- a/lighthouse-core/report/html/renderer/report-renderer.js +++ b/lighthouse-core/report/html/renderer/report-renderer.js @@ -40,30 +40,17 @@ class ReportRenderer { } /** - * @param {LH.ReportResult} report + * @param {LH.ReportResult} result * @param {Element} container Parent element to render the report into. */ - renderReport(report, container) { - // If any mutations happen to the report within the renderers, we want the original object untouched - const clone = /** @type {LH.ReportResult} */ (JSON.parse(JSON.stringify(report))); + renderReport(result, container) { // Mutate the UIStrings if necessary (while saving originals) const originalUIStrings = JSON.parse(JSON.stringify(Util.UIStrings)); - // If LHR is older (≤3.0.3), it has no locale setting. Set default. - if (!clone.configSettings.locale) { - clone.configSettings.locale = 'en-US'; - } - Util.setNumberDateLocale(clone.configSettings.locale); - if (clone.i18n && clone.i18n.rendererFormattedStrings) { - ReportRenderer.updateAllUIStrings(clone.i18n.rendererFormattedStrings); - } - // TODO(phulce): we all agree this is technical debt we should fix - if (typeof clone.categories !== 'object') throw new Error('No categories provided.'); - clone.reportCategories = Object.values(clone.categories); - ReportRenderer.smooshAuditResultsIntoCategories(clone.audits, clone.reportCategories); + const report = Util.prepareReportResult(result); container.textContent = ''; // Remove previous report. - container.appendChild(this._renderReport(clone)); + container.appendChild(this._renderReport(report)); // put the UIStrings back into original state ReportRenderer.updateAllUIStrings(originalUIStrings); @@ -230,20 +217,6 @@ class ReportRenderer { return reportFragment; } - /** - * Place the AuditResult into the auditDfn (which has just weight & group) - * @param {Object} audits - * @param {Array} reportCategories - */ - static smooshAuditResultsIntoCategories(audits, reportCategories) { - for (const category of reportCategories) { - category.auditRefs.forEach(auditMeta => { - const result = audits[auditMeta.id]; - auditMeta.result = result; - }); - } - } - /** * @param {LH.I18NRendererStrings} rendererFormattedStrings */ diff --git a/lighthouse-core/report/html/renderer/util.js b/lighthouse-core/report/html/renderer/util.js index e34c24ebc8d8..68fd2df836b4 100644 --- a/lighthouse-core/report/html/renderer/util.js +++ b/lighthouse-core/report/html/renderer/util.js @@ -38,6 +38,55 @@ class Util { return `%10d${NBSP}ms`; } + /** + * + * @param {LH.Result} result + * @return {LH.ReportResult} + */ + static prepareReportResult(result) { + // If any mutations happen to the report within the renderers, we want the original object untouched + const clone = /** @type {LH.ReportResult} */ (JSON.parse(JSON.stringify(result))); + + // If LHR is older (≤3.0.3), it has no locale setting. Set default. + if (!clone.configSettings.locale) { + clone.configSettings.locale = 'en-US'; + } + Util.setNumberDateLocale(clone.configSettings.locale); + if (clone.i18n && clone.i18n.rendererFormattedStrings) { + Util.rendererFormattedStrings = clone.i18n.rendererFormattedStrings; + } + + // TODO(phulce): we all agree this is technical debt we should fix + if (typeof clone.categories !== 'object') throw new Error('No categories provided.'); + clone.reportCategories = Object.values(clone.categories); + Util.smooshAuditResultsIntoCategories(clone.audits, clone.reportCategories); + return clone; + } + + /** + * Place the AuditResult into the auditDfn (which has just weight & group) + * @param {Object} audits + * @param {Array} reportCategories + */ + static smooshAuditResultsIntoCategories(audits, reportCategories) { + for (const category of reportCategories) { + category.auditRefs.forEach(auditMeta => { + const result = audits[auditMeta.id]; + auditMeta.result = result; + }); + } + } + + /** + * @param {LH.I18NRendererStrings} rendererFormattedStrings + */ + static updateAllUIStrings(rendererFormattedStrings) { + // TODO(i18n): don't mutate these here but on the LHR and pass that around everywhere + for (const [key, value] of Object.entries(rendererFormattedStrings)) { + Util.UIStrings[key] = value; + } + } + /** * @param {string|Array=} displayValue * @return {string}