<commit_before>/* globals $ */

import { remote } from 'electron';
import { servers } from './servers';

export var start = function() {
    var defaultInstance = 'https://demo.rocket.chat';

    //init loader
    var loader = document.querySelector('.loader');
    if (loader) {
        var src = loader.getAttribute('data-src');
        var http = new XMLHttpRequest();
        http.open('GET', src);
        http.onreadystatechange = function() {
            if (this.readyState === this.DONE) {
                if (this.response) {
                    loader.innerHTML = this.response + loader.innerHTML;
                }
            }
        };
        http.send();
    }

    function loadPreviousHost() {
        var current = localStorage.getItem('rocket.chat.currentHost');
        if (current) {
            var item = getInstanceButtonByURL(current);
            if (item) {
                loadServer(item);
            }
        }
    }

    // connection check
    if (!navigator.onLine) {
        offline();
    }
    window.addEventListener('online', online);
    window.addEventListener('offline', offline);

    function online() {
        document.body.classList.remove('offline');
    }

    function offline() {
        document.body.classList.add('offline');
    }
    // end connection check

    var form = document.querySelector('form');
    var hostField = form.querySelector('[name="host"]');
    var button = form.querySelector('[type="submit"]');
    var invalidUrl = form.querySelector('#invalidUrl');
    function validateHost() {
        invalidUrl.style.display = 'none';
        hostField.classList.remove('wrong');

        var host = hostField.value.trim();
        host = host.replace(/\/$/, '');
        hostField.value = host;

        if (host.length === 0) {
            button.value = 'Connect';
            button.disabled = false;
            return;
        }

        button.value = 'Validating...';
        button.disabled = true;

        servers.validateHost(host, 2000).then(function() {
            button.value = 'Connect';
            button.disabled = false;
        }, function() {
            // If the url begins with HTTP, mark as invalid
            if (/^http:\/\/.+/.test(host)) {
                button.value = 'Invalid url';
                invalidUrl.style.display = 'block';
                hostField.classList.add('wrong');
                return;
            }

            // If the url begins with HTTPS, fallback to HTTP
            if (/^https:\/\/.+/.test(host)) {
                hostField.value = host.replace('https://', 'http://');
                return validateHost();
            }

            // If the url isn't localhost, don't have dots and don't have protocol
            // try as a .rocket.chat subdomain
            if (!/(^https?:\/\/)|(\.)|(^localhost$)/.test(host)) {
                hostField.value = `https://${host}.rocket.chat`;
                return validateHost();
            }

            // If the url don't start with protocol try HTTPS
            if (!/^https?:\/\//.test(host)) {
                hostField.value = `https://${host}`;
                return validateHost();
            }
        });
    }

    hostField.addEventListener('blur', function() {
        validateHost();
    });


    form.addEventListener('submit', function(ev) {
        console.log('trying to connect');
        ev.preventDefault();
        ev.stopPropagation();
        var input = form.querySelector('[name="host"]');
        var url = input.value;
        url = url.replace(/\/$/, '');

        if (url.length === 0) {
            connectDefaultInstance();
        } else {
            addServer(url);
            redirect(url);
            input.value = '';
        }

        return false;
    });

    // function changeServer() {
    //     document.querySelector('.rocket-app').style.display = 'none';
    //     document.querySelector('.landing-page').style.display = 'block';
    // }

    function addServer(url) {
        if (servers.hostExists(url)) {
            var item = getInstanceButtonByURL(url);
            if (item) {
                loadServer(item);
            }
            return;
        }

        if (servers.addHost(url) === false) {
            return;
        }<commit_msg>Fix url validation on form submit<commit_after>/* globals $ */

import { remote } from 'electron';
import { servers } from './servers';

export var start = function() {
    var defaultInstance = 'https://demo.rocket.chat';

    //init loader
    var loader = document.querySelector('.loader');
    if (loader) {
        var src = loader.getAttribute('data-src');
        var http = new XMLHttpRequest();
        http.open('GET', src);
        http.onreadystatechange = function() {
            if (this.readyState === this.DONE) {
                if (this.response) {
                    loader.innerHTML = this.response + loader.innerHTML;
                }
            }
        };
        http.send();
    }

    function loadPreviousHost() {
        var current = localStorage.getItem('rocket.chat.currentHost');
        if (current) {
            var item = getInstanceButtonByURL(current);
            if (item) {
                loadServer(item);
            }
        }
    }

    // connection check
    if (!navigator.onLine) {
        offline();
    }
    window.addEventListener('online', online);
    window.addEventListener('offline', offline);

    function online() {
        document.body.classList.remove('offline');
    }

    function offline() {
        document.body.classList.add('offline');
    }
    // end connection check

    var form = document.querySelector('form');
    var hostField = form.querySelector('[name="host"]');
    var button = form.querySelector('[type="submit"]');
    var invalidUrl = form.querySelector('#invalidUrl');
    function validateHost() {
        return new Promise(function(resolve, reject) {
            var execValidation = function() {
                invalidUrl.style.display = 'none';
                hostField.classList.remove('wrong');

                var host = hostField.value.trim();
                host = host.replace(/\/$/, '');
                hostField.value = host;

                if (host.length === 0) {
                    button.value = 'Connect';
                    button.disabled = false;
                    resolve();
                    return;
                }

                button.value = 'Validating...';
                button.disabled = true;

                servers.validateHost(host, 2000).then(function() {
                    button.value = 'Connect';
                    button.disabled = false;
                    resolve();
                }, function() {
                    // If the url begins with HTTP, mark as invalid
                    if (/^http:\/\/.+/.test(host)) {
                        button.value = 'Invalid url';
                        invalidUrl.style.display = 'block';
                        hostField.classList.add('wrong');
                        reject();
                        return;
                    }

                    // If the url begins with HTTPS, fallback to HTTP
                    if (/^https:\/\/.+/.test(host)) {
                        hostField.value = host.replace('https://', 'http://');
                        return execValidation();
                    }

                    // If the url isn't localhost, don't have dots and don't have protocol
                    // try as a .rocket.chat subdomain
                    if (!/(^https?:\/\/)|(\.)|(^localhost$)/.test(host)) {
                        hostField.value = `https://${host}.rocket.chat`;
                        return execValidation();
                    }

                    // If the url don't start with protocol try HTTPS
                    if (!/^https?:\/\//.test(host)) {
                        hostField.value = `https://${host}`;
                        return execValidation();
                    }
                });
            };
            execValidation();
        });
    }

    hostField.addEventListener('blur', function() {
        validateHost();
    });


    form.addEventListener('submit', function(ev) {
        ev.preventDefault();
        ev.stopPropagation();
        validateHost().then(function() {
            var input = form.querySelector('[name="host"]');
            var url = input.value;
            console.log(url);

            if (url.length === 0) {
                connectDefaultInstance();
            } else {
                addServer(url);
                redirect(url);
                input.value = '';
            }
        });

        return false;
    });

    // function changeServer() {
    //     document.querySelector('.rocket-app').style.display = 'none';
    //     document.querySelector('.landing-page').style.display = 'block';
    // }

    function addServer(url) {
        if (servers.hostExists(url)) {
            var item = getInstanceButtonByURL(url);
            if (item) {
                loadServer(item);
            }
            return;
        }

        if (servers.addHost(url) === false) {
            return;
        }
{'fraction_non_alphanumeric': 0.14053526925994148, 'fraction_numerical': 0.0013002492144327663, 'mean_word_length': 1.475194422097077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1507182', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2308, 'n_words': 491}