Skip to content

Commit

Permalink
Merge pull request kintesh#110 from LoveIsGrief/fix-matchdomainonly-w…
Browse files Browse the repository at this point in the history
…ithout-host-prefix

Fix matchDomainOnly without host prefix
  • Loading branch information
kintesh authored Oct 23, 2019
2 parents 90bbee3 + fcf7271 commit 406631d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
9 changes: 8 additions & 1 deletion src/Storage/HostStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ class HostStorage extends PrefixStorage {
return super.getAll().then(maps => {
const sorted = sortMaps(Object.keys(maps).map(key => maps[key]));
// Sorts by domain length, then by path length
return sorted.find(matchesSavedMap.bind(null, url, matchDomainOnly)) || {};
return sorted.find((map) => {
try{
return matchesSavedMap( url, matchDomainOnly, map);
} catch (e) {
console.error('Error matching maps', map, url, matchDomainOnly, e);
return false;
}
}) || {};
});
}

Expand Down
61 changes: 61 additions & 0 deletions src/__tests__/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,65 @@ describe('utils', () => {

});

describe('matchesSavedMap', () => {
function test(matchDomainOnly) {
matchDomainOnly = !!matchDomainOnly;
return () => {
describe('without host prefix', () => {
it('should match url without path', () => {
expect(
utils.matchesSavedMap('https://duckduckgo.com', matchDomainOnly, {
host: 'duckduckgo.com',
})
).toBe(true);
});
});

function testPrefixes(isRegex) {
isRegex = !!isRegex;
const simplePattern = isRegex?
'@duckduckgo\\.com' : '!duckduckgo.com';
return () => {
it('should match url without path', () => {
expect(
utils.matchesSavedMap(
'https://duckduckgo.com',
matchDomainOnly, {
host: simplePattern,
})
).toBe(true);
});
it('should match url with path', () => {
expect(
utils.matchesSavedMap(
'https://duckduckgo.com/?q=search+me+baby',
matchDomainOnly, {
host: simplePattern,
})
).toBe(true);
});
let prefix = matchDomainOnly ? 'should not' : 'should';
let description = `${prefix} match url with pattern only in path`;
it(description, () => {
expect(
utils.matchesSavedMap(
'https://google.com/?q=duckduckgo',
matchDomainOnly, {
host: simplePattern,
})
).toBe(!matchDomainOnly);
});
};
}

describe('with regex host prefix', testPrefixes(true));
describe('with glob host prefix', testPrefixes());
};
}

test();
describe('with matchDomainOnly', test(true));

});

});
27 changes: 16 additions & 11 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ export const pathMatch = (url, map) => {
return true;
};

/**
*
* @param url {URL}
* @return {string}
*/
export const urlKeyFromUrl = (url) => {
const parsedUrl = new window.URL(url);
return punycode.toUnicode(parsedUrl.hostname.replace('www.', '')) + parsedUrl.pathname;
return punycode.toUnicode(url.hostname.replace('www.', '')) + url.pathname;
};

/**
Expand All @@ -70,32 +74,33 @@ export const urlKeyFromUrl = (url) => {
* @param map
* @return {*}
*/
export const matchesSavedMap = (url, matchDomainOnly, map) => {
const savedHost = map.host;
export const matchesSavedMap = (url, matchDomainOnly, {host}) => {
let toMatch = url;
let urlO = new window.URL(url);
if (matchDomainOnly) {
toMatch = new window.URL(url).host;
toMatch = urlO.host;
urlO = new window.URL(`${urlO.protocol}//${urlO.host}`);
}

if (savedHost[0] === PREFIX_REGEX) {
const regex = savedHost.substr(1);
if (host[0] === PREFIX_REGEX) {
const regex = host.substr(1);
try {
return new RegExp(regex).test(toMatch);
} catch (e) {
console.error('couldn\'t test regex', regex, e);
}
} else if (savedHost[0] === PREFIX_GLOB) {
} else if (host[0] === PREFIX_GLOB) {
// turning glob into regex isn't the worst thing:
// 1. * becomes .*
// 2. ? becomes .?
return new RegExp(savedHost.substr(1)
return new RegExp(host.substr(1)
.replace(/\*/g, '.*')
.replace(/\?/g, '.?'))
.test(toMatch);
} else {
const key = urlKeyFromUrl(toMatch);
const key = urlKeyFromUrl(urlO);
const _url = ((key.indexOf('/') === -1) ? key.concat('/') : key).toLowerCase();
const mapHost = ((map.host.indexOf('/') === -1) ? map.host.concat('/') : map.host).toLowerCase();
const mapHost = ((host.indexOf('/') === -1) ? host.concat('/') : host).toLowerCase();
return domainMatch(_url, mapHost) && pathMatch(_url, mapHost);

}
Expand Down

0 comments on commit 406631d

Please sign in to comment.