Namespace Pot.URI
Defined in: <pot.js>.
Constructor Attributes | Constructor Name and Description |
---|---|
URI utilities.
|
Method Attributes | Method Name and Description |
---|---|
<static> |
Pot.URI.buildURI(url, (query))
Build to the URI from a string or an object with query-string.
|
<static> |
Pot.URI.getExt(path)
Get the file extension name.
|
<static> |
Pot.URI.parseURI(uri)
Parse the URI string to an object that has keys like "location" object.
|
<static> |
Pot.URI.resolveRelativeURI(uri, (context))
Resolves the incomplete URI.
|
<static> |
Pot.URI.toDataURI(data, (mimeType), (base64), (charset))
Create Data Scheme (data:.
|
<static> |
Pot.URI.urlDecode(string)
Decode the URI string.
|
<static> |
Pot.URI.urlEncode(string)
Encode the URI string.
|
Method Detail
<static>
{Function}
Pot.URI.buildURI(url, (query))
Build to the URI from a string or an object with query-string.
Object key names can treat like "window.location" object keys.
Query-string will encode by percent-encoding.
var url = 'http://www.example.com/'; var params = { foo : '{foo}', bar : '{bar}' }; var result = Pot.buildURI(url, params); debug(result); // @results 'http://www.example.com/?foo=%7Bfoo%7D&bar=%7Bbar%7D'
var url = 'http://www.example.com/test?a=1'; var params = [ ['prototype', '{foo}'], ['__iterator__', '{bar}'], ]; var result = Pot.buildURI(url, params); debug(result); // @results // 'http://www.example.com/test?' + // 'a=1&prototype=%7Bfoo%7D&__iterator__=%7Bbar%7D'
var url = 'http://www.example.com/test?a=1'; var params = 'b=2&c=3'; var result = Pot.buildURI(url, params); debug(result); // @results 'http://www.example.com/test?a=1&b=2&c=3'
var parts = { protocol : 'http:', username : 'user', password : 'pass', hostname : 'www.example.com', port : 8000, pathname : '/path/to/file.ext', query : { arg1 : 'v1', arg2 : 'v#2' }, hash : 'a' }; var result = Pot.buildURI(parts); debug(result); // @results // 'http://user:pass@www.example.com:8000/path/to/file.ext' + // '?arg1=v1&arg2=v%232#a'
var uri = 'http://user:pass@host:8000/path/to/file.ext?' + 'arg=value#fragment'; var parts = parseURI(uri); var result = Pot.buildURI(parts); debug(result); // @results // 'http://user:pass@host:8000/path/to/file.ext?arg=value#fragment'
var parts = { protocol : 'file:', pathname : 'C:\\path\\to\\file.ext', query : { arg1 : 'value#1', arg2 : 'value#2' }, hash : '#fragment' }; var result = Pot.buildURI(parts); debug(result); // @results // 'file:///C:\\path\\to\\file.ext?' + // 'arg1=value%231&arg2=value%232#fragment'
- Parameters:
- {String|Object} url
- Base URI string or parts as Object.
- {Object|Array|String} (query)
- (Optional) queryString.
- Returns:
- {String} Return a builded URI string.
<static>
{Function}
Pot.URI.getExt(path)
Get the file extension name.
This method enabled for URI or URL string etc.
var fileName = 'foo.html'; var result = getExt(fileName); debug(result); // @results 'html'
var fileName = 'C:\\foo\\bar\\baz.tmp.txt'; var result = getExt(fileName); debug(result); // @results 'txt'
var uri = 'http://www.example.com/file.html?q=hoge.js'; var result = getExt(uri); debug(result); // @results 'html'
var uri = 'http://www.example.com/?output=hoge.xml#fuga.piyo'; var result = getExt(uri); debug(result); // @results 'xml'
var uri = 'http://www.example.com/?q=hoge'; var result = getExt(uri); debug(result); // @results ''
var uri, result; uri = 'http://www.example.com/http%3A%2F%2Fwww.example.com%2Ffoo%2Ejs'; result = getExt(uri); debug(result); // @results 'js'
- Parameters:
- {String} path
- The target filename or URI.
- Returns:
- {String} The file extension name that was excluded dot(.).
<static>
{Function}
Pot.URI.parseURI(uri)
Parse the URI string to an object that has keys like "location" object.
from: RFC 3986 URI Generic Syntax Parsing a URI Reference with a Regular Expression As the "first-match-wins" algorithm is identical to the "greedy" disambiguation method used by POSIX regular expressions, it is natural and commonplace to use a regular expression for parsing the potential five components of a URI reference. The following line is the regular expression for breaking-down a well-formed URI reference into its components. ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? 12 3 4 5 6 7 8 9 The numbers in the second line above are only to assist readability; they indicate the reference points for each subexpression (i.e., each paired parenthesis). We refer to the value matched for subexpressionas $ . For example, matching the above expression to http://www.ics.uci.edu/pub/ietf/uri/#Related results in the following subexpression matches: $1 = http: $2 = http $3 = //www.ics.uci.edu $4 = www.ics.uci.edu $5 = /pub/ietf/uri/ $6 = $7 = $8 = #Related $9 = Related where indicates that the component is not present, as is the case for the query component in the above example. Therefore, we can determine the value of the five components as scheme = $2 authority = $4 path = $5 query = $7 fragment = $9 Going in the opposite direction, we can recreate a URI reference from its components by using the algorithm of Section 5.3.
// // This results contains all the keys and values. // var uri, result; uri = 'http://user:pass@host:8000/path/to/file.ext?arg=value#fragment'; result = parseURI(uri); debug(result); // @results // { // protocol : 'http:', // scheme : 'http', // userinfo : 'user:pass', // username : 'user', // password : 'pass', // host : 'host:8000', // hostname : 'host', // port : '8000', // pathname : '/path/to/file.ext', // dirname : '/path/to', // filename : 'file.ext', // extension : 'ext', // search : '?arg=value', // query : 'arg=value', // hash : '#fragment', // fragment : 'fragment' // }
var uri = 'file:///C:/foo/bar/baz.js'; var result = parseURI(uri); debug(result); // @results // { // protocol : 'file:', // scheme : 'file', // userinfo : '', // username : '', // password : '', // host : '', // hostname : '', // port : '', // pathname : 'C:/foo/bar/baz.js', // dirname : 'C:/foo/bar', // filename : 'baz.js', // extension : 'js', // search : '', // query : '', // hash : '', // fragment : '' // }
- Parameters:
- {String} uri
- The target URI string.
- Returns:
- {Object} Object of parsing result.
<static>
{Function}
Pot.URI.resolveRelativeURI(uri, (context))
Resolves the incomplete URI.
Then, fix the invalid symbols for ".." and "./" etc hierarchies.
var uri = 'C:/path/to/foo/bar/../hoge.ext'; var result = resolveRelativeURI(uri); debug(result); // @results 'C:/path/to/foo/hoge.ext'
var uri = 'C:/path/to/../../hoge.ext'; var result = resolveRelativeURI(uri); debug(result); // @results 'C:/hoge.ext'
var uri = 'C:/path/to/../../../../././../../hoge.ext'; var result = resolveRelativeURI(uri); debug(result); // @results 'C:/hoge.ext'
var uri = '/////path/to/////hoge.ext'; var context = document; var result = resolveRelativeURI(uri, context); debug(result); // @results e.g., 'http://www.example.com/path/to/hoge.ext'
var uri = './hoge.png'; var context = document.getElementById('image1'); var result = resolveRelativeURI(uri, context); debug(result); // @results e.g., 'http://www.example.com/dir1/dir2/hoge.png'
var uri = '/usr/local/bin/../././hoge.ext'; var result = resolveRelativeURI(uri); debug(result); // @results '/usr/local/hoge.ext'
- Parameters:
- {String} uri
- The target URI.
- {Object} (context)
- The completion object that is able to reference absolute URI. (i.e., document).
- Returns:
- {String} The result string that has absolute URI.
<static>
{Function}
Pot.URI.toDataURI(data, (mimeType), (base64), (charset))
Create Data Scheme (data:...).
RFC 2397 - The "data" URL scheme
debug( toDataURI('Hello World!', 'text/plain', false, 'UTF-8', false) ); // @results 'data:text/plain;charset=UTF-8,Hello%20World!'
debug( toDataURI('Hello World!', 'text/plain', true, 'UTF-8', false) ); // @results 'data:text/plain;charset=UTF-8;base64,SGVsbG8gV29ybGQh'
debug( toDataURI({ data : 'Hello World!', mimeType : 'html', base64 : true }) ); // @results 'data:text/html;base64,SGVsbG8gV29ybGQh'
debug( toDataURI({ data : 'SGVsbG8gV29ybGQh', mimeType : 'txt', base64 : true, encoded : true }) ); // @results 'data:text/plain;base64,SGVsbG8gV29ybGQh'
- Parameters:
- {String|Object} data
- The target data string, or Object.
When specify Object: - data : {String} Data string. - mimeType : {String} MIMEType or Extension. e.g. 'image/png', 'png' etc. - base64 : {Boolean} True if encode base64. - charset : {String} Character Encoding. - encoded : {Boolean} True if already encoded data.
- {String} (mimeType)
- MIME Type (e.g. 'image/png').
- {Boolean} (base64)
- Whether the `data` is base64 format.
- {String} (charset)
- The character code if needed.
- Returns:
- {String} The result Data URI.
- See:
- http://tools.ietf.org/html/rfc2397
data:[
][;charset= ][;base64],
<static>
{Function}
Pot.URI.urlDecode(string)
Decode the URI string.
- Parameters:
- {String} string
- The subject string.
- Returns:
- {String} The decoded string.
<static>
{Function}
Pot.URI.urlEncode(string)
Encode the URI string.
- Parameters:
- {String} string
- The subject string.
- Returns:
- {String} The encoded string.