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

core(audit): make dom-size table prettier #6065

Merged
merged 7 commits into from Sep 20, 2018
41 changes: 26 additions & 15 deletions lighthouse-core/audits/dobetterweb/dom-size.js
Expand Up @@ -32,17 +32,23 @@ const UIStrings = {
'children/parent element. A large DOM can increase memory usage, cause longer ' +
'[style calculations](https://developers.google.com/web/fundamentals/performance/rendering/reduce-the-scope-and-complexity-of-style-calculations), ' +
'and produce costly [layout reflows](https://developers.google.com/speed/articles/reflow). [Learn more](https://developers.google.com/web/tools/lighthouse/audits/dom-size).',
/** Label for the total number of DOM nodes found in the page. */
columnDOMNodes: 'Total DOM Nodes',
/** Label for the numeric value of the maximum depth in the page's DOM tree. */
columnDOMDepth: 'Maximum DOM Depth',
/** Label for the value of the maximum number of children any DOM node in the page has. */
columnDOMWidth: 'Maximum Children',
/** Table column header for the type of statistic. These statistics describe how big the DOM is (count of DOM nodes, children, depth). */
columnCategory: 'Category',
Copy link
Member

Choose a reason for hiding this comment

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

bikeshed. wdyt about 'Statistic' or 'Metric'. cc @brendankenny @patrickhulce

Copy link
Member

Choose a reason for hiding this comment

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

I like either of those, maybe Metric more? Agreed they're slightly more descriptive than Category

Copy link
Member Author

Choose a reason for hiding this comment

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

I took the Category name from the Minimizes main thread work Audit table. So we can update the phrasing in every table if we like Metric more (which I agree is more descriptive than Category).

category_screenshot

Copy link
Member

Choose a reason for hiding this comment

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

heh, in that case I feel like "Category" is better (since it's actually bringing e.g. multiple Script Evaluation tasks together and grouping them into one line), but also see the appeal of consistency

exterkamp marked this conversation as resolved.
Show resolved Hide resolved
/** Table column header for the DOM element. Each DOM element is described with its HTML representation. */
columnElement: 'Element',
/** Table column header for the observed value of the DOM statistic. */
columnObserved: 'Observed',
exterkamp marked this conversation as resolved.
Show resolved Hide resolved
/** [ICU Syntax] Label for an audit identifying the number of DOM nodes found in the page. */
displayValue: `{itemCount, plural,
=1 {1 node}
other {# nodes}
}`,
/** Label for the total number of DOM nodes found in the page. */
categoryDOMNodes: 'Total DOM Nodes',
/** Label for the numeric value of the maximum depth in the page's DOM tree. */
categoryDOMDepth: 'Maximum DOM Depth',
/** Label for the numeric value of the maximum number of children any DOM node in the page has. The element described will have the most children in the page. */
categoryDOMWidth: 'Maximum Children',
exterkamp marked this conversation as resolved.
Show resolved Hide resolved
};

const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
Expand Down Expand Up @@ -96,28 +102,33 @@ class DOMSize extends Audit {
);

const headings = [
{key: 'totalNodes', itemType: 'text', text: str_(UIStrings.columnDOMNodes)},
{key: 'depth', itemType: 'text', text: str_(UIStrings.columnDOMDepth)},
{key: 'width', itemType: 'text', text: str_(UIStrings.columnDOMWidth)},
{key: 'category', itemType: 'text', text: str_(UIStrings.columnCategory)},
{key: 'element', itemType: 'code', text: str_(UIStrings.columnElement)},
{key: 'observed', itemType: 'text', text: str_(UIStrings.columnObserved)},
];

/** @type {Array<Object<string, LH.Audit.DetailsItem>>} */
const items = [
{
totalNodes: Util.formatNumber(stats.totalDOMNodes),
depth: Util.formatNumber(stats.depth.max),
width: Util.formatNumber(stats.width.max),
category: str_(UIStrings.categoryDOMNodes),
element: '',
observed: Util.formatNumber(stats.totalDOMNodes),
},
{
totalNodes: '',
depth: {
category: str_(UIStrings.categoryDOMDepth),
element: {
type: 'code',
value: stats.depth.snippet,
},
width: {
observed: Util.formatNumber(stats.depth.max),
},
{
category: str_(UIStrings.categoryDOMWidth),
element: {
type: 'code',
value: stats.width.snippet,
},
observed: Util.formatNumber(stats.width.max),
},
];

Expand Down
5 changes: 3 additions & 2 deletions lighthouse-core/gather/gatherers/dobetterweb/domstats.js
Expand Up @@ -123,12 +123,13 @@ function getDOMStats(element, deep=true) {
depth: {
max: result.maxDepth,
pathToElement: elementPathInDOM(deepestNode),
snippet: getOuterHTMLSnippet(deepestNode),
// ignore style since it will provide no additional context, and is often long
snippet: getOuterHTMLSnippet(deepestNode, ['style']),
},
width: {
max: result.maxWidth,
pathToElement: elementPathInDOM(parentWithMostChildren),
snippet: getOuterHTMLSnippet(parentWithMostChildren),
snippet: getOuterHTMLSnippet(parentWithMostChildren, ['style']),
},
};
}
Expand Down
18 changes: 15 additions & 3 deletions lighthouse-core/lib/locales/en-US.json
Expand Up @@ -159,18 +159,30 @@
"message": "Minimize Critical Requests Depth",
"description": "Imperative title of a Lighthouse audit that tells the user to reduce the depth of critical network requests to enhance initial load of a page. Critical request chains are series of dependent network requests that are important for page rendering. For example, here's a 4-request-deep chain: The biglogo.jpg image is required, but is requested via the styles.css style code, which is requested by the initialize.js javascript, which is requested by the page's HTML. This is displayed in a list of audit titles that Lighthouse generates."
},
"lighthouse-core/audits/dobetterweb/dom-size.js | columnDOMDepth": {
"lighthouse-core/audits/dobetterweb/dom-size.js | categoryDOMDepth": {
"message": "Maximum DOM Depth",
"description": "Label for the numeric value of the maximum depth in the page's DOM tree."
},
"lighthouse-core/audits/dobetterweb/dom-size.js | columnDOMNodes": {
"lighthouse-core/audits/dobetterweb/dom-size.js | categoryDOMNodes": {
"message": "Total DOM Nodes",
"description": "Label for the total number of DOM nodes found in the page."
},
"lighthouse-core/audits/dobetterweb/dom-size.js | columnDOMWidth": {
"lighthouse-core/audits/dobetterweb/dom-size.js | categoryDOMWidth": {
"message": "Maximum Children",
"description": "Label for the value of the maximum number of children any DOM node in the page has."
},
"lighthouse-core/audits/dobetterweb/dom-size.js | columnCategory": {
"message": "Category",
"description": "Table column header for the type of analytic found."
},
"lighthouse-core/audits/dobetterweb/dom-size.js | columnElement": {
"message": "Element",
"description": "Table column header for DOM Element's snippet."
},
"lighthouse-core/audits/dobetterweb/dom-size.js | columnObserved": {
"message": "Observed",
"description": "Table column header for the observed analytic's value."
},
"lighthouse-core/audits/dobetterweb/dom-size.js | description": {
"message": "Browser engineers recommend pages contain fewer than ~1,500 DOM nodes. The sweet spot is a tree depth < 32 elements and fewer than 60 children/parent element. A large DOM can increase memory usage, cause longer [style calculations](https://developers.google.com/web/fundamentals/performance/rendering/reduce-the-scope-and-complexity-of-style-calculations), and produce costly [layout reflows](https://developers.google.com/speed/articles/reflow). [Learn more](https://developers.google.com/web/tools/lighthouse/audits/dom-size).",
"description": "Description of a Lighthouse audit that tells the user *why* they should reduce the size of the web page's DOM. The size of a DOM is characterized by the total number of DOM nodes and greatest DOM depth. This is displayed after a user expands the section to see more. No character length limits. 'Learn More' becomes link text to additional documentation."
Expand Down
13 changes: 11 additions & 2 deletions lighthouse-core/lib/page-functions.js
Expand Up @@ -107,12 +107,20 @@ function getElementsInDocument(selector) {
/**
* Gets the opening tag text of the given node.
exterkamp marked this conversation as resolved.
Show resolved Hide resolved
* @param {Element} element
* @param {Array<string>=} ignoreAttrs Optionally provides an array of attribute tags to not include in HTML the snippet
exterkamp marked this conversation as resolved.
Show resolved Hide resolved
* @return {string}
*/
/* istanbul ignore next */
function getOuterHTMLSnippet(element) {
function getOuterHTMLSnippet(element, ignoreAttrs=[]) {
const clone = element.cloneNode();

ignoreAttrs.forEach(attribute =>{
clone.removeAttribute(attribute);
});

const reOpeningTag = /^.*?>/;
const match = element.outerHTML.match(reOpeningTag);
const match = clone.outerHTML.match(reOpeningTag);

return (match && match[0]) || '';
}

Expand Down Expand Up @@ -150,6 +158,7 @@ module.exports = {
checkTimeSinceLastLongTaskString: checkTimeSinceLastLongTask.toString(),
getElementsInDocumentString: getElementsInDocument.toString(),
getOuterHTMLSnippetString: getOuterHTMLSnippet.toString(),
getOuterHTMLSnippet: getOuterHTMLSnippet,
ultradumbBenchmark: ultradumbBenchmark,
ultradumbBenchmarkString: ultradumbBenchmark.toString(),
};
6 changes: 3 additions & 3 deletions lighthouse-core/test/audits/dobetterweb/dom-size-test.js
Expand Up @@ -26,9 +26,9 @@ describe('Num DOM nodes audit', () => {
assert.equal(auditResult.score, 0.43);
assert.equal(auditResult.rawValue, numNodes);
expect(auditResult.displayValue).toBeDisplayString('1,500 nodes');
assert.equal(auditResult.details.items[0].totalNodes, numNodes.toLocaleString());
assert.equal(auditResult.details.items[0].depth, '1');
assert.equal(auditResult.details.items[0].width, '2');
assert.equal(auditResult.details.items[0].observed, numNodes.toLocaleString());
assert.equal(auditResult.details.items[1].observed, '1');
assert.equal(auditResult.details.items[2].observed, '2');
});

it('calculates score hitting top distribution', () => {
Expand Down
53 changes: 53 additions & 0 deletions lighthouse-core/test/lib/page-functions-test.js
@@ -0,0 +1,53 @@
/**
* @license Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const assert = require('assert');
const jsdom = require('jsdom');
const DOM = require('../../report/html/renderer/dom.js');
const pageFunctions = require('../../lib/page-functions');

/* eslint-env jest */

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

beforeAll(() => {
const document = jsdom.jsdom();
dom = new DOM(document);
});

afterAll(() => {
exterkamp marked this conversation as resolved.
Show resolved Hide resolved
global.URL = undefined;
global.Util = undefined;
});

describe('get outer HTML snippets', () => {
it('gets full HTML snippet', () => {
assert.equal(pageFunctions.getOuterHTMLSnippet(
dom.createElement('div', '', {id: '1', style: 'style'})), '<div id="1" style="style">');
});

it('removes a specific attribute', () => {
assert.equal(pageFunctions.getOuterHTMLSnippet(
dom.createElement('div', '', {id: '1', style: 'style'}), ['style']), '<div id="1">');
});

it('removes multiple attributes', () => {
assert.equal(pageFunctions.getOuterHTMLSnippet(
dom.createElement('div', '', {'id': '1', 'style': 'style', 'aria-label': 'label'}),
['style', 'aria-label']
), '<div id="1">');
});

it('ignores when attribute not found', () => {
assert.equal(pageFunctions.getOuterHTMLSnippet(
dom.createElement('div', '', {'id': '1', 'style': 'style', 'aria-label': 'label'}),
['style-missing', 'aria-label-missing']
), '<div id="1" style="style" aria-label="label">');
});
});
});
48 changes: 31 additions & 17 deletions lighthouse-core/test/results/sample_v2.json
Expand Up @@ -2190,35 +2190,40 @@
"type": "table",
"headings": [
{
"key": "totalNodes",
"key": "category",
"itemType": "text",
"text": "Total DOM Nodes"
"text": "Category"
},
{
"key": "depth",
"itemType": "text",
"text": "Maximum DOM Depth"
"key": "element",
"itemType": "code",
"text": "Element"
},
{
"key": "width",
"key": "observed",
"itemType": "text",
"text": "Maximum Children"
"text": "Observed"
}
],
"items": [
{
"totalNodes": "53",
"depth": "4",
"width": "22"
"category": "Total DOM Nodes",
"element": "",
"observed": "53"
},
{
"totalNodes": "",
"depth": {
"category": "Maximum DOM Depth",
"element": {
"type": "code"
},
"width": {
"observed": "4"
},
{
"category": "Maximum Children",
"element": {
"type": "code"
}
},
"observed": "22"
}
]
}
Expand Down Expand Up @@ -3756,15 +3761,24 @@
"path": "audits[dom-size].displayValue"
}
],
"lighthouse-core/audits/dobetterweb/dom-size.js | columnDOMNodes": [
"lighthouse-core/audits/dobetterweb/dom-size.js | columnCategory": [
"audits[dom-size].details.headings[0].text"
],
"lighthouse-core/audits/dobetterweb/dom-size.js | columnDOMDepth": [
"lighthouse-core/audits/dobetterweb/dom-size.js | columnElement": [
"audits[dom-size].details.headings[1].text"
],
"lighthouse-core/audits/dobetterweb/dom-size.js | columnDOMWidth": [
"lighthouse-core/audits/dobetterweb/dom-size.js | columnObserved": [
"audits[dom-size].details.headings[2].text"
],
"lighthouse-core/audits/dobetterweb/dom-size.js | categoryDOMNodes": [
"audits[dom-size].details.items[0].category"
],
"lighthouse-core/audits/dobetterweb/dom-size.js | categoryDOMDepth": [
"audits[dom-size].details.items[1].category"
],
"lighthouse-core/audits/dobetterweb/dom-size.js | categoryDOMWidth": [
"audits[dom-size].details.items[2].category"
],
"lighthouse-core/config/default-config.js | performanceCategoryTitle": [
"categories.performance.title"
],
Expand Down