From aa6c82f8c99c60b89b77f286ac4e7f186b014650 Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Tue, 3 Apr 2018 09:55:48 +0200 Subject: [PATCH] Add a suggestion box on site field If a user paste an url like https://WWW.example.org/ in the site field. LessPass is suggesting : * example * example.org * www.example.org Fixes: https://github.com/lesspass/lesspass/issues/295 Fixes: https://github.com/lesspass/lesspass/issues/294 --- package-lock.json | 5 + package.json | 3 +- src/components/InputSite.vue | 91 +++++++++++ src/services/url-parser.js | 10 +- src/views/PasswordGenerator.vue | 327 ++++++++++++++++++++-------------------- test/unit/url-parser.js | 10 ++ 6 files changed, 281 insertions(+), 165 deletions(-) create mode 100644 src/components/InputSite.vue diff --git a/package-lock.json b/package-lock.json index e297152..86482a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8299,6 +8299,11 @@ "integrity": "sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=", "dev": true }, + "lodash.uniqby": { + "version": "4.7.0", + "resolved": "https://registry.npmjs.org/lodash.uniqby/-/lodash.uniqby-4.7.0.tgz", + "integrity": "sha1-2ZwHpmnp5tJOE2Lf4mbGdhavEwI=" + }, "log-symbols": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-2.2.0.tgz", diff --git a/package.json b/package.json index d1b3c56..c831520 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,8 @@ ] }, "dependencies": { - "@oslab/btoa": "^0.1.0", "@oslab/atob": "^0.1.0", + "@oslab/btoa": "^0.1.0", "awesomplete": "^1.1.2", "axios": "^0.18.0", "bootstrap": "^4.0.0", @@ -35,6 +35,7 @@ "jwt-decode": "^2.2.0", "lesspass": "^6.0.0", "lodash.debounce": "^4.0.8", + "lodash.uniqby": "^4.7.0", "vue": "^2.5.16", "vue-polyglot": "^0.2.3", "vue-router": "^3.0.1", diff --git a/src/components/InputSite.vue b/src/components/InputSite.vue new file mode 100644 index 0000000..9aea4df --- /dev/null +++ b/src/components/InputSite.vue @@ -0,0 +1,91 @@ + + + diff --git a/src/services/url-parser.js b/src/services/url-parser.js index bbe815e..f856770 100644 --- a/src/services/url-parser.js +++ b/src/services/url-parser.js @@ -16,10 +16,12 @@ function isAnIpAddressWithPort(address) { export function getSuggestions(url) { const cleanedUrl = cleanUrl(url) || url; - const urlElements = cleanedUrl.split("."); - if (isAnIpAddressWithPort(cleanedUrl) || urlElements.length < 2) { - return []; - } + if (isAnIpAddressWithPort(cleanedUrl)) return []; + const urlElements = cleanedUrl + .toLowerCase() + .split(".") + .filter(element => element.length >= 2); + if (urlElements.length < 2) return []; const baseName = urlElements[urlElements.length - 2]; const tld = urlElements[urlElements.length - 1]; return urlElements.reduceRight( diff --git a/src/views/PasswordGenerator.vue b/src/views/PasswordGenerator.vue index b2853d6..cdde893 100644 --- a/src/views/PasswordGenerator.vue +++ b/src/views/PasswordGenerator.vue @@ -1,31 +1,25 @@ diff --git a/test/unit/url-parser.js b/test/unit/url-parser.js index 0880b59..c9b96a4 100644 --- a/test/unit/url-parser.js +++ b/test/unit/url-parser.js @@ -55,6 +55,16 @@ test("getSuggestions", t => { urlParser.getSuggestions("https://192.168.1.1:10443/webapp/") ); t.deepEqual([], urlParser.getSuggestions("example")); + t.deepEqual([], urlParser.getSuggestions("example.")); + t.deepEqual([], urlParser.getSuggestions("example.o")); + t.deepEqual( + urlParser.getSuggestions("http://example.org"), + urlParser.getSuggestions("https://example.org") + ); + t.deepEqual( + ["example", "example.org"], + urlParser.getSuggestions("EXAMPLE.org") + ); }); test("getSite", t => {