Skip to content

Commit

Permalink
Added URL autocorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
nwittwer committed Feb 4, 2019
1 parent 14e1323 commit f3d1d26
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/renderer/components/ToolBar/URLInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
</template>

<script>
import autoCorrectURL from "@/mixins/autoCorrectURL.js";
export default {
name: "URLInput",
props: ["state"],
Expand All @@ -51,8 +53,12 @@ export default {
}
},
methods: {
triggerSiteLoad(url) {
this.$emit("url-changed", url);
async triggerSiteLoad(url) {
if (!url) return false;
// Validate URL
const newURL = await this.validateURL(url);
this.$emit("url-changed", newURL);
this.blur();
},
blur() {
Expand All @@ -61,6 +67,14 @@ export default {
vm.$nextTick(() => {
vm.$refs.input.blur();
});
},
async validateURL(url) {
try {
// @TODO: Refactor/simplify the URL corrector
return autoCorrectURL(url)
} catch(e) {
return false;
}
}
}
};
Expand Down
86 changes: 86 additions & 0 deletions src/renderer/mixins/autoCorrectURL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// @TODO: Refactor/simplify this
export default function (url) {
return new Promise((resolve, reject) => {
if (typeof url !== "string") {
throw new TypeError(
`Expected url to be of type string, got ${typeof url}`
)
}

let hasHttpPrefix = false
let hasDot = false
let hasLocalhost = false
let hasLocalPath = false

// Step 1: Does it have http:// or https:// ?
// The "?" in the Regex accepts http or https
if (new RegExp(/^https?/).test(url) == true) {
hasHttpPrefix = true;
} else if (new RegExp(/^HTTPS?/).test(url)) {
// Case: HTTP://somesite.com/SOMETHING
// ^ only replace this part
if (new RegExp(/^HTTP/).test(url)) {
url = url.replace(/^HTTP/, "http");
hasHttpPrefix = true;
} else if (new RegExp(/^HTTPS/).test(url)) {
url = url.replace(/^HTTPS/, "https");
hasHttpPrefix = true;
}
}

// Step 2: Does it have .* (i.e. ".com") ?
if (url.includes(".") == true) {
hasDot = true;
}

// Step 3: Does it include "localhost"?
if (url.includes("localhost") == true) {
hasLocalhost = true;
}

// Step 4: Does it include:
// Mac: "file://"
// Windows: "file:///C:/"
if (url.includes("file://") == true) {
hasLocalPath = true;
}

// Handle localhost URLs
if (hasLocalhost) {
if (hasHttpPrefix) {
// Example: http://localhost:8000
resolve(url)
} else {
url = "http://" + url;
resolve(url)
}
}

// Handle local paths
if (hasLocalPath) {
// Example: file:///users/nick/sites/index.html
resolve(url)
}

// Handle non-localhost URLs
if (hasHttpPrefix && hasDot) {
// Perfect format:
// http[s]://example.com
resolve(url)
} else if (hasHttpPrefix == false && hasDot == true) {
// Case: example.com
// Check if URL starts with anything besides a letter or digit
if (new RegExp(/^[0-9a-z]/).test(url) == false) {
reject()
} else {
// The URL can be prepended by http://
url = "http://" + url;
resolve(url)
}
} else {
// Empty string or unknown error
// Example: alert(url + " is not a valid URL.");
reject()
}
});
}

0 comments on commit f3d1d26

Please sign in to comment.