Ox.URL: add json url part, encode '?' and '#' in values
This commit is contained in:
parent
5ca1c5b36c
commit
6ecbff5fce
1 changed files with 34 additions and 5 deletions
|
@ -207,6 +207,16 @@ Ox.URL <f> URL controller
|
||||||
operator: '&'
|
operator: '&'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
'/?a=[1,2]&b=true&n=1.2&o={"k":"v"}&s1="foo"&s2=bar': {
|
||||||
|
json: {
|
||||||
|
a: [1, 2],
|
||||||
|
b: true,
|
||||||
|
n: 1.2,
|
||||||
|
o: {k: 'v'},
|
||||||
|
s1: 'foo',
|
||||||
|
s2: 'bar'
|
||||||
|
}
|
||||||
|
},
|
||||||
'/#a?k=v&l=w': {
|
'/#a?k=v&l=w': {
|
||||||
hash: {
|
hash: {
|
||||||
anchor: 'a',
|
anchor: 'a',
|
||||||
|
@ -246,6 +256,8 @@ Ox.URL <f> URL controller
|
||||||
true
|
true
|
||||||
> !!Ox.test.url.parse('/population>0&(name=a*|name=*z)', function(o) { Ox.test(o, Ox.test.result['/population>0&(name=a*|name=*z)']); })
|
> !!Ox.test.url.parse('/population>0&(name=a*|name=*z)', function(o) { Ox.test(o, Ox.test.result['/population>0&(name=a*|name=*z)']); })
|
||||||
true
|
true
|
||||||
|
> !!Ox.test.url.parse('/?a=[1,2]&b=true&n=1.2&o={"k":"v"}&s1="foo"&s2=bar', function(o) { Ox.test(o, Ox.test.result['/?a=[1,2]&b=true&n=1.2&o={"k":"v"}&s1="foo"&s2=bar']); })
|
||||||
|
true
|
||||||
> !!Ox.test.url.parse('/#a?k=v&l=w', function(o) { Ox.test(o, Ox.test.result['/#a?k=v&l=w']); })
|
> !!Ox.test.url.parse('/#a?k=v&l=w', function(o) { Ox.test(o, Ox.test.result['/#a?k=v&l=w']); })
|
||||||
true
|
true
|
||||||
@*/
|
@*/
|
||||||
|
@ -254,7 +266,7 @@ Ox.URL <f> URL controller
|
||||||
|
|
||||||
example.com[/page][#hash]
|
example.com[/page][#hash]
|
||||||
or
|
or
|
||||||
example.com[/type][/item][/view][/span][/sort][/find][#hash]
|
example.com[/type][/item][/view][/span][/sort][/find][?json][#hash]
|
||||||
|
|
||||||
page Special page, like "about" or "contact"
|
page Special page, like "about" or "contact"
|
||||||
type Section a.k.a. item type, like "movies", "edits", "texts" etc.
|
type Section a.k.a. item type, like "movies", "edits", "texts" etc.
|
||||||
|
@ -273,6 +285,10 @@ find Query, like a=x or a=x&b=y or a=x&(b=y|c=z). A query object has the form
|
||||||
object has the form {key: '', value: '' or ['', ''], operator: ''}
|
object has the form {key: '', value: '' or ['', ''], operator: ''}
|
||||||
(comparison operator) or {conditions: [], operator: ''} (logical
|
(comparison operator) or {conditions: [], operator: ''} (logical
|
||||||
operator). Condition strings can be more than just "k=v", see below.
|
operator). Condition strings can be more than just "k=v", see below.
|
||||||
|
json Query, like 'a=[1,2]&b=true&f=1.2&i=3&o={"k":"v"}&s="foo"'. Values are
|
||||||
|
evaluated as JSON, and if that fails interpreted as a string. So strings
|
||||||
|
(outside arrays or objects) work without quotes as well, unless their
|
||||||
|
value is valid JSON, like 'true' or '1'.
|
||||||
hash Anchor and/or query, like 'a' or '?k=v' or 'a?k=v&l=w'.
|
hash Anchor and/or query, like 'a' or '?k=v' or 'a?k=v&l=w'.
|
||||||
|
|
||||||
String Key Value Operator
|
String Key Value Operator
|
||||||
|
@ -423,6 +439,10 @@ Ox.URL = function(options) {
|
||||||
}) : '';
|
}) : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function constructJSON(json) {
|
||||||
|
return Ox.serialize(json, true);
|
||||||
|
}
|
||||||
|
|
||||||
function constructLocation(location) {
|
function constructLocation(location) {
|
||||||
return location.join(',');
|
return location.join(',');
|
||||||
}
|
}
|
||||||
|
@ -496,7 +516,7 @@ Ox.URL = function(options) {
|
||||||
|
|
||||||
function encodeValue(str) {
|
function encodeValue(str) {
|
||||||
// var chars = '/&|()=*:';
|
// var chars = '/&|()=*:';
|
||||||
var chars = '&|()=*',
|
var chars = '&|()=*?#',
|
||||||
ret = '';
|
ret = '';
|
||||||
str.toString().split('').forEach(function(char) {
|
str.toString().split('').forEach(function(char) {
|
||||||
var index = chars.indexOf(char);
|
var index = chars.indexOf(char);
|
||||||
|
@ -641,6 +661,10 @@ Ox.URL = function(options) {
|
||||||
} : {});
|
} : {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseJSON(str) {
|
||||||
|
return Ox.unserialize(str, true);
|
||||||
|
}
|
||||||
|
|
||||||
function parseLocation(str) {
|
function parseLocation(str) {
|
||||||
return str.split(',').map(function(str, i) {
|
return str.split(',').map(function(str, i) {
|
||||||
return Ox.limit(parseInt(str, 10), -90 * (i + 1), 90 * (i + 1));
|
return Ox.limit(parseInt(str, 10), -90 * (i + 1), 90 * (i + 1));
|
||||||
|
@ -675,9 +699,14 @@ Ox.URL = function(options) {
|
||||||
|
|
||||||
function parseURL(str, callback) {
|
function parseURL(str, callback) {
|
||||||
// fixme: removing trailing slash makes it impossible to search for '/'
|
// fixme: removing trailing slash makes it impossible to search for '/'
|
||||||
var split = str.split('#'),
|
var hashSplit = str.split('#'),
|
||||||
parts = split.shift().replace(/(^\/|\/$)/g, '').split('/'),
|
jsonSplit = hashSplit[0].split('?'),
|
||||||
state = split.length ? {hash: parseHash(split.join('#'))} : {};
|
parts = jsonSplit[0].replace(/(^\/|\/$)/g, '').split('/'),
|
||||||
|
state = Ox.extend(
|
||||||
|
{},
|
||||||
|
hashSplit.length == 2 ? {hash: parseHash(hashSplit[1])} : {},
|
||||||
|
jsonSplit.length == 2 ? {json: parseJSON(jsonSplit[1])} : {}
|
||||||
|
);
|
||||||
if (parts[0] == '') {
|
if (parts[0] == '') {
|
||||||
// empty URL
|
// empty URL
|
||||||
callback(state);
|
callback(state);
|
||||||
|
|
Loading…
Reference in a new issue