Skip to content

Commit

Permalink
Avoid splitting node labels in the middle of unicode surrogate pairs. F…
Browse files Browse the repository at this point in the history
…ixes #11697
  • Loading branch information
wildlyinaccurate committed Nov 23, 2020
1 parent 5608cbd commit 32afa11
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lighthouse-core/lib/page-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ function getNodeLabel(node) {
if (str.length <= maxLength) {
return str;
}
return str.slice(0, maxLength - 1) + '…';
return Array.from(str).slice(0, maxLength - 1).join('') + '…';
}
const tagName = node.tagName.toLowerCase();
// html and body content is too broad to be useful, since they contain all page content
Expand Down
6 changes: 6 additions & 0 deletions lighthouse-core/test/lib/page-functions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ describe('Page Functions', () => {
assert.equal(pageFunctions.getNodeLabel(el).length, 80);
});

it('Truncates long text containing unicode surrogate pairs', () => {
const el = dom.createElement('div');
el.innerText = Array(78).fill('a').join('') + '💡💡💡';
assert.equal(pageFunctions.getNodeLabel(el), Array(78).fill('a').join('') + '💡…');
});

it('Uses tag name for html tags', () => {
const el = dom.createElement('html');
assert.equal(pageFunctions.getNodeLabel(el), 'html');
Expand Down

0 comments on commit 32afa11

Please sign in to comment.