more localhost use cases

jh-changes
chenchao 5 years ago
parent 5758100f43
commit f3ce1c300a
  1. 14
      src/shared/urls.ts
  2. 14
      test/shared/urls.test.ts

@ -5,7 +5,16 @@ const trimStart = (str: string): string => {
return str.replace(/^\s+/, ''); return str.replace(/^\s+/, '');
}; };
const SUPPORTED_PROTOCOLS = ['http:', 'https:', 'ftp:', 'mailto:', 'about:', 'localhost:']; const SUPPORTED_PROTOCOLS = ['http:', 'https:', 'ftp:', 'mailto:', 'about:'];
const isLocalhost = (url: string): boolean => {
if (url === 'localhost') {
return true;
}
let [host, port] = url.split(':', 2);
return host === 'localhost' && !isNaN(Number(port));
};
const searchUrl = (keywords: string, search: Search): string => { const searchUrl = (keywords: string, search: Search): string => {
try { try {
@ -16,7 +25,8 @@ const searchUrl = (keywords: string, search: Search): string => {
} catch (e) { } catch (e) {
// fallthrough // fallthrough
} }
if (keywords.includes('.') && !keywords.includes(' ')) { if (isLocalhost(keywords) ||
(keywords.includes('.') && !keywords.includes(' '))) {
return 'http://' + keywords; return 'http://' + keywords;
} }
let template = search.engines[search.defaultEngine]; let template = search.engines[search.defaultEngine];

@ -36,6 +36,19 @@ describe("shared/commands/parsers", () => {
expect(parsers.searchUrl('std::vector', config)) expect(parsers.searchUrl('std::vector', config))
.to.equal('https://google.com/search?q=std%3A%3Avector'); .to.equal('https://google.com/search?q=std%3A%3Avector');
}); });
it('localhost urls', () => {
expect(parsers.searchUrl('localhost', config))
.to.equal('http://localhost');
expect(parsers.searchUrl('http://localhost', config))
.to.equal('http://localhost');
expect(parsers.searchUrl('localhost:8080', config))
.to.equal('http://locahost:8080');
expect(parsers.searchUrl('localhost:80nan', config))
.to.equal('https://google.com/search?q=localhost:80nan');
expect(parsers.searchUrl('localhost 8080', config))
.to.equal('https://google.com/search?q=localhost%208080');
})
}); });
describe('#normalizeUrl', () => { describe('#normalizeUrl', () => {
@ -47,4 +60,3 @@ describe("shared/commands/parsers", () => {
}); });
}); });
}); });