Skip to content

Instantly share code, notes, and snippets.

@razass
Forked from ericf/sanitizeUrl.js
Created August 31, 2010 17:53
Show Gist options
  • Save razass/559430 to your computer and use it in GitHub Desktop.
Save razass/559430 to your computer and use it in GitHub Desktop.
/**
* Sanitize URL — Validate it looks like a URL, then make it less dirty.
*
* Oddnut Software
* Copyright (c) 2010 Eric Ferraiuolo - http://eric.ferraiuolo.name
* MIT License - http://www.opensource.org/licenses/mit-license.php
*
* Examples:
*
* 'Http://WWW.example.com/' » 'http://www.example.com/'
* 'example.com' » 'http://example.com'
* 'example.c' » null
* 'https://foo:Bar@Example.com' » 'https://foo:Bar@example.com'
* ' WWW.EXAMPLE.COM ' » 'http://www.example.com'
*/
var sanitizeUrl = (function(){
var URL_REGEXP = /^(?:(https?:)\/\/)?(?:([^:@\s]+:?[^:@\s]+?)@)?((?:[^;:@=\/\?\.\s]+\.)+[A-Za-z0-9\-]{2,})(?::(\d+))?(?=\/|$)(\/.*)?/i,
TRIM_REGEXP = /^\s+|\s+$/g,
HTTP_PROTOCOL = 'http:',
SLASH_SLASH = '//',
AT = '@',
COLON = ':',
EMPTY_STRING = '';
function trim (s) {
try {
return s.replace(TRIM_REGEXP, EMPTY_STRING);
} catch(e) {
return s;
}
}
return function (url) {
url = trim(url);
if (url.length < 1) { return null; }
var urlParts = url.match(URL_REGEXP),
protocol, authorization, host, port, path;
if ( ! urlParts) { return null; }
protocol = (urlParts[1] ? urlParts[1].toLowerCase() : HTTP_PROTOCOL) + SLASH_SLASH;
authorization = (urlParts[2] ? (urlParts[2] + AT) : EMPTY_STRING);
host = (urlParts[3] ? urlParts[3].toLowerCase() : EMPTY_STRING);
port = (urlParts[4] ? (COLON + urlParts[4]) : EMPTY_STRING);
path = (urlParts[5] || EMPTY_STRING);
return ( protocol + authorization + host + port + path );
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment