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-display): more accurately follow CSS spec #7191

Merged
merged 4 commits into from
Feb 12, 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
8 changes: 5 additions & 3 deletions lighthouse-core/audits/font-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const Audit = require('./audit');
const URL = require('../lib/url-shim').URL;
const PASSING_FONT_DISPLAY_REGEX = /block|fallback|optional|swap/;
const PASSING_FONT_DISPLAY_REGEX = /^(block|fallback|optional|swap)$/;
const CSS_URL_REGEX = /url\((.*?)\)/;
const CSS_URL_GLOBAL_REGEX = new RegExp(CSS_URL_REGEX, 'g');
const i18n = require('../lib/i18n/i18n.js');
Expand Down Expand Up @@ -57,11 +57,13 @@ class FontDisplay extends Audit {
const fontFaceDeclarations = newlinesStripped.match(/@font-face\s*{(.*?)}/g) || [];
// Go through all the @font-face declarations to find a declared `font-display: ` property
for (const declaration of fontFaceDeclarations) {
const rawFontDisplay = declaration.match(/font-display:(.*?);/);
// Find the font-display value by matching a single token, optionally surrounded by whitespace,
// followed either by a semicolon or the end of a block.
const rawFontDisplay = declaration.match(/font-display\s*:\s*(\w+)\s*(;|\})/);
// If they didn't have a font-display property, it's the default, and it's failing; bail
if (!rawFontDisplay) continue;
// If they don't have one of the passing font-display values, it's failing; bail
const hasPassingFontDisplay = PASSING_FONT_DISPLAY_REGEX.test(rawFontDisplay[0]);
const hasPassingFontDisplay = PASSING_FONT_DISPLAY_REGEX.test(rawFontDisplay[1]);
if (!hasPassingFontDisplay) continue;

// If it's passing, we'll try to find the URL it's referencing.
Expand Down
56 changes: 45 additions & 11 deletions lighthouse-core/test/audits/font-display-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ describe('Performance: Font Display audit', () => {
}

@font-face {
font-display: 'optional'; // invalid quotes, should still fail for being invalid rule
/* try up a directory with ' */
src: url('../font-b.woff');
}

@font-face {
font-display: optional // missing a semi-colon, should still fail for being invalid block
/* try no path with no quotes ' */
src: url(font.woff);
}
Expand Down Expand Up @@ -72,32 +74,32 @@ describe('Performance: Font Display audit', () => {
{url: networkRecords[2].url, wastedMs: 1000},
];
assert.strictEqual(result.rawValue, false);
assert.deepEqual(result.details.items, items);
expect(result.details.items).toEqual(items);
});

it('resolves URLs relative to stylesheet URL when available', async () => {
stylesheet.header.sourceURL = 'https://cdn.example.com/foo/bar/file.css';
stylesheet.content = `
@font-face {
font-display: 'block';
font-display: block;
Copy link
Member

Choose a reason for hiding this comment

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

ha, whoops

/* try with " and same directory */
src: url("./font-a.woff");
}

@font-face {
font-display: 'block';
font-display: block;
/* try with " and site origin */
src: url("https://example.com/foo/bar/document-font.woff");
}

@font-face {
font-display: 'fallback';
font-display: fallback;
/* try up a directory with ' */
src: url('../font-b.woff');
}

@font-face {
font-display: 'optional';
font-display: optional;
/* try no path with no quotes ' */
src: url(font.woff);
}
Expand Down Expand Up @@ -128,25 +130,25 @@ describe('Performance: Font Display audit', () => {

const result = await FontDisplayAudit.audit(getArtifacts(), context);
assert.strictEqual(result.rawValue, true);
assert.deepEqual(result.details.items, []);
expect(result.details.items).toEqual([]);
});

it('passes when all fonts have a correct font-display rule', async () => {
stylesheet.content = `
@font-face {
font-display: 'block';
font-display: block;
/* try with " */
src: url("./font-a.woff");
}

@font-face {
font-display: 'fallback';
font-display: fallback;
/* try up a directory with ' */
src: url('../font-b.woff');
}

@font-face {
font-display: 'optional';
font-display: optional;
/* try no path with no quotes ' */
src: url(font.woff);
}
Expand All @@ -172,7 +174,7 @@ describe('Performance: Font Display audit', () => {

const result = await FontDisplayAudit.audit(getArtifacts(), context);
assert.strictEqual(result.rawValue, true);
assert.deepEqual(result.details.items, []);
expect(result.details.items).toEqual([]);
});

it('should handle real-world font-face declarations', async () => {
Expand Down Expand Up @@ -212,12 +214,44 @@ describe('Performance: Font Display audit', () => {

const result = await FontDisplayAudit.audit(getArtifacts(), context);
assert.strictEqual(result.rawValue, false);
assert.deepEqual(result.details.items.map(item => item.url), [
expect(result.details.items.map(item => item.url)).toEqual([
'https://edition.i.cdn.cnn.com/.a/fonts/cnn/3.7.2/cnnclock-black.woff2',
'https://registry.api.cnn.io/assets/fave/fonts/2.0.15/cnnsans-bold.woff',
// FontAwesome should pass
// 'https://example.com/foo/fonts/fontawesome-webfont.woff2?v=4.6.1',
'https://fonts.gstatic.com/s/lato/v14/S6u9w4BMUTPHh50XSwiPGQ3q5d0.woff2',
]);
});

it('handles varied font-display declarations', async () => {
stylesheet.content = `
@font-face {
src: url(font-0.woff);
font-display: swap
}

@font-face {src: url(font-1.woff);font-display : fallback
}

@font-face {
src: url(font-2.woff);
font-display:optional}

@font-face {
src: url(font-3.woff);
font-display : swap ; }

@font-face {src: url(font-4.woff);font-display:swap;}
`;

networkRecords = Array.from({length: 5}).map((_, idx) => ({
url: `https://example.com/foo/bar/font-${idx}.woff`,
endTime: 2, startTime: 1,
resourceType: 'Font',
}));

const result = await FontDisplayAudit.audit(getArtifacts(), context);
expect(result.details.items).toEqual([]);
assert.strictEqual(result.rawValue, true);
});
});