Created
January 7, 2011 20:59
-
-
Save think49/770087 to your computer and use it in GitHub Desktop.
rfc3986.js : RFC3986 に準拠した URI チェッカー
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* rfc3986.js | |
* Uniform Resource Identifier (URI): Generic Syntax (RFC3986) | |
* | |
* @version 1.0 | |
* @author think49 | |
* @url https://gist.github.com/770087 | |
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">www.ietf.org/rfc/rfc3986.txt</a> | |
* @see <a href="http://www.studyinghttp.net/rfc_ja/rfc3986">Uniform Resource Identifier (URI): 一般的構文</a> | |
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License) | |
*/ | |
'use strict'; | |
function RFC3986 () { | |
var | |
// === RFC2234 | |
// 6.1 Core Rules | |
ALPHA = '[\u0041-\u005A\u0061-\u007A]', // [A-Za-z] | |
DIGIT = '[\u0030-\u0039]', // [0-9] | |
HEXDIG = '(?:' + DIGIT + '|[A-Z])', | |
// === RFC3986 | |
// 2.1. Percent-Encoding | |
pct_encoded = '%' + HEXDIG + HEXDIG, | |
// 2.2. Reserved Characters | |
gen_delims = '[:/?#[\\]@]', | |
sub_delims = '[!$&\u0027()*+,;=]', | |
reserved = '(?:' + gen_delims + '|' + sub_delims + ')', | |
// 2.3. Unreserved Characters | |
unreserved = '(?:' + ALPHA + '|' + DIGIT + '|[\\-._~])', | |
// 3.1. Scheme | |
scheme = ALPHA + '(?:' + ALPHA + '|' + DIGIT + '|[+\\-.])*', | |
// 3.2.1. User Information | |
userinfo = '(?:' + unreserved + '|' + pct_encoded + '|' + sub_delims + '|:)*', | |
// 3.2.2. Host | |
dec_octet = '(?:' + DIGIT + '|[\u0031-\u0039]' + DIGIT + '|1(?:' + DIGIT + '){2}|2[\u0030-\u0034]' + DIGIT + '|25[\u0030-\u0035])', | |
IPv4address = dec_octet + '\\.' + dec_octet + '\\.' + dec_octet + '\\.' + dec_octet, | |
h16 = '(?:' + HEXDIG + '){1,4}', | |
ls32 = '(?:' + h16 + ':' + h16 + '|' + IPv4address + ')', | |
IPv6address = ['(?:' + h16 + ':){6}' + ls32, | |
'::' + '(?:' + h16 + ':){5}' + ls32, | |
'(?:' + h16 + ')?::' + '(?:' + h16 + ':){4}' + ls32, | |
'(?:(?:' + h16 + ':)+' + h16 + ')?::' + '(?:' + h16 + ':){3}' + ls32, | |
'(?:(?:' + h16 + ':){0,2}' + h16 + ')?::' + '(?:' + h16 + ':){2}' + ls32, | |
'(?:(?:' + h16 + ':){0,3}' + h16 + ')?::' + h16 + ':' + ls32, | |
'(?:(?:' + h16 + ':){0,4}' + h16 + ')?::' + ls32, | |
'(?:(?:' + h16 + ':){0,5}' + h16 + ')?::' + h16, | |
'(?:(?:' + h16 + ':){0,6}' + h16 + ')?::' + h16 | |
].join('|'), | |
IPvFuture = 'v(?:' + HEXDIG + ')+\\.(?:' + unreserved + '|' + sub_delims + '|:)+', | |
IP_literal = '(?:' + IPv6address + '|' + IPvFuture + ')?', | |
reg_name = '(?:' + unreserved + '|' + pct_encoded + '|' + sub_delims + ')*', | |
host = '(?:' + IP_literal + '|' + IPv4address + '|' + reg_name + ')', | |
// 3.2.3. Port | |
port = '(?:' + DIGIT + ')*', | |
// 3.2. Authority | |
authority = '(?:' + userinfo + '@)?' + host + '(?::' + port + ')?', | |
// 3.3. Path | |
pchar = '(?:' + unreserved + '|' + pct_encoded + '|' + sub_delims + '|[:@])', | |
segment = '(?:' + pchar + ')*', | |
segment_nz = '(?:' + pchar + ')+', | |
segment_nz_nc = '(?:' + unreserved + '|' + pct_encoded + '|' + sub_delims + '|@)+', | |
path_abempty = '(?:/' + segment + ')*', | |
path_absolute = '/(?:' + segment_nz + '(?:/' + segment + ')*)?', | |
path_noscheme = segment_nz_nc + '(?:/' + segment + ')*', | |
path_rootless = segment_nz + '(?:/' + segment + ')*', | |
path_empty = '(?:' + pchar + '){0}', | |
path = '(?:' + path_abempty + '|' + path_absolute + '|' + path_noscheme + '|' + path_rootless + '|' + path_empty + ')', | |
// 3.4. Query | |
query = '(?:' + pchar + '|[/?])*', | |
// 3.5. Fragment | |
fragment = '(?:' + pchar + '|[/?])*', | |
// 3. Syntax Components | |
hier_part = '(?://' + authority + path_abempty + '|' + path_absolute + '|' + path_rootless + '|' + path_empty + ')', | |
URI = scheme + ':' + hier_part + '(?:\\?' + query + ')?(?:#' + fragment + ')?', | |
// 4.2. Relative Reference | |
relative_part = '(?://' + authority + path_abempty + '|' + path_absolute + '|' + path_noscheme + '|' + path_empty + ')', | |
relative_ref = relative_part + '(?:\\?' + query + ')?(?:#' + fragment + ')?', | |
// 4.3. Absolute URI | |
absolute_URI = scheme + ':' + hier_part + '(?:\\?' + query + ')?', | |
// 4.1. URI Reference | |
URI_reference = '(?:' + URI + '|' + relative_ref + ')'; | |
this.absolute_URI = absolute_URI; | |
this.relative_ref = relative_ref; | |
this.URI = URI; | |
this.URI_reference = URI_reference; | |
} | |
RFC3986.prototype.isAbsoluteURI = function (arg) { | |
if ('string' !== typeof arg) throw new TypeError(arg + ' is not a string'); | |
return new RegExp('^' + this.absolute_URI + '$').test(arg); | |
}; | |
RFC3986.prototype.isRelativeRef = function (arg) { | |
if ('string' !== typeof arg) throw new TypeError(arg + ' is not a string'); | |
return new RegExp('^' + this.relative_ref + '$').test(arg); | |
}; | |
RFC3986.prototype.isURI = function (arg) { | |
if ('string' !== typeof arg) throw new TypeError(arg + ' is not a string'); | |
return new RegExp('^' + this.URI + '$').test(arg); | |
}; | |
RFC3986.prototype.isURIReference = function (arg) { | |
if ('string' !== typeof arg) throw new TypeError(arg + ' is not a string'); | |
return new RegExp('^' + this.URI_reference + '$').test(arg); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考リンク。