Ox.URL: add json url part, encode '?' and '#' in values

This commit is contained in:
rolux 2013-02-18 12:50:20 +05:30
parent 5ca1c5b36c
commit 6ecbff5fce

View file

@ -207,6 +207,16 @@ Ox.URL <f> URL controller
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': {
hash: {
anchor: 'a',
@ -246,6 +256,8 @@ Ox.URL <f> URL controller
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)']); })
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']); })
true
@*/
@ -254,7 +266,7 @@ Ox.URL <f> URL controller
example.com[/page][#hash]
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"
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: ''}
(comparison operator) or {conditions: [], operator: ''} (logical
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'.
String Key Value Operator
@ -423,6 +439,10 @@ Ox.URL = function(options) {
}) : '';
}
function constructJSON(json) {
return Ox.serialize(json, true);
}
function constructLocation(location) {
return location.join(',');
}
@ -496,7 +516,7 @@ Ox.URL = function(options) {
function encodeValue(str) {
// var chars = '/&|()=*:';
var chars = '&|()=*',
var chars = '&|()=*?#',
ret = '';
str.toString().split('').forEach(function(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) {
return str.split(',').map(function(str, i) {
return Ox.limit(parseInt(str, 10), -90 * (i + 1), 90 * (i + 1));
@ -675,9 +699,14 @@ Ox.URL = function(options) {
function parseURL(str, callback) {
// fixme: removing trailing slash makes it impossible to search for '/'
var split = str.split('#'),
parts = split.shift().replace(/(^\/|\/$)/g, '').split('/'),
state = split.length ? {hash: parseHash(split.join('#'))} : {};
var hashSplit = str.split('#'),
jsonSplit = hashSplit[0].split('?'),
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] == '') {
// empty URL
callback(state);