Skip to content

Commit

Permalink
give up
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Mar 14, 2017
1 parent e6df2e5 commit f61f42d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
Expand Up @@ -39,10 +39,7 @@ module.exports = [
extendedInfo: {
value: {
results: {
length: 4,
1: {
isCrossOrigin: true
}
length: 4
}
}
}
Expand Down
Expand Up @@ -79,7 +79,7 @@ class OptimizedImages extends Gatherer {

seenUrls.add(record._url);
const isOptimizableImage = /image\/(png|bmp|jpeg)/.test(record._mimeType);
const isSameOrigin = URL.hostsMatch(pageUrl, record._url);
const isSameOrigin = URL.originsMatch(pageUrl, record._url);
const isBase64DataUri = /^data:.{2,40}base64\s*,/.test(record._url);

if (isOptimizableImage) {
Expand Down
13 changes: 13 additions & 0 deletions lighthouse-core/lib/url-shim.js
Expand Up @@ -59,6 +59,19 @@ URL.hostsMatch = function hostsMatch(urlA, urlB) {
}
};

/**
* @param {string} urlA
* @param {string} urlB
* @return {boolean}
*/
URL.originsMatch = function originsMatch(urlA, urlB) {
try {
return new URL(urlA).origin === new URL(urlB).origin;
} catch (e) {
return false;
}
};

/**
* @param {string} url
* @param {{numPathParts: number, preserveQuery: boolean, preserveHost: boolean}=} options
Expand Down
18 changes: 18 additions & 0 deletions lighthouse-core/test/lib/url-shim-test.js
Expand Up @@ -58,6 +58,24 @@ describe('URL Shim', () => {
assert.equal(URL.hostsMatch(urlA, urlB), false);
});

it('safely identifies same origins', () => {
const urlA = 'https://5321212.fls.net/page?query=string#hash';
const urlB = 'https://5321212.fls.net/deeply/nested/page';
assert.ok(URL.originsMatch(urlA, urlB));
});

it('safely identifies different origins', () => {
const urlA = 'https://5321212.fls.net/page?query=string#hash';
const urlB = 'http://5321212.fls.net/deeply/nested/page';
assert.equal(URL.originsMatch(urlA, urlB), false);
});

it('safely identifies different invalid origins', () => {
const urlA = 'https://google.com/page?query=string#hash';
const urlB = 'anonymous:90';
assert.equal(URL.originsMatch(urlA, urlB), false);
});

describe('getDisplayName', () => {
it('respects numPathParts option', () => {
const url = 'http://example.com/a/deep/nested/file.css';
Expand Down

0 comments on commit f61f42d

Please sign in to comment.