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: truncate long attribute values in HTML snippets #10984

Merged
merged 9 commits into from
Jul 15, 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
24 changes: 21 additions & 3 deletions lighthouse-core/lib/page-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ function getElementsInDocument(selector) {
* @return {string}
*/
/* istanbul ignore next */
function getOuterHTMLSnippet(element, ignoreAttrs = []) {
function getOuterHTMLSnippet(element, ignoreAttrs = [], snippetCharacterLimit = 500) {
const ATTRIBUTE_CHAR_LIMIT = 75;
try {
// ShadowRoots are sometimes passed in; use their hosts' outerHTML.
if (element instanceof ShadowRoot) {
Expand All @@ -123,9 +124,26 @@ function getOuterHTMLSnippet(element, ignoreAttrs = []) {
ignoreAttrs.forEach(attribute =>{
clone.removeAttribute(attribute);
});
let charCount = 0;
for (const attributeName of clone.getAttributeNames()) {
if (charCount > snippetCharacterLimit) {
clone.removeAttribute(attributeName);
} else {
let attributeValue = clone.getAttribute(attributeName);
if (attributeValue.length > ATTRIBUTE_CHAR_LIMIT) {
attributeValue = attributeValue.slice(0, ATTRIBUTE_CHAR_LIMIT - 1) + '…';
clone.setAttribute(attributeName, attributeValue);
}
charCount += attributeName.length + attributeValue.length;
}
}

const reOpeningTag = /^[\s\S]*?>/;
const match = clone.outerHTML.match(reOpeningTag);
return (match && match[0]) || '';
const [match] = clone.outerHTML.match(reOpeningTag) || [];
if (match && charCount > snippetCharacterLimit) {
return match.slice(0, match.length - 1) + ' …>';
}
return match || '';
} catch (_) {
// As a last resort, fall back to localName.
return `<${element.localName}>`;
Expand Down
22 changes: 22 additions & 0 deletions lighthouse-core/test/lib/page-functions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ describe('Page Functions', () => {
assert.equal(pageFunctions.getOuterHTMLSnippet(
dom.createElement('div', '', {style: 'style1\nstyle2'})), '<div style="style1\nstyle2">');
});

it('truncates attribute values that are too long', () => {
const longClass = 'a'.repeat(200);
const truncatedExpectation = 'a'.repeat(74) + '…';
assert.equal(pageFunctions.getOuterHTMLSnippet(
dom.createElement('div', '', {class: longClass})), `<div class="${truncatedExpectation}">`
);
});

it('removes attributes if the length of the attribute name + value is too long', () => {
const longValue = 'a'.repeat(200);
const truncatedValue = 'a'.repeat(74) + '…';
const element = dom.createElement('div', '', {
class: longValue,
id: longValue,
att1: 'shouldn\'t see this',
att2: 'shouldn\'t see this either',
});
const snippet = pageFunctions.getOuterHTMLSnippet(element, [], 150);
assert.equal(snippet, `<div class="${truncatedValue}" id="${truncatedValue}" …>`
);
});
});

describe('getNodeSelector', () => {
Expand Down