Skip to content

Commit

Permalink
core(font-display): do not use invalid sourceURLs (#8535)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored and brendankenny committed Apr 26, 2019
1 parent 9bb7915 commit a527587
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lighthouse-core/audits/font-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

const Audit = require('./audit');
const URL = require('../lib/url-shim').URL;
const URL = require('../lib/url-shim');
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');
Expand Down Expand Up @@ -82,11 +82,11 @@ class FontDisplay extends Audit {

return s;
});

// Convert the relative CSS URL to an absolute URL and add it to the passing set
for (const relativeURL of relativeURLs) {
try {
const relativeRoot = stylesheet.header.sourceURL || artifacts.URL.finalUrl;
const relativeRoot = URL.isValid(stylesheet.header.sourceURL) ?
stylesheet.header.sourceURL : artifacts.URL.finalUrl;
const absoluteURL = new URL(relativeURL, relativeRoot);
passingURLs.add(absoluteURL.href);
} catch (err) {
Expand Down
21 changes: 21 additions & 0 deletions lighthouse-core/test/audits/font-display-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,25 @@ describe('Performance: Font Display audit', () => {
expect(result.details.items).toEqual([]);
assert.strictEqual(result.score, 1);
});

it('handles custom source URLs from sourcemaps', async () => {
// Make sure we don't use sourceURL when it's not a valid URL, see https://github.com/GoogleChrome/lighthouse/issues/8534
stylesheet.header.sourceURL = 'custom-url-from-source-map';
stylesheet.content = `
@font-face {
src: url(font-0.woff);
font-display: swap
}
`;

networkRecords = [{
url: `https://example.com/foo/bar/font-0.woff`,
endTime: 2, startTime: 1,
resourceType: 'Font',
}];

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

0 comments on commit a527587

Please sign in to comment.