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(font-size): precise text length calculation #6973

Merged
merged 4 commits into from
Jan 17, 2019
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
3 changes: 2 additions & 1 deletion lighthouse-core/gather/gatherers/seo/font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ function getEffectiveFontRule({inlineStyle, matchedCSSRules, inherited}) {
* @returns {number}
*/
function getNodeTextLength(node) {
return !node.nodeValue ? 0 : node.nodeValue.trim().length;
// Array.from to count symbols not unicode code points. See: #6973
return !node.nodeValue ? 0 : Array.from(node.nodeValue.trim()).length;
Copy link
Member

Choose a reason for hiding this comment

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

Can we get a comment mentioning this PR or explaining why this is here. e.g.

// Use Array.from in order to more accurately count unicode characters. See: #6973

Maybe even add a TODO for @patrickhulce's comment on future rabbit hole exploring.

}

/**
Expand Down
13 changes: 7 additions & 6 deletions lighthouse-core/test/gather/gatherers/seo/font-size-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
const FontSizeGather = require('../../../../gather/gatherers/seo/font-size');
let fontSizeGather;

const smallText = ' body small text ';
const bigText = 'body big text';
const failingText = 'failing text';
const smallText = ' body sm𝐀ll text ';
const bigText = 'body 𝐁ig text';
const failingText = 'failing text 💩';
const bodyNode = {nodeId: 3, nodeName: 'BODY', parentId: 1};
const failingNode = {nodeId: 10, nodeName: 'P', parentId: 3};
const nodes = [
Expand Down Expand Up @@ -93,9 +93,10 @@ describe('Font size gatherer', () => {
};

const artifact = await fontSizeGather.afterPass({driver});
const expectedFailingTextLength = smallText.trim().length;
const expectedVisitedTextLength = bigText.trim().length + expectedFailingTextLength;
const expectedTotalTextLength = failingText.trim().length + expectedVisitedTextLength;
const expectedFailingTextLength = Array.from(smallText.trim()).length;
const expectedVisitedTextLength = Array.from(bigText.trim()).length + expectedFailingTextLength;
const expectedTotalTextLength = Array.from(failingText.trim()).length +
expectedVisitedTextLength;
const expectedAnalyzedFailingTextLength = expectedFailingTextLength;

expect(artifact).toEqual({
Expand Down