├── params.erl
├── openchrome
├── Makefile
├── json_round_trip.ehe
├── server.erl
├── misultin_adapter.erl
├── mochiweb_adapter.erl
├── Readme
├── cowboy_adapter.erl
├── ehe.erl
├── mochijson2.erl
└── jquery-1.5.min.js
/params.erl:
--------------------------------------------------------------------------------
1 | -module(params).
2 | -compile(export_all).
3 |
4 | abc(X, Y) ->
5 | {abcCalled, X, Y}.
6 |
7 | %% test of parameterised modules calling sequences
8 |
9 | %% {params,1,2,3,4,5}:abc(123)
10 | %% => {abcCalled, 123, {foo,1,2,3,4,5}
11 |
--------------------------------------------------------------------------------
/openchrome:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | # an OS independent way to open a URL in a browser
4 |
5 | os=`uname`
6 |
7 | # echo "starting $1"
8 |
9 | case $os in
10 | Linux)
11 | firefox $1;;
12 | Darwin)
13 | open -a 'Google Chrome' $1;;
14 | *)
15 | echo unknown OS $os;;
16 | esac
17 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | .SUFFIXES: .erl .beam .yrl
2 |
3 | MODS := $(wildcard *.erl)
4 |
5 | %.beam: %.erl
6 | erlc -W $<
7 |
8 | server1: beam
9 | (sleep 1 && ./openchrome http://localhost:1234/json_round_trip.ehe) &
10 | erl -noshell -s server start misultin_adapter
11 |
12 | server2: beam
13 | (sleep 1 && ./openchrome http://localhost:1234/json_round_trip.ehe) &
14 | erl -noshell -s server start cowboy_adapter
15 |
16 | server3: beam
17 | (sleep 1 && ./openchrome http://localhost:1234/json_round_trip.ehe) &
18 | erl -noshell -s server start mochiweb_adapter
19 |
20 | beam: ${MODS:%.erl=%.beam}
21 |
22 | clean:
23 | rm -rf *.beam *~ erl_crash.dump
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/json_round_trip.ehe:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | Ehe test
47 |
48 | 1;(G,N) -> N*G(G,N-1) end,
49 | Fact = fun(N) -> F(F, N) end,"" ?>
50 |
51 | Factorial =
52 |
53 | Test the JSON round trip
54 |
55 | test
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/server.erl:
--------------------------------------------------------------------------------
1 | -module(server).
2 | -compile(export_all).
3 | -import(lists, [reverse/1]).
4 |
5 | start([Mod]) ->
6 | Port = 1234,
7 | Z = Mod:start_link([{port, Port},
8 | {handler, fun handle_request/1}]),
9 | io:format("Server started with ~w~n",[Mod]),
10 | receive
11 | after
12 | infinity ->
13 | void
14 | end.
15 |
16 | handle_request(R) ->
17 | Path = R:get(path),
18 | Args = R:get(args),
19 | io:format("Path=~p Args=~p~n",[Path, Args]),
20 | handle(Path, Args, R).
21 |
22 | handle(["echo"], Args, R) ->
23 | Obj = R:json_to_erl(Args),
24 | case obj() of
25 | Obj -> R:send_data(html, "Test worked");
26 | _ -> R:send_data(html, R:pre({"Test failed", sent, obj(), received, Obj}))
27 | end;
28 | handle(["get_term"],[{"tag1","this"},{"tag2","10"}],R) ->
29 | Json = obj(),
30 | R:reply_json(Json);
31 | handle([File], _, R) ->
32 | case filelib:is_file(File) of
33 | true -> R:send_file(File);
34 | false -> R:send_data(html, R:pre({missing_file,File}))
35 | end;
36 | handle(X, Args, R) ->
37 | R:send_data(html, R:pre({funny,X,Args})).
38 |
39 | obj() ->
40 | [{struct,[{<<"string">>,<<"joe">>},
41 | {<<"int">>,42},
42 | {<<"float">>,3.14159}
43 | ]},
44 | 123,
45 | <<"a string">>].
46 |
47 |
--------------------------------------------------------------------------------
/misultin_adapter.erl:
--------------------------------------------------------------------------------
1 | -module(misultin_adapter).
2 | -compile(export_all).
3 |
4 | start_link([{port,Port},{handler,F}]) ->
5 | Z = misultin:start_link([{port, Port}, {loop, fun(Req) -> handle_http(Req, F) end}]),
6 | ok.
7 |
8 | handle_http(Req, F) ->
9 | case (catch F({?MODULE, Req})) of
10 | {'EXIT', Why} ->
11 | io:format("EXIT:~p~n",[Why]);
12 | X ->
13 | X
14 | end.
15 |
16 | json_to_erl([{Str,[]}], _) ->
17 | mochijson2:decode(Str).
18 |
19 | send_data(Type, Data, {_,Req}) ->
20 | Req:ok([header(Type)], Data).
21 |
22 | send_file(File, {_, Req}) ->
23 | case filename:extension(File) of
24 | ".ehe" ->
25 | {ok, Bin} = file:read_file(File),
26 | {Data, Bs} = ehe:expand_binary(Bin, [{'Req', {?MODULE,Req}}]),
27 | Req:ok([header(html)], Data);
28 | Ext ->
29 | {ok, Bin} = file:read_file(File),
30 | Req:ok([header(classify_extension(Ext))], Bin)
31 | end.
32 |
33 | magic(_) ->
34 | "Hello from the misultin adapter".
35 |
36 | get(path, {_,Req}) ->
37 | Resource = Req:resource([lowercase, urldecode]);
38 | get(args, {_,Req}) ->
39 | Req:parse_qs().
40 |
41 | reply_json(Obj, {_,Req}) ->
42 | Json = mochijson2:encode(Obj),
43 | Req:ok([header(json)], Json).
44 |
45 | header(X) ->
46 | {"Content-Type", mime_type(X)}.
47 |
48 | mime_type(gif) -> "image/gif";
49 | mime_type(jpg) -> "image/jpeg";
50 | mime_type(png) -> "image/png";
51 | mime_type(css) -> "text/css";
52 | mime_type(special) -> "text/plain; charset=x-user-defined";
53 | mime_type(json) -> "application/json";
54 | mime_type(swf) -> "application/x-shockwave-flash";
55 | mime_type(html) -> "text/html";
56 | mime_type(xul) -> "application/vnd.mozilla.xul+xml";
57 | mime_type(js) -> "application/x-javascript";
58 | mime_type(svg) -> "image/svg+xml".
59 |
60 | pre(X, _) ->
61 | ["\n",quote(lists:flatten(io_lib:format("~p",[X]))), " "].
62 |
63 | quote("<" ++ T) -> "<" ++ quote(T);
64 | quote("&" ++ T) -> "&" ++ quote(T);
65 | quote([H|T]) -> [H|quote(T)];
66 | quote([]) -> [].
67 |
68 | classify_extension(".GIF") -> gif;
69 | classify_extension(".jpg") -> jpeg;
70 | classify_extension(".js") -> js;
71 | classify_extension(".css") -> css;
72 | classify_extension(_) -> html.
73 |
--------------------------------------------------------------------------------
/mochiweb_adapter.erl:
--------------------------------------------------------------------------------
1 | -module(mochiweb_adapter).
2 | -compile(export_all).
3 |
4 | start_link([{port,Port},{handler,F}]) ->
5 | Z = mochiweb_http:start_link([{port, Port},
6 | {loop, {?MODULE, handle_http, [F]}}
7 | ]),
8 | ok.
9 |
10 | handle_http(Req, F) ->
11 | case (catch F({?MODULE, Req})) of
12 | {'EXIT', Why} ->
13 | io:format("EXIT:~p~n",[Why]);
14 | X ->
15 | X
16 | end.
17 |
18 | json_to_erl([{Str,[]}], _) ->
19 | mochijson2:decode(Str).
20 |
21 | send_data(Type, Data, {_,Req}) ->
22 | Req:ok({mime_type(Type),[], Data}).
23 |
24 | send_file(File, {_, Req}) ->
25 | case filename:extension(File) of
26 | ".ehe" ->
27 | {ok, Bin} = file:read_file(File),
28 | {Data, Bs} = ehe:expand_binary(Bin, [{'Req', {?MODULE,Req}}]),
29 | Req:ok({mime_type(html),[],Data});
30 | Ext ->
31 | {ok, Bin} = file:read_file(File),
32 | Req:ok({mime_type(classify_extension(Ext)),[],Bin})
33 | end.
34 |
35 | magic(_) ->
36 | "Hello from the mochiweb adapter".
37 |
38 | get(path, {_,Req}) ->
39 | Path = Req:get(path),
40 | case filename:split(Path) of
41 | ["/"|T] -> T;
42 | Path -> Path
43 | end;
44 | get(args, {_,Req}) ->
45 | Req:parse_qs().
46 |
47 | reply_json(Obj, {_,Req}) ->
48 | Json = mochijson2:encode(Obj),
49 | Req:ok({mime_type(json), [], Json}).
50 |
51 | header(X) ->
52 | {"Content-Type", mime_type(X)}.
53 |
54 | mime_type(gif) -> "image/gif";
55 | mime_type(jpg) -> "image/jpeg";
56 | mime_type(png) -> "image/png";
57 | mime_type(css) -> "text/css";
58 | mime_type(special) -> "text/plain; charset=x-user-defined";
59 | mime_type(json) -> "application/json";
60 | mime_type(swf) -> "application/x-shockwave-flash";
61 | mime_type(html) -> "text/html";
62 | mime_type(xul) -> "application/vnd.mozilla.xul+xml";
63 | mime_type(js) -> "application/x-javascript";
64 | mime_type(svg) -> "image/svg+xml".
65 |
66 | pre(X, _) ->
67 | ["\n",quote(lists:flatten(io_lib:format("~p",[X]))), " "].
68 |
69 | quote("<" ++ T) -> "<" ++ quote(T);
70 | quote("&" ++ T) -> "&" ++ quote(T);
71 | quote([H|T]) -> [H|quote(T)];
72 | quote([]) -> [].
73 |
74 |
75 | classify_extension(".GIF") -> gif;
76 | classify_extension(".jpg") -> jpeg;
77 | classify_extension(".js") -> js;
78 | classify_extension(".css") -> css;
79 | classify_extension(_) -> html.
80 |
--------------------------------------------------------------------------------
/Readme:
--------------------------------------------------------------------------------
1 | This is an experiment in the use of the adapter pattern
2 |
3 | It started off as an experiment in passing JSON terms back and
4 | forth between a server and the browser - for this I used misultin
5 | but yesterdays announcement that misultin would cease to be supported
6 | promoted this work - then I threw in ehe which I've been toying with
7 | for a while ...
8 |
9 |
10 | I have made simple adapters for misultin/mochiweb and cowboy.
11 |
12 | The example assumes that misultin/mochiweb and cowboy have been
13 | installed and compiled on your machine.
14 |
15 | On my machine I have downloaded the latest zip files from github
16 | and built the packages
17 |
18 | I have added three lines in ${HOME}.erlang so that the erlang loader
19 | can find these three programs.
20 |
21 | code:add_patha("/Users/joe/nobackup/mochi-mochiweb-af4cb95/ebin").
22 | code:add_patha("/Users/joe/nobackup/extend-cowboy-e7b6e2a/ebin").
23 | code:add_patha("/Users/joe/nobackup/installed/mochi-mochiweb-b7f3693/ebin").
24 |
25 | There are three adapters
26 |
27 | misultin_adapter.erl
28 | cowboy_adapter.erl
29 | mochiweb_adapter.erl
30 |
31 | Using these adapters we can write server.erl in a generic manner.
32 |
33 | I have also made a simple scripting language ehe.
34 |
35 | About ehe
36 | =========
37 |
38 | Ehe is *very* simple.
39 |
40 | Files with the .ehe extension are assumed to be html files containing embedded
41 | Erlang.
42 |
43 | The syntax assumes the bit inside the delimiters is a sequence
44 | or Erlang expressions. The replacement value of the sequence is the
45 | *last* value in the sequence. Binndings created in the squence are propagated
46 | to the next block of embedded erlang.
47 |
48 | The Global variable 'Req' inside an embedded Erlang expression is the
49 | paramterised module which allows the appropriate adapter to be called.
50 |
51 | Thus 'Req:magic()' calls (for example) calls mochiweb_adapter:magic(Req)
52 | if we are using the mochiweb adapter.
53 |
54 | To play with this
55 | =================
56 |
57 | 1) install mochiweb, misultin and cowboy
58 | 2) edit your .erlang so that the system can find the mochiweb, misultin and
59 | cowboy compiled beam files
60 | 3) type
61 | > make server1
62 | > make server2
63 | > make server3
64 | to test
65 | 4) Take a look at the (generic) server.erl
66 | 5) Take a look at json_round_trip.ehe
67 |
68 | Note: this assumes the script openchrome starts a chrome browser
69 | you will have to tweak openchrome to start some other browser
70 |
71 |
72 |
73 |
74 |
--------------------------------------------------------------------------------
/cowboy_adapter.erl:
--------------------------------------------------------------------------------
1 | -module(cowboy_adapter).
2 | -compile(export_all).
3 |
4 | start_link([{port,Port},{handler,F}]) ->
5 | ok = application:start(cowboy),
6 | Dispatch = [{'_', [{'_', ?MODULE, F}]}], %% server is the name of this module
7 | NumberOfAcceptors = 100,
8 | cowboy:start_listener(my_named_thing,
9 | NumberOfAcceptors,
10 | cowboy_tcp_transport, [{port, Port}],
11 | cowboy_http_protocol, [{dispatch, Dispatch}]),
12 | ok.
13 |
14 |
15 | init({tcp, http}, Req, Opts) -> {ok, Req, Opts}.
16 |
17 | terminate(_, _) -> ok.
18 |
19 | handle(Req, F) ->
20 | case (catch F({?MODULE, Req, F})) of
21 | {'EXIT', Why} ->
22 | io:format("EXIT:~p~n",[Why]);
23 | X ->
24 | X
25 | end.
26 |
27 | json_to_erl([{Str,[]}], _) ->
28 | mochijson2:decode(Str).
29 |
30 | send_data(Type, Data, {_,Req, F}) ->
31 | %% warning Type ignored ... fix me ...
32 | {ok, Req1} = cowboy_http_req:reply(200, [], Data, Req),
33 | {ok, Req1, F}.
34 |
35 | send_file(File, {_, Req, F}) ->
36 | %% should check the mime type here ... do later
37 | case filename:extension(File) of
38 | ".ehe" ->
39 | {ok, Bin} = file:read_file(File),
40 | {Data, Bs} = ehe:expand_binary(Bin, [{'Req', {?MODULE,Req, F}}]),
41 | {ok, Req1} = cowboy_http_req:reply(200, [], Data, Req),
42 | {ok, Req1, F};
43 | Ext ->
44 | {ok, Bin} = file:read_file(File),
45 | {ok, Req1} = cowboy_http_req:reply(200, [], Bin, Req),
46 | {ok, Req1, F}
47 | end.
48 |
49 | magic(_) ->
50 | "Hello from the cowboy adapter".
51 |
52 |
53 | get(path, {_, Req, _}) ->
54 | {Path, _} = cowboy_http_req:path(Req),
55 | [binary_to_list(I) || I <- Path];
56 | get(args, {_, Req, _}) ->
57 | {Args, _} = cowboy_http_req:qs_vals(Req),
58 | Args1 = [{cvt(K),cvt(V)} || {K,V} <- Args],
59 | Args1.
60 |
61 | cvt(X) when is_binary(X) -> binary_to_list(X);
62 | cvt(true) -> [].
63 |
64 | reply_json(Obj, {_,Req, F}) ->
65 | Json = mochijson2:encode(Obj),
66 | {ok, Req1} = cowboy_http_req:reply(200, [], Json, Req),
67 | {ok, Req1, F}.
68 |
69 | header(X) ->
70 | {"Content-Type", mime_type(X)}.
71 |
72 | mime_type(gif) -> "image/gif";
73 | mime_type(jpg) -> "image/jpeg";
74 | mime_type(png) -> "image/png";
75 | mime_type(css) -> "text/css";
76 | mime_type(special) -> "text/plain; charset=x-user-defined";
77 | mime_type(json) -> "application/json";
78 | mime_type(swf) -> "application/x-shockwave-flash";
79 | mime_type(html) -> "text/html";
80 | mime_type(xul) -> "application/vnd.mozilla.xul+xml";
81 | mime_type(js) -> "application/x-javascript";
82 | mime_type(svg) -> "image/svg+xml".
83 |
84 | pre(X, _) ->
85 | ["\n",quote(lists:flatten(io_lib:format("~p",[X]))), " "].
86 |
87 | quote("<" ++ T) -> "<" ++ quote(T);
88 | quote("&" ++ T) -> "&" ++ quote(T);
89 | quote([H|T]) -> [H|quote(T)];
90 | quote([]) -> [].
91 |
92 |
--------------------------------------------------------------------------------
/ehe.erl:
--------------------------------------------------------------------------------
1 | -module(ehe).
2 | -compile(export_all).
3 | -import(lists, [reverse/1, reverse/2]).
4 |
5 | expand_file(File, Bs) ->
6 | {ok, Bin} = file:read_file(File),
7 | expand_string(binary_to_list(Bin), Bs).
8 |
9 | expand_binary(Str, Bs) ->
10 | expand_string(binary_to_list(Str), Bs).
11 |
12 | expand_string(Str, Bs) ->
13 | B1 = make_bindings(Bs, erl_eval:new_bindings()),
14 | expand(Str, 0, B1, []).
15 |
16 | make_bindings([{K,V}|T], B) -> make_bindings(T, erl_eval:add_binding(K,V,B));
17 | make_bindings([], B) -> B.
18 |
19 | expand("
20 | {Str, Ln1, T1} = collect_form(T, Ln, []),
21 | {Value, B1} = string2value(Str, Ln, B0),
22 | expand(T1, Ln1, B1, reverse(Value, L));
23 | expand([$\n|T], Ln, B, L) ->
24 | expand(T, Ln+1, B, [$\n|L]);
25 | expand([H|T], Ln, B, L) ->
26 | expand(T, Ln, B, [H|L]);
27 | expand([], _, B, L) ->
28 | {reverse(L), B}.
29 |
30 | collect_form("?>" ++ T, Ln, L) -> {reverse(L), Ln, T};
31 | collect_form([$\n|T], Ln, L) -> collect_form(T, Ln+1, [$\n|L]);
32 | collect_form([H|T], Ln, L) -> collect_form(T, Ln, [H|L]);
33 | collect_form([], Ln, L) -> {reverse(L), Ln, []}.
34 |
35 | string2value(Str, Ln, Bindings0) ->
36 | case erl_scan:string(Str ++ ".", Ln) of
37 | {ok, Tokens, _} ->
38 | case erl_parse:parse_exprs(Tokens) of
39 | {ok, Exprs} ->
40 | {value, Value, Bindings1} = eval(Exprs, Bindings0),
41 | {Value, Bindings1};
42 | Other ->
43 | io:format("cannot parse:~p Reason=~p~n",[Tokens,Other])
44 | end;
45 | Other ->
46 | io:format("cannot tokenise:~p Reason=~p~n",[Str,Other])
47 | end.
48 |
49 | %% eval([{call,_,{remote,_,{atom,_,ehe},{atom,_,bindings}},[]}|T], B0) ->
50 | %% {B0, B0},
51 |
52 |
53 | eval(Exprs, B0) ->
54 | %% io:format("ehe:~p~n",[Exprs]),
55 | case (catch erl_eval:exprs(Exprs, B0, {eval, fun local/3})) of
56 | {'EXIT', Why} ->
57 | Error = pre({error,evaluating,Exprs,content,B0,was,Why}),
58 | {value, Error, B0};
59 | {redirect, X} ->
60 | throw({redirect,X});
61 | Other ->
62 | Other
63 | end.
64 |
65 | local(bindings, [], B0) ->
66 | {value, B0, B0};
67 | local(pre, [Expr], B0) ->
68 | {value, Val, B1} = eval([Expr], B0),
69 | {value, pre(Val), B1};
70 | local(show_bindings,[],B0) ->
71 | {value, pre(B0), B0};
72 | local(redirect, [Expr], B0) ->
73 | {value, Val, B1} = eval([Expr], B0),
74 | io:format("Redirecting to:~p~n",[Val]),
75 | throw({redirect, Val});
76 | local(Name, Args, B0) ->
77 | io:format("local handler called:~p~n",[{Name,Args,B0}]),
78 | {true, B0}.
79 |
80 | pre(X) ->
81 | ["\n",quote(lists:flatten(io_lib:format("~p",[X]))), " "].
82 |
83 | quote("<" ++ T) -> "<" ++ quote(T);
84 | quote("&" ++ T) -> "&" ++ quote(T);
85 | quote([H|T]) -> [H|quote(T)];
86 | quote([]) -> [].
87 |
88 | require_arg(Key, [{Key,Val}]) -> Val;
89 | require_arg(Key, [_|T]) -> require_arg(Key, T);
90 | require_arg(Key, []) -> io:format("Missign argument:~p~n",[Key]),
91 | exit(ebadInput).
92 |
93 |
94 |
95 |
--------------------------------------------------------------------------------
/mochijson2.erl:
--------------------------------------------------------------------------------
1 | %% @author Bob Ippolito
2 | %% @copyright 2007 Mochi Media, Inc.
3 |
4 | %% @doc Yet another JSON (RFC 4627) library for Erlang. mochijson2 works
5 | %% with binaries as strings, arrays as lists (without an {array, _})
6 | %% wrapper and it only knows how to decode UTF-8 (and ASCII).
7 | %%
8 | %% JSON terms are decoded as follows (javascript -> erlang):
9 | %%
10 | %% {"key": "value"} ->
11 | %% {struct, [{<<"key">>, <<"value">>}]}
12 | %% ["array", 123, 12.34, true, false, null] ->
13 | %% [<<"array">>, 123, 12.34, true, false, null]
14 | %%
15 | %%
16 | %%
17 | %% Strings in JSON decode to UTF-8 binaries in Erlang
18 | %% Objects decode to {struct, PropList}
19 | %% Numbers decode to integer or float
20 | %% true, false, null decode to their respective terms.
21 | %%
22 | %% The encoder will accept the same format that the decoder will produce,
23 | %% but will also allow additional cases for leniency:
24 | %%
25 | %% atoms other than true, false, null will be considered UTF-8
26 | %% strings (even as a proplist key)
27 | %%
28 | %% {json, IoList} will insert IoList directly into the output
29 | %% with no validation
30 | %%
31 | %% {array, Array} will be encoded as Array
32 | %% (legacy mochijson style)
33 | %%
34 | %% A non-empty raw proplist will be encoded as an object as long
35 | %% as the first pair does not have an atom key of json, struct,
36 | %% or array
37 | %%
38 | %%
39 |
40 | -module(mochijson2).
41 | -author('bob@mochimedia.com').
42 | -export([encoder/1, encode/1]).
43 | -export([decoder/1, decode/1, decode/2]).
44 |
45 | %% This is a macro to placate syntax highlighters..
46 | -define(Q, $\").
47 | -define(ADV_COL(S, N), S#decoder{offset=N+S#decoder.offset,
48 | column=N+S#decoder.column}).
49 | -define(INC_COL(S), S#decoder{offset=1+S#decoder.offset,
50 | column=1+S#decoder.column}).
51 | -define(INC_LINE(S), S#decoder{offset=1+S#decoder.offset,
52 | column=1,
53 | line=1+S#decoder.line}).
54 | -define(INC_CHAR(S, C),
55 | case C of
56 | $\n ->
57 | S#decoder{column=1,
58 | line=1+S#decoder.line,
59 | offset=1+S#decoder.offset};
60 | _ ->
61 | S#decoder{column=1+S#decoder.column,
62 | offset=1+S#decoder.offset}
63 | end).
64 | -define(IS_WHITESPACE(C),
65 | (C =:= $\s orelse C =:= $\t orelse C =:= $\r orelse C =:= $\n)).
66 |
67 | %% @type iolist() = [char() | binary() | iolist()]
68 | %% @type iodata() = iolist() | binary()
69 | %% @type json_string() = atom | binary()
70 | %% @type json_number() = integer() | float()
71 | %% @type json_array() = [json_term()]
72 | %% @type json_object() = {struct, [{json_string(), json_term()}]}
73 | %% @type json_eep18_object() = {[{json_string(), json_term()}]}
74 | %% @type json_iolist() = {json, iolist()}
75 | %% @type json_term() = json_string() | json_number() | json_array() |
76 | %% json_object() | json_eep18_object() | json_iolist()
77 |
78 | -record(encoder, {handler=null,
79 | utf8=false}).
80 |
81 | -record(decoder, {object_hook=null,
82 | offset=0,
83 | line=1,
84 | column=1,
85 | state=null}).
86 |
87 | %% @spec encoder([encoder_option()]) -> function()
88 | %% @doc Create an encoder/1 with the given options.
89 | %% @type encoder_option() = handler_option() | utf8_option()
90 | %% @type utf8_option() = boolean(). Emit unicode as utf8 (default - false)
91 | encoder(Options) ->
92 | State = parse_encoder_options(Options, #encoder{}),
93 | fun (O) -> json_encode(O, State) end.
94 |
95 | %% @spec encode(json_term()) -> iolist()
96 | %% @doc Encode the given as JSON to an iolist.
97 | encode(Any) ->
98 | json_encode(Any, #encoder{}).
99 |
100 | %% @spec decoder([decoder_option()]) -> function()
101 | %% @doc Create a decoder/1 with the given options.
102 | decoder(Options) ->
103 | State = parse_decoder_options(Options, #decoder{}),
104 | fun (O) -> json_decode(O, State) end.
105 |
106 | %% @spec decode(iolist(), [{format, proplist | eep18 | struct}]) -> json_term()
107 | %% @doc Decode the given iolist to Erlang terms using the given object format
108 | %% for decoding, where proplist returns JSON objects as [{binary(), json_term()}]
109 | %% proplists, eep18 returns JSON objects as {[binary(), json_term()]}, and struct
110 | %% returns them as-is.
111 | decode(S, Options) ->
112 | json_decode(S, parse_decoder_options(Options, #decoder{})).
113 |
114 | %% @spec decode(iolist()) -> json_term()
115 | %% @doc Decode the given iolist to Erlang terms.
116 | decode(S) ->
117 | json_decode(S, #decoder{}).
118 |
119 | %% Internal API
120 |
121 | parse_encoder_options([], State) ->
122 | State;
123 | parse_encoder_options([{handler, Handler} | Rest], State) ->
124 | parse_encoder_options(Rest, State#encoder{handler=Handler});
125 | parse_encoder_options([{utf8, Switch} | Rest], State) ->
126 | parse_encoder_options(Rest, State#encoder{utf8=Switch}).
127 |
128 | parse_decoder_options([], State) ->
129 | State;
130 | parse_decoder_options([{object_hook, Hook} | Rest], State) ->
131 | parse_decoder_options(Rest, State#decoder{object_hook=Hook});
132 | parse_decoder_options([{format, Format} | Rest], State)
133 | when Format =:= struct orelse Format =:= eep18 orelse Format =:= proplist ->
134 | parse_decoder_options(Rest, State#decoder{object_hook=Format}).
135 |
136 | json_encode(true, _State) ->
137 | <<"true">>;
138 | json_encode(false, _State) ->
139 | <<"false">>;
140 | json_encode(null, _State) ->
141 | <<"null">>;
142 | json_encode(I, _State) when is_integer(I) ->
143 | integer_to_list(I);
144 | json_encode(F, _State) when is_float(F) ->
145 | mochinum:digits(F);
146 | json_encode(S, State) when is_binary(S); is_atom(S) ->
147 | json_encode_string(S, State);
148 | json_encode([{K, _}|_] = Props, State) when (K =/= struct andalso
149 | K =/= array andalso
150 | K =/= json) ->
151 | json_encode_proplist(Props, State);
152 | json_encode({struct, Props}, State) when is_list(Props) ->
153 | json_encode_proplist(Props, State);
154 | json_encode({Props}, State) when is_list(Props) ->
155 | json_encode_proplist(Props, State);
156 | json_encode({}, State) ->
157 | json_encode_proplist([], State);
158 | json_encode(Array, State) when is_list(Array) ->
159 | json_encode_array(Array, State);
160 | json_encode({array, Array}, State) when is_list(Array) ->
161 | json_encode_array(Array, State);
162 | json_encode({json, IoList}, _State) ->
163 | IoList;
164 | json_encode(Bad, #encoder{handler=null}) ->
165 | exit({json_encode, {bad_term, Bad}});
166 | json_encode(Bad, State=#encoder{handler=Handler}) ->
167 | json_encode(Handler(Bad), State).
168 |
169 | json_encode_array([], _State) ->
170 | <<"[]">>;
171 | json_encode_array(L, State) ->
172 | F = fun (O, Acc) ->
173 | [$,, json_encode(O, State) | Acc]
174 | end,
175 | [$, | Acc1] = lists:foldl(F, "[", L),
176 | lists:reverse([$\] | Acc1]).
177 |
178 | json_encode_proplist([], _State) ->
179 | <<"{}">>;
180 | json_encode_proplist(Props, State) ->
181 | F = fun ({K, V}, Acc) ->
182 | KS = json_encode_string(K, State),
183 | VS = json_encode(V, State),
184 | [$,, VS, $:, KS | Acc]
185 | end,
186 | [$, | Acc1] = lists:foldl(F, "{", Props),
187 | lists:reverse([$\} | Acc1]).
188 |
189 | json_encode_string(A, State) when is_atom(A) ->
190 | L = atom_to_list(A),
191 | case json_string_is_safe(L) of
192 | true ->
193 | [?Q, L, ?Q];
194 | false ->
195 | json_encode_string_unicode(xmerl_ucs:from_utf8(L), State, [?Q])
196 | end;
197 | json_encode_string(B, State) when is_binary(B) ->
198 | case json_bin_is_safe(B) of
199 | true ->
200 | [?Q, B, ?Q];
201 | false ->
202 | json_encode_string_unicode(xmerl_ucs:from_utf8(B), State, [?Q])
203 | end;
204 | json_encode_string(I, _State) when is_integer(I) ->
205 | [?Q, integer_to_list(I), ?Q];
206 | json_encode_string(L, State) when is_list(L) ->
207 | case json_string_is_safe(L) of
208 | true ->
209 | [?Q, L, ?Q];
210 | false ->
211 | json_encode_string_unicode(L, State, [?Q])
212 | end.
213 |
214 | json_string_is_safe([]) ->
215 | true;
216 | json_string_is_safe([C | Rest]) ->
217 | case C of
218 | ?Q ->
219 | false;
220 | $\\ ->
221 | false;
222 | $\b ->
223 | false;
224 | $\f ->
225 | false;
226 | $\n ->
227 | false;
228 | $\r ->
229 | false;
230 | $\t ->
231 | false;
232 | C when C >= 0, C < $\s; C >= 16#7f, C =< 16#10FFFF ->
233 | false;
234 | C when C < 16#7f ->
235 | json_string_is_safe(Rest);
236 | _ ->
237 | false
238 | end.
239 |
240 | json_bin_is_safe(<<>>) ->
241 | true;
242 | json_bin_is_safe(<>) ->
243 | case C of
244 | ?Q ->
245 | false;
246 | $\\ ->
247 | false;
248 | $\b ->
249 | false;
250 | $\f ->
251 | false;
252 | $\n ->
253 | false;
254 | $\r ->
255 | false;
256 | $\t ->
257 | false;
258 | C when C >= 0, C < $\s; C >= 16#7f ->
259 | false;
260 | C when C < 16#7f ->
261 | json_bin_is_safe(Rest)
262 | end.
263 |
264 | json_encode_string_unicode([], _State, Acc) ->
265 | lists:reverse([$\" | Acc]);
266 | json_encode_string_unicode([C | Cs], State, Acc) ->
267 | Acc1 = case C of
268 | ?Q ->
269 | [?Q, $\\ | Acc];
270 | %% Escaping solidus is only useful when trying to protect
271 | %% against "" injection attacks which are only
272 | %% possible when JSON is inserted into a HTML document
273 | %% in-line. mochijson2 does not protect you from this, so
274 | %% if you do insert directly into HTML then you need to
275 | %% uncomment the following case or escape the output of encode.
276 | %%
277 | %% $/ ->
278 | %% [$/, $\\ | Acc];
279 | %%
280 | $\\ ->
281 | [$\\, $\\ | Acc];
282 | $\b ->
283 | [$b, $\\ | Acc];
284 | $\f ->
285 | [$f, $\\ | Acc];
286 | $\n ->
287 | [$n, $\\ | Acc];
288 | $\r ->
289 | [$r, $\\ | Acc];
290 | $\t ->
291 | [$t, $\\ | Acc];
292 | C when C >= 0, C < $\s ->
293 | [unihex(C) | Acc];
294 | C when C >= 16#7f, C =< 16#10FFFF, State#encoder.utf8 ->
295 | [xmerl_ucs:to_utf8(C) | Acc];
296 | C when C >= 16#7f, C =< 16#10FFFF, not State#encoder.utf8 ->
297 | [unihex(C) | Acc];
298 | C when C < 16#7f ->
299 | [C | Acc];
300 | _ ->
301 | exit({json_encode, {bad_char, C}})
302 | end,
303 | json_encode_string_unicode(Cs, State, Acc1).
304 |
305 | hexdigit(C) when C >= 0, C =< 9 ->
306 | C + $0;
307 | hexdigit(C) when C =< 15 ->
308 | C + $a - 10.
309 |
310 | unihex(C) when C < 16#10000 ->
311 | <> = <>,
312 | Digits = [hexdigit(D) || D <- [D3, D2, D1, D0]],
313 | [$\\, $u | Digits];
314 | unihex(C) when C =< 16#10FFFF ->
315 | N = C - 16#10000,
316 | S1 = 16#d800 bor ((N bsr 10) band 16#3ff),
317 | S2 = 16#dc00 bor (N band 16#3ff),
318 | [unihex(S1), unihex(S2)].
319 |
320 | json_decode(L, S) when is_list(L) ->
321 | json_decode(iolist_to_binary(L), S);
322 | json_decode(B, S) ->
323 | {Res, S1} = decode1(B, S),
324 | {eof, _} = tokenize(B, S1#decoder{state=trim}),
325 | Res.
326 |
327 | decode1(B, S=#decoder{state=null}) ->
328 | case tokenize(B, S#decoder{state=any}) of
329 | {{const, C}, S1} ->
330 | {C, S1};
331 | {start_array, S1} ->
332 | decode_array(B, S1);
333 | {start_object, S1} ->
334 | decode_object(B, S1)
335 | end.
336 |
337 | make_object(V, #decoder{object_hook=N}) when N =:= null orelse N =:= struct ->
338 | V;
339 | make_object({struct, P}, #decoder{object_hook=eep18}) ->
340 | {P};
341 | make_object({struct, P}, #decoder{object_hook=proplist}) ->
342 | P;
343 | make_object(V, #decoder{object_hook=Hook}) ->
344 | Hook(V).
345 |
346 | decode_object(B, S) ->
347 | decode_object(B, S#decoder{state=key}, []).
348 |
349 | decode_object(B, S=#decoder{state=key}, Acc) ->
350 | case tokenize(B, S) of
351 | {end_object, S1} ->
352 | V = make_object({struct, lists:reverse(Acc)}, S1),
353 | {V, S1#decoder{state=null}};
354 | {{const, K}, S1} ->
355 | {colon, S2} = tokenize(B, S1),
356 | {V, S3} = decode1(B, S2#decoder{state=null}),
357 | decode_object(B, S3#decoder{state=comma}, [{K, V} | Acc])
358 | end;
359 | decode_object(B, S=#decoder{state=comma}, Acc) ->
360 | case tokenize(B, S) of
361 | {end_object, S1} ->
362 | V = make_object({struct, lists:reverse(Acc)}, S1),
363 | {V, S1#decoder{state=null}};
364 | {comma, S1} ->
365 | decode_object(B, S1#decoder{state=key}, Acc)
366 | end.
367 |
368 | decode_array(B, S) ->
369 | decode_array(B, S#decoder{state=any}, []).
370 |
371 | decode_array(B, S=#decoder{state=any}, Acc) ->
372 | case tokenize(B, S) of
373 | {end_array, S1} ->
374 | {lists:reverse(Acc), S1#decoder{state=null}};
375 | {start_array, S1} ->
376 | {Array, S2} = decode_array(B, S1),
377 | decode_array(B, S2#decoder{state=comma}, [Array | Acc]);
378 | {start_object, S1} ->
379 | {Array, S2} = decode_object(B, S1),
380 | decode_array(B, S2#decoder{state=comma}, [Array | Acc]);
381 | {{const, Const}, S1} ->
382 | decode_array(B, S1#decoder{state=comma}, [Const | Acc])
383 | end;
384 | decode_array(B, S=#decoder{state=comma}, Acc) ->
385 | case tokenize(B, S) of
386 | {end_array, S1} ->
387 | {lists:reverse(Acc), S1#decoder{state=null}};
388 | {comma, S1} ->
389 | decode_array(B, S1#decoder{state=any}, Acc)
390 | end.
391 |
392 | tokenize_string(B, S=#decoder{offset=O}) ->
393 | case tokenize_string_fast(B, O) of
394 | {escape, O1} ->
395 | Length = O1 - O,
396 | S1 = ?ADV_COL(S, Length),
397 | <<_:O/binary, Head:Length/binary, _/binary>> = B,
398 | tokenize_string(B, S1, lists:reverse(binary_to_list(Head)));
399 | O1 ->
400 | Length = O1 - O,
401 | <<_:O/binary, String:Length/binary, ?Q, _/binary>> = B,
402 | {{const, String}, ?ADV_COL(S, Length + 1)}
403 | end.
404 |
405 | tokenize_string_fast(B, O) ->
406 | case B of
407 | <<_:O/binary, ?Q, _/binary>> ->
408 | O;
409 | <<_:O/binary, $\\, _/binary>> ->
410 | {escape, O};
411 | <<_:O/binary, C1, _/binary>> when C1 < 128 ->
412 | tokenize_string_fast(B, 1 + O);
413 | <<_:O/binary, C1, C2, _/binary>> when C1 >= 194, C1 =< 223,
414 | C2 >= 128, C2 =< 191 ->
415 | tokenize_string_fast(B, 2 + O);
416 | <<_:O/binary, C1, C2, C3, _/binary>> when C1 >= 224, C1 =< 239,
417 | C2 >= 128, C2 =< 191,
418 | C3 >= 128, C3 =< 191 ->
419 | tokenize_string_fast(B, 3 + O);
420 | <<_:O/binary, C1, C2, C3, C4, _/binary>> when C1 >= 240, C1 =< 244,
421 | C2 >= 128, C2 =< 191,
422 | C3 >= 128, C3 =< 191,
423 | C4 >= 128, C4 =< 191 ->
424 | tokenize_string_fast(B, 4 + O);
425 | _ ->
426 | throw(invalid_utf8)
427 | end.
428 |
429 | tokenize_string(B, S=#decoder{offset=O}, Acc) ->
430 | case B of
431 | <<_:O/binary, ?Q, _/binary>> ->
432 | {{const, iolist_to_binary(lists:reverse(Acc))}, ?INC_COL(S)};
433 | <<_:O/binary, "\\\"", _/binary>> ->
434 | tokenize_string(B, ?ADV_COL(S, 2), [$\" | Acc]);
435 | <<_:O/binary, "\\\\", _/binary>> ->
436 | tokenize_string(B, ?ADV_COL(S, 2), [$\\ | Acc]);
437 | <<_:O/binary, "\\/", _/binary>> ->
438 | tokenize_string(B, ?ADV_COL(S, 2), [$/ | Acc]);
439 | <<_:O/binary, "\\b", _/binary>> ->
440 | tokenize_string(B, ?ADV_COL(S, 2), [$\b | Acc]);
441 | <<_:O/binary, "\\f", _/binary>> ->
442 | tokenize_string(B, ?ADV_COL(S, 2), [$\f | Acc]);
443 | <<_:O/binary, "\\n", _/binary>> ->
444 | tokenize_string(B, ?ADV_COL(S, 2), [$\n | Acc]);
445 | <<_:O/binary, "\\r", _/binary>> ->
446 | tokenize_string(B, ?ADV_COL(S, 2), [$\r | Acc]);
447 | <<_:O/binary, "\\t", _/binary>> ->
448 | tokenize_string(B, ?ADV_COL(S, 2), [$\t | Acc]);
449 | <<_:O/binary, "\\u", C3, C2, C1, C0, Rest/binary>> ->
450 | C = erlang:list_to_integer([C3, C2, C1, C0], 16),
451 | if C > 16#D7FF, C < 16#DC00 ->
452 | %% coalesce UTF-16 surrogate pair
453 | <<"\\u", D3, D2, D1, D0, _/binary>> = Rest,
454 | D = erlang:list_to_integer([D3,D2,D1,D0], 16),
455 | [CodePoint] = xmerl_ucs:from_utf16be(<>),
457 | Acc1 = lists:reverse(xmerl_ucs:to_utf8(CodePoint), Acc),
458 | tokenize_string(B, ?ADV_COL(S, 12), Acc1);
459 | true ->
460 | Acc1 = lists:reverse(xmerl_ucs:to_utf8(C), Acc),
461 | tokenize_string(B, ?ADV_COL(S, 6), Acc1)
462 | end;
463 | <<_:O/binary, C1, _/binary>> when C1 < 128 ->
464 | tokenize_string(B, ?INC_CHAR(S, C1), [C1 | Acc]);
465 | <<_:O/binary, C1, C2, _/binary>> when C1 >= 194, C1 =< 223,
466 | C2 >= 128, C2 =< 191 ->
467 | tokenize_string(B, ?ADV_COL(S, 2), [C2, C1 | Acc]);
468 | <<_:O/binary, C1, C2, C3, _/binary>> when C1 >= 224, C1 =< 239,
469 | C2 >= 128, C2 =< 191,
470 | C3 >= 128, C3 =< 191 ->
471 | tokenize_string(B, ?ADV_COL(S, 3), [C3, C2, C1 | Acc]);
472 | <<_:O/binary, C1, C2, C3, C4, _/binary>> when C1 >= 240, C1 =< 244,
473 | C2 >= 128, C2 =< 191,
474 | C3 >= 128, C3 =< 191,
475 | C4 >= 128, C4 =< 191 ->
476 | tokenize_string(B, ?ADV_COL(S, 4), [C4, C3, C2, C1 | Acc]);
477 | _ ->
478 | throw(invalid_utf8)
479 | end.
480 |
481 | tokenize_number(B, S) ->
482 | case tokenize_number(B, sign, S, []) of
483 | {{int, Int}, S1} ->
484 | {{const, list_to_integer(Int)}, S1};
485 | {{float, Float}, S1} ->
486 | {{const, list_to_float(Float)}, S1}
487 | end.
488 |
489 | tokenize_number(B, sign, S=#decoder{offset=O}, []) ->
490 | case B of
491 | <<_:O/binary, $-, _/binary>> ->
492 | tokenize_number(B, int, ?INC_COL(S), [$-]);
493 | _ ->
494 | tokenize_number(B, int, S, [])
495 | end;
496 | tokenize_number(B, int, S=#decoder{offset=O}, Acc) ->
497 | case B of
498 | <<_:O/binary, $0, _/binary>> ->
499 | tokenize_number(B, frac, ?INC_COL(S), [$0 | Acc]);
500 | <<_:O/binary, C, _/binary>> when C >= $1 andalso C =< $9 ->
501 | tokenize_number(B, int1, ?INC_COL(S), [C | Acc])
502 | end;
503 | tokenize_number(B, int1, S=#decoder{offset=O}, Acc) ->
504 | case B of
505 | <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
506 | tokenize_number(B, int1, ?INC_COL(S), [C | Acc]);
507 | _ ->
508 | tokenize_number(B, frac, S, Acc)
509 | end;
510 | tokenize_number(B, frac, S=#decoder{offset=O}, Acc) ->
511 | case B of
512 | <<_:O/binary, $., C, _/binary>> when C >= $0, C =< $9 ->
513 | tokenize_number(B, frac1, ?ADV_COL(S, 2), [C, $. | Acc]);
514 | <<_:O/binary, E, _/binary>> when E =:= $e orelse E =:= $E ->
515 | tokenize_number(B, esign, ?INC_COL(S), [$e, $0, $. | Acc]);
516 | _ ->
517 | {{int, lists:reverse(Acc)}, S}
518 | end;
519 | tokenize_number(B, frac1, S=#decoder{offset=O}, Acc) ->
520 | case B of
521 | <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
522 | tokenize_number(B, frac1, ?INC_COL(S), [C | Acc]);
523 | <<_:O/binary, E, _/binary>> when E =:= $e orelse E =:= $E ->
524 | tokenize_number(B, esign, ?INC_COL(S), [$e | Acc]);
525 | _ ->
526 | {{float, lists:reverse(Acc)}, S}
527 | end;
528 | tokenize_number(B, esign, S=#decoder{offset=O}, Acc) ->
529 | case B of
530 | <<_:O/binary, C, _/binary>> when C =:= $- orelse C=:= $+ ->
531 | tokenize_number(B, eint, ?INC_COL(S), [C | Acc]);
532 | _ ->
533 | tokenize_number(B, eint, S, Acc)
534 | end;
535 | tokenize_number(B, eint, S=#decoder{offset=O}, Acc) ->
536 | case B of
537 | <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
538 | tokenize_number(B, eint1, ?INC_COL(S), [C | Acc])
539 | end;
540 | tokenize_number(B, eint1, S=#decoder{offset=O}, Acc) ->
541 | case B of
542 | <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
543 | tokenize_number(B, eint1, ?INC_COL(S), [C | Acc]);
544 | _ ->
545 | {{float, lists:reverse(Acc)}, S}
546 | end.
547 |
548 | tokenize(B, S=#decoder{offset=O}) ->
549 | case B of
550 | <<_:O/binary, C, _/binary>> when ?IS_WHITESPACE(C) ->
551 | tokenize(B, ?INC_CHAR(S, C));
552 | <<_:O/binary, "{", _/binary>> ->
553 | {start_object, ?INC_COL(S)};
554 | <<_:O/binary, "}", _/binary>> ->
555 | {end_object, ?INC_COL(S)};
556 | <<_:O/binary, "[", _/binary>> ->
557 | {start_array, ?INC_COL(S)};
558 | <<_:O/binary, "]", _/binary>> ->
559 | {end_array, ?INC_COL(S)};
560 | <<_:O/binary, ",", _/binary>> ->
561 | {comma, ?INC_COL(S)};
562 | <<_:O/binary, ":", _/binary>> ->
563 | {colon, ?INC_COL(S)};
564 | <<_:O/binary, "null", _/binary>> ->
565 | {{const, null}, ?ADV_COL(S, 4)};
566 | <<_:O/binary, "true", _/binary>> ->
567 | {{const, true}, ?ADV_COL(S, 4)};
568 | <<_:O/binary, "false", _/binary>> ->
569 | {{const, false}, ?ADV_COL(S, 5)};
570 | <<_:O/binary, "\"", _/binary>> ->
571 | tokenize_string(B, ?INC_COL(S));
572 | <<_:O/binary, C, _/binary>> when (C >= $0 andalso C =< $9)
573 | orelse C =:= $- ->
574 | tokenize_number(B, S);
575 | <<_:O/binary>> ->
576 | trim = S#decoder.state,
577 | {eof, S}
578 | end.
579 | %%
580 | %% Tests
581 | %%
582 | -ifdef(TEST).
583 | -include_lib("eunit/include/eunit.hrl").
584 |
585 |
586 | %% testing constructs borrowed from the Yaws JSON implementation.
587 |
588 | %% Create an object from a list of Key/Value pairs.
589 |
590 | obj_new() ->
591 | {struct, []}.
592 |
593 | is_obj({struct, Props}) ->
594 | F = fun ({K, _}) when is_binary(K) -> true end,
595 | lists:all(F, Props).
596 |
597 | obj_from_list(Props) ->
598 | Obj = {struct, Props},
599 | ?assert(is_obj(Obj)),
600 | Obj.
601 |
602 | %% Test for equivalence of Erlang terms.
603 | %% Due to arbitrary order of construction, equivalent objects might
604 | %% compare unequal as erlang terms, so we need to carefully recurse
605 | %% through aggregates (tuples and objects).
606 |
607 | equiv({struct, Props1}, {struct, Props2}) ->
608 | equiv_object(Props1, Props2);
609 | equiv(L1, L2) when is_list(L1), is_list(L2) ->
610 | equiv_list(L1, L2);
611 | equiv(N1, N2) when is_number(N1), is_number(N2) -> N1 == N2;
612 | equiv(B1, B2) when is_binary(B1), is_binary(B2) -> B1 == B2;
613 | equiv(A, A) when A =:= true orelse A =:= false orelse A =:= null -> true.
614 |
615 | %% Object representation and traversal order is unknown.
616 | %% Use the sledgehammer and sort property lists.
617 |
618 | equiv_object(Props1, Props2) ->
619 | L1 = lists:keysort(1, Props1),
620 | L2 = lists:keysort(1, Props2),
621 | Pairs = lists:zip(L1, L2),
622 | true = lists:all(fun({{K1, V1}, {K2, V2}}) ->
623 | equiv(K1, K2) and equiv(V1, V2)
624 | end, Pairs).
625 |
626 | %% Recursively compare tuple elements for equivalence.
627 |
628 | equiv_list([], []) ->
629 | true;
630 | equiv_list([V1 | L1], [V2 | L2]) ->
631 | equiv(V1, V2) andalso equiv_list(L1, L2).
632 |
633 | decode_test() ->
634 | [1199344435545.0, 1] = decode(<<"[1199344435545.0,1]">>),
635 | <<16#F0,16#9D,16#9C,16#95>> = decode([34,"\\ud835","\\udf15",34]).
636 |
637 | e2j_vec_test() ->
638 | test_one(e2j_test_vec(utf8), 1).
639 |
640 | test_one([], _N) ->
641 | %% io:format("~p tests passed~n", [N-1]),
642 | ok;
643 | test_one([{E, J} | Rest], N) ->
644 | %% io:format("[~p] ~p ~p~n", [N, E, J]),
645 | true = equiv(E, decode(J)),
646 | true = equiv(E, decode(encode(E))),
647 | test_one(Rest, 1+N).
648 |
649 | e2j_test_vec(utf8) ->
650 | [
651 | {1, "1"},
652 | {3.1416, "3.14160"}, %% text representation may truncate, trail zeroes
653 | {-1, "-1"},
654 | {-3.1416, "-3.14160"},
655 | {12.0e10, "1.20000e+11"},
656 | {1.234E+10, "1.23400e+10"},
657 | {-1.234E-10, "-1.23400e-10"},
658 | {10.0, "1.0e+01"},
659 | {123.456, "1.23456E+2"},
660 | {10.0, "1e1"},
661 | {<<"foo">>, "\"foo\""},
662 | {<<"foo", 5, "bar">>, "\"foo\\u0005bar\""},
663 | {<<"">>, "\"\""},
664 | {<<"\n\n\n">>, "\"\\n\\n\\n\""},
665 | {<<"\" \b\f\r\n\t\"">>, "\"\\\" \\b\\f\\r\\n\\t\\\"\""},
666 | {obj_new(), "{}"},
667 | {obj_from_list([{<<"foo">>, <<"bar">>}]), "{\"foo\":\"bar\"}"},
668 | {obj_from_list([{<<"foo">>, <<"bar">>}, {<<"baz">>, 123}]),
669 | "{\"foo\":\"bar\",\"baz\":123}"},
670 | {[], "[]"},
671 | {[[]], "[[]]"},
672 | {[1, <<"foo">>], "[1,\"foo\"]"},
673 |
674 | %% json array in a json object
675 | {obj_from_list([{<<"foo">>, [123]}]),
676 | "{\"foo\":[123]}"},
677 |
678 | %% json object in a json object
679 | {obj_from_list([{<<"foo">>, obj_from_list([{<<"bar">>, true}])}]),
680 | "{\"foo\":{\"bar\":true}}"},
681 |
682 | %% fold evaluation order
683 | {obj_from_list([{<<"foo">>, []},
684 | {<<"bar">>, obj_from_list([{<<"baz">>, true}])},
685 | {<<"alice">>, <<"bob">>}]),
686 | "{\"foo\":[],\"bar\":{\"baz\":true},\"alice\":\"bob\"}"},
687 |
688 | %% json object in a json array
689 | {[-123, <<"foo">>, obj_from_list([{<<"bar">>, []}]), null],
690 | "[-123,\"foo\",{\"bar\":[]},null]"}
691 | ].
692 |
693 | %% test utf8 encoding
694 | encoder_utf8_test() ->
695 | %% safe conversion case (default)
696 | [34,"\\u0001","\\u0442","\\u0435","\\u0441","\\u0442",34] =
697 | encode(<<1,"\321\202\320\265\321\201\321\202">>),
698 |
699 | %% raw utf8 output (optional)
700 | Enc = mochijson2:encoder([{utf8, true}]),
701 | [34,"\\u0001",[209,130],[208,181],[209,129],[209,130],34] =
702 | Enc(<<1,"\321\202\320\265\321\201\321\202">>).
703 |
704 | input_validation_test() ->
705 | Good = [
706 | {16#00A3, <>}, %% pound
707 | {16#20AC, <>}, %% euro
708 | {16#10196, <>} %% denarius
709 | ],
710 | lists:foreach(fun({CodePoint, UTF8}) ->
711 | Expect = list_to_binary(xmerl_ucs:to_utf8(CodePoint)),
712 | Expect = decode(UTF8)
713 | end, Good),
714 |
715 | Bad = [
716 | %% 2nd, 3rd, or 4th byte of a multi-byte sequence w/o leading byte
717 | <>,
718 | %% missing continuations, last byte in each should be 80-BF
719 | <>,
720 | <>,
721 | <>,
722 | %% we don't support code points > 10FFFF per RFC 3629
723 | <>,
724 | %% escape characters trigger a different code path
725 | <>
726 | ],
727 | lists:foreach(
728 | fun(X) ->
729 | ok = try decode(X) catch invalid_utf8 -> ok end,
730 | %% could be {ucs,{bad_utf8_character_code}} or
731 | %% {json_encode,{bad_char,_}}
732 | {'EXIT', _} = (catch encode(X))
733 | end, Bad).
734 |
735 | inline_json_test() ->
736 | ?assertEqual(<<"\"iodata iodata\"">>,
737 | iolist_to_binary(
738 | encode({json, [<<"\"iodata">>, " iodata\""]}))),
739 | ?assertEqual({struct, [{<<"key">>, <<"iodata iodata">>}]},
740 | decode(
741 | encode({struct,
742 | [{key, {json, [<<"\"iodata">>, " iodata\""]}}]}))),
743 | ok.
744 |
745 | big_unicode_test() ->
746 | UTF8Seq = list_to_binary(xmerl_ucs:to_utf8(16#0001d120)),
747 | ?assertEqual(
748 | <<"\"\\ud834\\udd20\"">>,
749 | iolist_to_binary(encode(UTF8Seq))),
750 | ?assertEqual(
751 | UTF8Seq,
752 | decode(iolist_to_binary(encode(UTF8Seq)))),
753 | ok.
754 |
755 | custom_decoder_test() ->
756 | ?assertEqual(
757 | {struct, [{<<"key">>, <<"value">>}]},
758 | (decoder([]))("{\"key\": \"value\"}")),
759 | F = fun ({struct, [{<<"key">>, <<"value">>}]}) -> win end,
760 | ?assertEqual(
761 | win,
762 | (decoder([{object_hook, F}]))("{\"key\": \"value\"}")),
763 | ok.
764 |
765 | atom_test() ->
766 | %% JSON native atoms
767 | [begin
768 | ?assertEqual(A, decode(atom_to_list(A))),
769 | ?assertEqual(iolist_to_binary(atom_to_list(A)),
770 | iolist_to_binary(encode(A)))
771 | end || A <- [true, false, null]],
772 | %% Atom to string
773 | ?assertEqual(
774 | <<"\"foo\"">>,
775 | iolist_to_binary(encode(foo))),
776 | ?assertEqual(
777 | <<"\"\\ud834\\udd20\"">>,
778 | iolist_to_binary(encode(list_to_atom(xmerl_ucs:to_utf8(16#0001d120))))),
779 | ok.
780 |
781 | key_encode_test() ->
782 | %% Some forms are accepted as keys that would not be strings in other
783 | %% cases
784 | ?assertEqual(
785 | <<"{\"foo\":1}">>,
786 | iolist_to_binary(encode({struct, [{foo, 1}]}))),
787 | ?assertEqual(
788 | <<"{\"foo\":1}">>,
789 | iolist_to_binary(encode({struct, [{<<"foo">>, 1}]}))),
790 | ?assertEqual(
791 | <<"{\"foo\":1}">>,
792 | iolist_to_binary(encode({struct, [{"foo", 1}]}))),
793 | ?assertEqual(
794 | <<"{\"foo\":1}">>,
795 | iolist_to_binary(encode([{foo, 1}]))),
796 | ?assertEqual(
797 | <<"{\"foo\":1}">>,
798 | iolist_to_binary(encode([{<<"foo">>, 1}]))),
799 | ?assertEqual(
800 | <<"{\"foo\":1}">>,
801 | iolist_to_binary(encode([{"foo", 1}]))),
802 | ?assertEqual(
803 | <<"{\"\\ud834\\udd20\":1}">>,
804 | iolist_to_binary(
805 | encode({struct, [{[16#0001d120], 1}]}))),
806 | ?assertEqual(
807 | <<"{\"1\":1}">>,
808 | iolist_to_binary(encode({struct, [{1, 1}]}))),
809 | ok.
810 |
811 | unsafe_chars_test() ->
812 | Chars = "\"\\\b\f\n\r\t",
813 | [begin
814 | ?assertEqual(false, json_string_is_safe([C])),
815 | ?assertEqual(false, json_bin_is_safe(<>)),
816 | ?assertEqual(<>, decode(encode(<>)))
817 | end || C <- Chars],
818 | ?assertEqual(
819 | false,
820 | json_string_is_safe([16#0001d120])),
821 | ?assertEqual(
822 | false,
823 | json_bin_is_safe(list_to_binary(xmerl_ucs:to_utf8(16#0001d120)))),
824 | ?assertEqual(
825 | [16#0001d120],
826 | xmerl_ucs:from_utf8(
827 | binary_to_list(
828 | decode(encode(list_to_atom(xmerl_ucs:to_utf8(16#0001d120))))))),
829 | ?assertEqual(
830 | false,
831 | json_string_is_safe([16#110000])),
832 | ?assertEqual(
833 | false,
834 | json_bin_is_safe(list_to_binary(xmerl_ucs:to_utf8([16#110000])))),
835 | %% solidus can be escaped but isn't unsafe by default
836 | ?assertEqual(
837 | <<"/">>,
838 | decode(<<"\"\\/\"">>)),
839 | ok.
840 |
841 | int_test() ->
842 | ?assertEqual(0, decode("0")),
843 | ?assertEqual(1, decode("1")),
844 | ?assertEqual(11, decode("11")),
845 | ok.
846 |
847 | large_int_test() ->
848 | ?assertEqual(<<"-2147483649214748364921474836492147483649">>,
849 | iolist_to_binary(encode(-2147483649214748364921474836492147483649))),
850 | ?assertEqual(<<"2147483649214748364921474836492147483649">>,
851 | iolist_to_binary(encode(2147483649214748364921474836492147483649))),
852 | ok.
853 |
854 | float_test() ->
855 | ?assertEqual(<<"-2147483649.0">>, iolist_to_binary(encode(-2147483649.0))),
856 | ?assertEqual(<<"2147483648.0">>, iolist_to_binary(encode(2147483648.0))),
857 | ok.
858 |
859 | handler_test() ->
860 | ?assertEqual(
861 | {'EXIT',{json_encode,{bad_term,{x,y}}}},
862 | catch encode({x,y})),
863 | F = fun ({x,y}) -> [] end,
864 | ?assertEqual(
865 | <<"[]">>,
866 | iolist_to_binary((encoder([{handler, F}]))({x, y}))),
867 | ok.
868 |
869 | encode_empty_test_() ->
870 | [{A, ?_assertEqual(<<"{}">>, iolist_to_binary(encode(B)))}
871 | || {A, B} <- [{"eep18 {}", {}},
872 | {"eep18 {[]}", {[]}},
873 | {"{struct, []}", {struct, []}}]].
874 |
875 | encode_test_() ->
876 | P = [{<<"k">>, <<"v">>}],
877 | JSON = iolist_to_binary(encode({struct, P})),
878 | [{atom_to_list(F),
879 | ?_assertEqual(JSON, iolist_to_binary(encode(decode(JSON, [{format, F}]))))}
880 | || F <- [struct, eep18, proplist]].
881 |
882 | format_test_() ->
883 | P = [{<<"k">>, <<"v">>}],
884 | JSON = iolist_to_binary(encode({struct, P})),
885 | [{atom_to_list(F),
886 | ?_assertEqual(A, decode(JSON, [{format, F}]))}
887 | || {F, A} <- [{struct, {struct, P}},
888 | {eep18, {P}},
889 | {proplist, P}]].
890 |
891 | -endif.
892 |
--------------------------------------------------------------------------------
/jquery-1.5.min.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * jQuery JavaScript Library v1.5
3 | * http://jquery.com/
4 | *
5 | * Copyright 2011, John Resig
6 | * Dual licensed under the MIT or GPL Version 2 licenses.
7 | * http://jquery.org/license
8 | *
9 | * Includes Sizzle.js
10 | * http://sizzlejs.com/
11 | * Copyright 2011, The Dojo Foundation
12 | * Released under the MIT, BSD, and GPL Licenses.
13 | *
14 | * Date: Mon Jan 31 08:31:29 2011 -0500
15 | */
16 | (function(a,b){function b$(a){return d.isWindow(a)?a:a.nodeType===9?a.defaultView||a.parentWindow:!1}function bX(a){if(!bR[a]){var b=d("<"+a+">").appendTo("body"),c=b.css("display");b.remove();if(c==="none"||c==="")c="block";bR[a]=c}return bR[a]}function bW(a,b){var c={};d.each(bV.concat.apply([],bV.slice(0,b)),function(){c[this]=a});return c}function bJ(a,c){a.dataFilter&&(c=a.dataFilter(c,a.dataType));var e=a.dataTypes,f=a.converters,g,h=e.length,i,j=e[0],k,l,m,n,o;for(g=1;g=0===c})}function N(a){return!a||!a.parentNode||a.parentNode.nodeType===11}function F(a,b){return(a&&a!=="*"?a+".":"")+b.replace(q,"`").replace(r,"&")}function E(a){var b,c,e,f,g,h,i,j,k,l,m,n,p,q=[],r=[],s=d._data(this,u);typeof s==="function"&&(s=s.events);if(a.liveFired!==this&&s&&s.live&&!a.target.disabled&&(!a.button||a.type!=="click")){a.namespace&&(n=new RegExp("(^|\\.)"+a.namespace.split(".").join("\\.(?:.*\\.)?")+"(\\.|$)")),a.liveFired=this;var t=s.live.slice(0);for(i=0;ic)break;a.currentTarget=f.elem,a.data=f.handleObj.data,a.handleObj=f.handleObj,p=f.handleObj.origHandler.apply(f.elem,arguments);if(p===!1||a.isPropagationStopped()){c=f.level,p===!1&&(b=!1);if(a.isImmediatePropagationStopped())break}}return b}}function C(a,b,c){c[0].type=a;return d.event.handle.apply(b,c)}function w(){return!0}function v(){return!1}function f(a,c,f){if(f===b&&a.nodeType===1){f=a.getAttribute("data-"+c);if(typeof f==="string"){try{f=f==="true"?!0:f==="false"?!1:f==="null"?null:d.isNaN(f)?e.test(f)?d.parseJSON(f):f:parseFloat(f)}catch(g){}d.data(a,c,f)}else f=b}return f}var c=a.document,d=function(){function I(){if(!d.isReady){try{c.documentElement.doScroll("left")}catch(a){setTimeout(I,1);return}d.ready()}}var d=function(a,b){return new d.fn.init(a,b,g)},e=a.jQuery,f=a.$,g,h=/^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,i=/\S/,j=/^\s+/,k=/\s+$/,l=/\d/,m=/^<(\w+)\s*\/?>(?:<\/\1>)?$/,n=/^[\],:{}\s]*$/,o=/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,p=/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,q=/(?:^|:|,)(?:\s*\[)+/g,r=/(webkit)[ \/]([\w.]+)/,s=/(opera)(?:.*version)?[ \/]([\w.]+)/,t=/(msie) ([\w.]+)/,u=/(mozilla)(?:.*? rv:([\w.]+))?/,v=navigator.userAgent,w,x=!1,y,z="then done fail isResolved isRejected promise".split(" "),A,B=Object.prototype.toString,C=Object.prototype.hasOwnProperty,D=Array.prototype.push,E=Array.prototype.slice,F=String.prototype.trim,G=Array.prototype.indexOf,H={};d.fn=d.prototype={constructor:d,init:function(a,e,f){var g,i,j,k;if(!a)return this;if(a.nodeType){this.context=this[0]=a,this.length=1;return this}if(a==="body"&&!e&&c.body){this.context=c,this[0]=c.body,this.selector="body",this.length=1;return this}if(typeof a==="string"){g=h.exec(a);if(!g||!g[1]&&e)return!e||e.jquery?(e||f).find(a):this.constructor(e).find(a);if(g[1]){e=e instanceof d?e[0]:e,k=e?e.ownerDocument||e:c,j=m.exec(a),j?d.isPlainObject(e)?(a=[c.createElement(j[1])],d.fn.attr.call(a,e,!0)):a=[k.createElement(j[1])]:(j=d.buildFragment([g[1]],[k]),a=(j.cacheable?d.clone(j.fragment):j.fragment).childNodes);return d.merge(this,a)}i=c.getElementById(g[2]);if(i&&i.parentNode){if(i.id!==g[2])return f.find(a);this.length=1,this[0]=i}this.context=c,this.selector=a;return this}if(d.isFunction(a))return f.ready(a);a.selector!==b&&(this.selector=a.selector,this.context=a.context);return d.makeArray(a,this)},selector:"",jquery:"1.5",length:0,size:function(){return this.length},toArray:function(){return E.call(this,0)},get:function(a){return a==null?this.toArray():a<0?this[this.length+a]:this[a]},pushStack:function(a,b,c){var e=this.constructor();d.isArray(a)?D.apply(e,a):d.merge(e,a),e.prevObject=this,e.context=this.context,b==="find"?e.selector=this.selector+(this.selector?" ":"")+c:b&&(e.selector=this.selector+"."+b+"("+c+")");return e},each:function(a,b){return d.each(this,a,b)},ready:function(a){d.bindReady(),y.done(a);return this},eq:function(a){return a===-1?this.slice(a):this.slice(a,+a+1)},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},slice:function(){return this.pushStack(E.apply(this,arguments),"slice",E.call(arguments).join(","))},map:function(a){return this.pushStack(d.map(this,function(b,c){return a.call(b,c,b)}))},end:function(){return this.prevObject||this.constructor(null)},push:D,sort:[].sort,splice:[].splice},d.fn.init.prototype=d.fn,d.extend=d.fn.extend=function(){var a,c,e,f,g,h,i=arguments[0]||{},j=1,k=arguments.length,l=!1;typeof i==="boolean"&&(l=i,i=arguments[1]||{},j=2),typeof i!=="object"&&!d.isFunction(i)&&(i={}),k===j&&(i=this,--j);for(;j0)return;y.resolveWith(c,[d]),d.fn.trigger&&d(c).trigger("ready").unbind("ready")}},bindReady:function(){if(!x){x=!0;if(c.readyState==="complete")return setTimeout(d.ready,1);if(c.addEventListener)c.addEventListener("DOMContentLoaded",A,!1),a.addEventListener("load",d.ready,!1);else if(c.attachEvent){c.attachEvent("onreadystatechange",A),a.attachEvent("onload",d.ready);var b=!1;try{b=a.frameElement==null}catch(e){}c.documentElement.doScroll&&b&&I()}}},isFunction:function(a){return d.type(a)==="function"},isArray:Array.isArray||function(a){return d.type(a)==="array"},isWindow:function(a){return a&&typeof a==="object"&&"setInterval"in a},isNaN:function(a){return a==null||!l.test(a)||isNaN(a)},type:function(a){return a==null?String(a):H[B.call(a)]||"object"},isPlainObject:function(a){if(!a||d.type(a)!=="object"||a.nodeType||d.isWindow(a))return!1;if(a.constructor&&!C.call(a,"constructor")&&!C.call(a.constructor.prototype,"isPrototypeOf"))return!1;var c;for(c in a){}return c===b||C.call(a,c)},isEmptyObject:function(a){for(var b in a)return!1;return!0},error:function(a){throw a},parseJSON:function(b){if(typeof b!=="string"||!b)return null;b=d.trim(b);if(n.test(b.replace(o,"@").replace(p,"]").replace(q,"")))return a.JSON&&a.JSON.parse?a.JSON.parse(b):(new Function("return "+b))();d.error("Invalid JSON: "+b)},parseXML:function(b,c,e){a.DOMParser?(e=new DOMParser,c=e.parseFromString(b,"text/xml")):(c=new ActiveXObject("Microsoft.XMLDOM"),c.async="false",c.loadXML(b)),e=c.documentElement,(!e||!e.nodeName||e.nodeName==="parsererror")&&d.error("Invalid XML: "+b);return c},noop:function(){},globalEval:function(a){if(a&&i.test(a)){var b=c.getElementsByTagName("head")[0]||c.documentElement,e=c.createElement("script");e.type="text/javascript",d.support.scriptEval()?e.appendChild(c.createTextNode(a)):e.text=a,b.insertBefore(e,b.firstChild),b.removeChild(e)}},nodeName:function(a,b){return a.nodeName&&a.nodeName.toUpperCase()===b.toUpperCase()},each:function(a,c,e){var f,g=0,h=a.length,i=h===b||d.isFunction(a);if(e){if(i){for(f in a)if(c.apply(a[f],e)===!1)break}else for(;g1?(g=Array(c),d.each(b,function(a,b){d.when(b).then(function(b){g[a]=arguments.length>1?E.call(arguments,0):b,--c||e.resolveWith(f,g)},e.reject)})):e!==a&&e.resolve(a);return f},uaMatch:function(a){a=a.toLowerCase();var b=r.exec(a)||s.exec(a)||t.exec(a)||a.indexOf("compatible")<0&&u.exec(a)||[];return{browser:b[1]||"",version:b[2]||"0"}},sub:function(){function a(b,c){return new a.fn.init(b,c)}d.extend(!0,a,this),a.superclass=this,a.fn=a.prototype=this(),a.fn.constructor=a,a.subclass=this.subclass,a.fn.init=function b(b,c){c&&c instanceof d&&!(c instanceof a)&&(c=a(c));return d.fn.init.call(this,b,c,e)},a.fn.init.prototype=a.fn;var e=a(c);return a},browser:{}}),y=d._Deferred(),d.each("Boolean Number String Function Array Date RegExp Object".split(" "),function(a,b){H["[object "+b+"]"]=b.toLowerCase()}),w=d.uaMatch(v),w.browser&&(d.browser[w.browser]=!0,d.browser.version=w.version),d.browser.webkit&&(d.browser.safari=!0),G&&(d.inArray=function(a,b){return G.call(b,a)}),i.test(" ")&&(j=/^[\s\xA0]+/,k=/[\s\xA0]+$/),g=d(c),c.addEventListener?A=function(){c.removeEventListener("DOMContentLoaded",A,!1),d.ready()}:c.attachEvent&&(A=function(){c.readyState==="complete"&&(c.detachEvent("onreadystatechange",A),d.ready())});return a.jQuery=a.$=d}();(function(){d.support={};var b=c.createElement("div");b.style.display="none",b.innerHTML=" a ";var e=b.getElementsByTagName("*"),f=b.getElementsByTagName("a")[0],g=c.createElement("select"),h=g.appendChild(c.createElement("option"));if(e&&e.length&&f){d.support={leadingWhitespace:b.firstChild.nodeType===3,tbody:!b.getElementsByTagName("tbody").length,htmlSerialize:!!b.getElementsByTagName("link").length,style:/red/.test(f.getAttribute("style")),hrefNormalized:f.getAttribute("href")==="/a",opacity:/^0.55$/.test(f.style.opacity),cssFloat:!!f.style.cssFloat,checkOn:b.getElementsByTagName("input")[0].value==="on",optSelected:h.selected,deleteExpando:!0,optDisabled:!1,checkClone:!1,_scriptEval:null,noCloneEvent:!0,boxModel:null,inlineBlockNeedsLayout:!1,shrinkWrapBlocks:!1,reliableHiddenOffsets:!0},g.disabled=!0,d.support.optDisabled=!h.disabled,d.support.scriptEval=function(){if(d.support._scriptEval===null){var b=c.documentElement,e=c.createElement("script"),f="script"+d.now();e.type="text/javascript";try{e.appendChild(c.createTextNode("window."+f+"=1;"))}catch(g){}b.insertBefore(e,b.firstChild),a[f]?(d.support._scriptEval=!0,delete a[f]):d.support._scriptEval=!1,b.removeChild(e),b=e=f=null}return d.support._scriptEval};try{delete b.test}catch(i){d.support.deleteExpando=!1}b.attachEvent&&b.fireEvent&&(b.attachEvent("onclick",function j(){d.support.noCloneEvent=!1,b.detachEvent("onclick",j)}),b.cloneNode(!0).fireEvent("onclick")),b=c.createElement("div"),b.innerHTML=" ";var k=c.createDocumentFragment();k.appendChild(b.firstChild),d.support.checkClone=k.cloneNode(!0).cloneNode(!0).lastChild.checked,d(function(){var a=c.createElement("div"),b=c.getElementsByTagName("body")[0];if(b){a.style.width=a.style.paddingLeft="1px",b.appendChild(a),d.boxModel=d.support.boxModel=a.offsetWidth===2,"zoom"in a.style&&(a.style.display="inline",a.style.zoom=1,d.support.inlineBlockNeedsLayout=a.offsetWidth===2,a.style.display="",a.innerHTML="
",d.support.shrinkWrapBlocks=a.offsetWidth!==2),a.innerHTML="";var e=a.getElementsByTagName("td");d.support.reliableHiddenOffsets=e[0].offsetHeight===0,e[0].style.display="",e[1].style.display="none",d.support.reliableHiddenOffsets=d.support.reliableHiddenOffsets&&e[0].offsetHeight===0,a.innerHTML="",b.removeChild(a).style.display="none",a=e=null}});var l=function(a){var b=c.createElement("div");a="on"+a;if(!b.attachEvent)return!0;var d=a in b;d||(b.setAttribute(a,"return;"),d=typeof b[a]==="function"),b=null;return d};d.support.submitBubbles=l("submit"),d.support.changeBubbles=l("change"),b=e=f=null}})();var e=/^(?:\{.*\}|\[.*\])$/;d.extend({cache:{},uuid:0,expando:"jQuery"+(d.fn.jquery+Math.random()).replace(/\D/g,""),noData:{embed:!0,object:"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",applet:!0},hasData:function(a){a=a.nodeType?d.cache[a[d.expando]]:a[d.expando];return!!a&&!d.isEmptyObject(a)},data:function(a,c,e,f){if(d.acceptData(a)){var g=d.expando,h=typeof c==="string",i,j=a.nodeType,k=j?d.cache:a,l=j?a[d.expando]:a[d.expando]&&d.expando;if((!l||f&&l&&!k[l][g])&&h&&e===b)return;l||(j?a[d.expando]=l=++d.uuid:l=d.expando),k[l]||(k[l]={}),typeof c==="object"&&(f?k[l][g]=d.extend(k[l][g],c):k[l]=d.extend(k[l],c)),i=k[l],f&&(i[g]||(i[g]={}),i=i[g]),e!==b&&(i[c]=e);if(c==="events"&&!i[c])return i[g]&&i[g].events;return h?i[c]:i}},removeData:function(b,c,e){if(d.acceptData(b)){var f=d.expando,g=b.nodeType,h=g?d.cache:b,i=g?b[d.expando]:d.expando;if(!h[i])return;if(c){var j=e?h[i][f]:h[i];if(j){delete j[c];if(!d.isEmptyObject(j))return}}if(e){delete h[i][f];if(!d.isEmptyObject(h[i]))return}var k=h[i][f];d.support.deleteExpando||h!=a?delete h[i]:h[i]=null,k?(h[i]={},h[i][f]=k):g&&(d.support.deleteExpando?delete b[d.expando]:b.removeAttribute?b.removeAttribute(d.expando):b[d.expando]=null)}},_data:function(a,b,c){return d.data(a,b,c,!0)},acceptData:function(a){if(a.nodeName){var b=d.noData[a.nodeName.toLowerCase()];if(b)return b!==!0&&a.getAttribute("classid")===b}return!0}}),d.fn.extend({data:function(a,c){var e=null;if(typeof a==="undefined"){if(this.length){e=d.data(this[0]);if(this[0].nodeType===1){var g=this[0].attributes,h;for(var i=0,j=g.length;i-1)return!0;return!1},val:function(a){if(!arguments.length){var c=this[0];if(c){if(d.nodeName(c,"option")){var e=c.attributes.value;return!e||e.specified?c.value:c.text}if(d.nodeName(c,"select")){var f=c.selectedIndex,g=[],h=c.options,j=c.type==="select-one";if(f<0)return null;for(var k=j?f:0,l=j?f+1:h.length;k=0;else if(d.nodeName(this,"select")){var f=d.makeArray(e);d("option",this).each(function(){this.selected=d.inArray(d(this).val(),f)>=0}),f.length||(this.selectedIndex=-1)}else this.value=e}})}}),d.extend({attrFn:{val:!0,css:!0,html:!0,text:!0,data:!0,width:!0,height:!0,offset:!0},attr:function(a,c,e,f){if(!a||a.nodeType===3||a.nodeType===8||a.nodeType===2)return b;if(f&&c in d.attrFn)return d(a)[c](e);var g=a.nodeType!==1||!d.isXMLDoc(a),h=e!==b;c=g&&d.props[c]||c;if(a.nodeType===1){var i=j.test(c);if(c==="selected"&&!d.support.optSelected){var n=a.parentNode;n&&(n.selectedIndex,n.parentNode&&n.parentNode.selectedIndex)}if((c in a||a[c]!==b)&&g&&!i){h&&(c==="type"&&k.test(a.nodeName)&&a.parentNode&&d.error("type property can't be changed"),e===null?a.nodeType===1&&a.removeAttribute(c):a[c]=e);if(d.nodeName(a,"form")&&a.getAttributeNode(c))return a.getAttributeNode(c).nodeValue;if(c==="tabIndex"){var o=a.getAttributeNode("tabIndex");return o&&o.specified?o.value:l.test(a.nodeName)||m.test(a.nodeName)&&a.href?0:b}return a[c]}if(!d.support.style&&g&&c==="style"){h&&(a.style.cssText=""+e);return a.style.cssText}h&&a.setAttribute(c,""+e);if(!a.attributes[c]&&(a.hasAttribute&&!a.hasAttribute(c)))return b;var p=!d.support.hrefNormalized&&g&&i?a.getAttribute(c,2):a.getAttribute(c);return p===null?b:p}h&&(a[c]=e);return a[c]}});var o=/\.(.*)$/,p=/^(?:textarea|input|select)$/i,q=/\./g,r=/ /g,s=/[^\w\s.|`]/g,t=function(a){return a.replace(s,"\\$&")},u="events";d.event={add:function(c,e,f,g){if(c.nodeType!==3&&c.nodeType!==8){d.isWindow(c)&&(c!==a&&!c.frameElement)&&(c=a);if(f===!1)f=v;else if(!f)return;var h,i;f.handler&&(h=f,f=h.handler),f.guid||(f.guid=d.guid++);var j=d._data(c);if(!j)return;var k=j[u],l=j.handle;typeof k==="function"?(l=k.handle,k=k.events):k||(c.nodeType||(j[u]=j=function(){}),j.events=k={}),l||(j.handle=l=function(){return typeof d!=="undefined"&&!d.event.triggered?d.event.handle.apply(l.elem,arguments):b}),l.elem=c,e=e.split(" ");var m,n=0,o;while(m=e[n++]){i=h?d.extend({},h):{handler:f,data:g},m.indexOf(".")>-1?(o=m.split("."),m=o.shift(),i.namespace=o.slice(0).sort().join(".")):(o=[],i.namespace=""),i.type=m,i.guid||(i.guid=f.guid);var p=k[m],q=d.event.special[m]||{};if(!p){p=k[m]=[];if(!q.setup||q.setup.call(c,g,o,l)===!1)c.addEventListener?c.addEventListener(m,l,!1):c.attachEvent&&c.attachEvent("on"+m,l)}q.add&&(q.add.call(c,i),i.handler.guid||(i.handler.guid=f.guid)),p.push(i),d.event.global[m]=!0}c=null}},global:{},remove:function(a,c,e,f){if(a.nodeType!==3&&a.nodeType!==8){e===!1&&(e=v);var g,h,i,j,k=0,l,m,n,o,p,q,r,s=d.hasData(a)&&d._data(a),w=s&&s[u];if(!s||!w)return;typeof w==="function"&&(s=w,w=w.events),c&&c.type&&(e=c.handler,c=c.type);if(!c||typeof c==="string"&&c.charAt(0)==="."){c=c||"";for(h in w)d.event.remove(a,h+c);return}c=c.split(" ");while(h=c[k++]){r=h,q=null,l=h.indexOf(".")<0,m=[],l||(m=h.split("."),h=m.shift(),n=new RegExp("(^|\\.)"+d.map(m.slice(0).sort(),t).join("\\.(?:.*\\.)?")+"(\\.|$)")),p=w[h];if(!p)continue;if(!e){for(j=0;j=0&&(a.type=f=f.slice(0,-1),a.exclusive=!0),e||(a.stopPropagation(),d.event.global[f]&&d.each(d.cache,function(){var b=d.expando,e=this[b];e&&e.events&&e.events[f]&&d.event.trigger(a,c,e.handle.elem)}));if(!e||e.nodeType===3||e.nodeType===8)return b;a.result=b,a.target=e,c=d.makeArray(c),c.unshift(a)}a.currentTarget=e;var h=e.nodeType?d._data(e,"handle"):(d._data(e,u)||{}).handle;h&&h.apply(e,c);var i=e.parentNode||e.ownerDocument;try{e&&e.nodeName&&d.noData[e.nodeName.toLowerCase()]||e["on"+f]&&e["on"+f].apply(e,c)===!1&&(a.result=!1,a.preventDefault())}catch(j){}if(!a.isPropagationStopped()&&i)d.event.trigger(a,c,i,!0);else if(!a.isDefaultPrevented()){var k,l=a.target,m=f.replace(o,""),n=d.nodeName(l,"a")&&m==="click",p=d.event.special[m]||{};if((!p._default||p._default.call(e,a)===!1)&&!n&&!(l&&l.nodeName&&d.noData[l.nodeName.toLowerCase()])){try{l[m]&&(k=l["on"+m],k&&(l["on"+m]=null),d.event.triggered=!0,l[m]())}catch(q){}k&&(l["on"+m]=k),d.event.triggered=!1}}},handle:function(c){var e,f,g,h,i,j=[],k=d.makeArray(arguments);c=k[0]=d.event.fix(c||a.event),c.currentTarget=this,e=c.type.indexOf(".")<0&&!c.exclusive,e||(g=c.type.split("."),c.type=g.shift(),j=g.slice(0).sort(),h=new RegExp("(^|\\.)"+j.join("\\.(?:.*\\.)?")+"(\\.|$)")),c.namespace=c.namespace||j.join("."),i=d._data(this,u),typeof i==="function"&&(i=i.events),f=(i||{})[c.type];if(i&&f){f=f.slice(0);for(var l=0,m=f.length;l-1?d.map(a.options,function(a){return a.selected}).join("-"):"":a.nodeName.toLowerCase()==="select"&&(c=a.selectedIndex);return c},B=function B(a){var c=a.target,e,f;if(p.test(c.nodeName)&&!c.readOnly){e=d._data(c,"_change_data"),f=A(c),(a.type!=="focusout"||c.type!=="radio")&&d._data(c,"_change_data",f);if(e===b||f===e)return;if(e!=null||f){a.type="change",a.liveFired=b;return d.event.trigger(a,arguments[1],c)}}};d.event.special.change={filters:{focusout:B,beforedeactivate:B,click:function(a){var b=a.target,c=b.type;if(c==="radio"||c==="checkbox"||b.nodeName.toLowerCase()==="select")return B.call(this,a)},keydown:function(a){var b=a.target,c=b.type;if(a.keyCode===13&&b.nodeName.toLowerCase()!=="textarea"||a.keyCode===32&&(c==="checkbox"||c==="radio")||c==="select-multiple")return B.call(this,a)},beforeactivate:function(a){var b=a.target;d._data(b,"_change_data",A(b))}},setup:function(a,b){if(this.type==="file")return!1;for(var c in z)d.event.add(this,c+".specialChange",z[c]);return p.test(this.nodeName)},teardown:function(a){d.event.remove(this,".specialChange");return p.test(this.nodeName)}},z=d.event.special.change.filters,z.focus=z.beforeactivate}c.addEventListener&&d.each({focus:"focusin",blur:"focusout"},function(a,b){function c(a){a=d.event.fix(a),a.type=b;return d.event.handle.call(this,a)}d.event.special[b]={setup:function(){this.addEventListener(a,c,!0)},teardown:function(){this.removeEventListener(a,c,!0)}}}),d.each(["bind","one"],function(a,c){d.fn[c]=function(a,e,f){if(typeof a==="object"){for(var g in a)this[c](g,e,a[g],f);return this}if(d.isFunction(e)||e===!1)f=e,e=b;var h=c==="one"?d.proxy(f,function(a){d(this).unbind(a,h);return f.apply(this,arguments)}):f;if(a==="unload"&&c!=="one")this.one(a,e,f);else for(var i=0,j=this.length;i0?this.bind(b,a,c):this.trigger(b)},d.attrFn&&(d.attrFn[b]=!0)}),function(){function s(a,b,c,d,e,f){for(var g=0,h=d.length;g0){k=j;break}}j=j[a]}d[g]=k}}}function r(a,b,c,d,e,f){for(var g=0,h=d.length;g+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,e=0,f=Object.prototype.toString,g=!1,h=!0;[0,0].sort(function(){h=!1;return 0});var i=function(b,d,e,g){e=e||[],d=d||c;var h=d;if(d.nodeType!==1&&d.nodeType!==9)return[];if(!b||typeof b!=="string")return e;var l,m,o,p,q,r,s,u,v=!0,w=i.isXML(d),x=[],y=b;do{a.exec(""),l=a.exec(y);if(l){y=l[3],x.push(l[1]);if(l[2]){p=l[3];break}}}while(l);if(x.length>1&&k.exec(b))if(x.length===2&&j.relative[x[0]])m=t(x[0]+x[1],d);else{m=j.relative[x[0]]?[d]:i(x.shift(),d);while(x.length)b=x.shift(),j.relative[b]&&(b+=x.shift()),m=t(b,m)}else{!g&&x.length>1&&d.nodeType===9&&!w&&j.match.ID.test(x[0])&&!j.match.ID.test(x[x.length-1])&&(q=i.find(x.shift(),d,w),d=q.expr?i.filter(q.expr,q.set)[0]:q.set[0]);if(d){q=g?{expr:x.pop(),set:n(g)}:i.find(x.pop(),x.length===1&&(x[0]==="~"||x[0]==="+")&&d.parentNode?d.parentNode:d,w),m=q.expr?i.filter(q.expr,q.set):q.set,x.length>0?o=n(m):v=!1;while(x.length)r=x.pop(),s=r,j.relative[r]?s=x.pop():r="",s==null&&(s=d),j.relative[r](o,s,w)}else o=x=[]}o||(o=m),o||i.error(r||b);if(f.call(o)==="[object Array]")if(v)if(d&&d.nodeType===1)for(u=0;o[u]!=null;u++)o[u]&&(o[u]===!0||o[u].nodeType===1&&i.contains(d,o[u]))&&e.push(m[u]);else for(u=0;o[u]!=null;u++)o[u]&&o[u].nodeType===1&&e.push(m[u]);else e.push.apply(e,o);else n(o,e);p&&(i(p,h,e,g),i.uniqueSort(e));return e};i.uniqueSort=function(a){if(p){g=h,a.sort(p);if(g)for(var b=1;b0},i.find=function(a,b,c){var d;if(!a)return[];for(var e=0,f=j.order.length;e":function(a,b){var c,d=typeof b==="string",e=0,f=a.length;if(d&&!/\W/.test(b)){b=b.toLowerCase();for(;e=0)?c||d.push(h):c&&(b[g]=!1));return!1},ID:function(a){return a[1].replace(/\\/g,"")},TAG:function(a,b){return a[1].toLowerCase()},CHILD:function(a){if(a[1]==="nth"){a[2]||i.error(a[0]),a[2]=a[2].replace(/^\+|\s*/g,"");var b=/(-?)(\d*)(?:n([+\-]?\d*))?/.exec(a[2]==="even"&&"2n"||a[2]==="odd"&&"2n+1"||!/\D/.test(a[2])&&"0n+"+a[2]||a[2]);a[2]=b[1]+(b[2]||1)-0,a[3]=b[3]-0}else a[2]&&i.error(a[0]);a[0]=e++;return a},ATTR:function(a,b,c,d,e,f){var g=a[1]=a[1].replace(/\\/g,"");!f&&j.attrMap[g]&&(a[1]=j.attrMap[g]),a[4]=(a[4]||a[5]||"").replace(/\\/g,""),a[2]==="~="&&(a[4]=" "+a[4]+" ");return a},PSEUDO:function(b,c,d,e,f){if(b[1]==="not")if((a.exec(b[3])||"").length>1||/^\w/.test(b[3]))b[3]=i(b[3],null,null,c);else{var g=i.filter(b[3],c,d,!0^f);d||e.push.apply(e,g);return!1}else if(j.match.POS.test(b[0])||j.match.CHILD.test(b[0]))return!0;return b},POS:function(a){a.unshift(!0);return a}},filters:{enabled:function(a){return a.disabled===!1&&a.type!=="hidden"},disabled:function(a){return a.disabled===!0},checked:function(a){return a.checked===!0},selected:function(a){a.parentNode.selectedIndex;return a.selected===!0},parent:function(a){return!!a.firstChild},empty:function(a){return!a.firstChild},has:function(a,b,c){return!!i(c[3],a).length},header:function(a){return/h\d/i.test(a.nodeName)},text:function(a){return"text"===a.type},radio:function(a){return"radio"===a.type},checkbox:function(a){return"checkbox"===a.type},file:function(a){return"file"===a.type},password:function(a){return"password"===a.type},submit:function(a){return"submit"===a.type},image:function(a){return"image"===a.type},reset:function(a){return"reset"===a.type},button:function(a){return"button"===a.type||a.nodeName.toLowerCase()==="button"},input:function(a){return/input|select|textarea|button/i.test(a.nodeName)}},setFilters:{first:function(a,b){return b===0},last:function(a,b,c,d){return b===d.length-1},even:function(a,b){return b%2===0},odd:function(a,b){return b%2===1},lt:function(a,b,c){return bc[3]-0},nth:function(a,b,c){return c[3]-0===b},eq:function(a,b,c){return c[3]-0===b}},filter:{PSEUDO:function(a,b,c,d){var e=b[1],f=j.filters[e];if(f)return f(a,c,b,d);if(e==="contains")return(a.textContent||a.innerText||i.getText([a])||"").indexOf(b[3])>=0;if(e==="not"){var g=b[3];for(var h=0,k=g.length;h=0}},ID:function(a,b){return a.nodeType===1&&a.getAttribute("id")===b},TAG:function(a,b){return b==="*"&&a.nodeType===1||a.nodeName.toLowerCase()===b},CLASS:function(a,b){return(" "+(a.className||a.getAttribute("class"))+" ").indexOf(b)>-1},ATTR:function(a,b){var c=b[1],d=j.attrHandle[c]?j.attrHandle[c](a):a[c]!=null?a[c]:a.getAttribute(c),e=d+"",f=b[2],g=b[4];return d==null?f==="!=":f==="="?e===g:f==="*="?e.indexOf(g)>=0:f==="~="?(" "+e+" ").indexOf(g)>=0:g?f==="!="?e!==g:f==="^="?e.indexOf(g)===0:f==="$="?e.substr(e.length-g.length)===g:f==="|="?e===g||e.substr(0,g.length+1)===g+"-":!1:e&&d!==!1},POS:function(a,b,c,d){var e=b[2],f=j.setFilters[e];if(f)return f(a,c,b,d)}}},k=j.match.POS,l=function(a,b){return"\\"+(b-0+1)};for(var m in j.match)j.match[m]=new RegExp(j.match[m].source+/(?![^\[]*\])(?![^\(]*\))/.source),j.leftMatch[m]=new RegExp(/(^(?:.|\r|\n)*?)/.source+j.match[m].source.replace(/\\(\d+)/g,l));var n=function(a,b){a=Array.prototype.slice.call(a,0);if(b){b.push.apply(b,a);return b}return a};try{Array.prototype.slice.call(c.documentElement.childNodes,0)[0].nodeType}catch(o){n=function(a,b){var c=0,d=b||[];if(f.call(a)==="[object Array]")Array.prototype.push.apply(d,a);else if(typeof a.length==="number")for(var e=a.length;c ",e.insertBefore(a,e.firstChild),c.getElementById(d)&&(j.find.ID=function(a,c,d){if(typeof c.getElementById!=="undefined"&&!d){var e=c.getElementById(a[1]);return e?e.id===a[1]||typeof e.getAttributeNode!=="undefined"&&e.getAttributeNode("id").nodeValue===a[1]?[e]:b:[]}},j.filter.ID=function(a,b){var c=typeof a.getAttributeNode!=="undefined"&&a.getAttributeNode("id");return a.nodeType===1&&c&&c.nodeValue===b}),e.removeChild(a),e=a=null}(),function(){var a=c.createElement("div");a.appendChild(c.createComment("")),a.getElementsByTagName("*").length>0&&(j.find.TAG=function(a,b){var c=b.getElementsByTagName(a[1]);if(a[1]==="*"){var d=[];for(var e=0;c[e];e++)c[e].nodeType===1&&d.push(c[e]);c=d}return c}),a.innerHTML=" ",a.firstChild&&typeof a.firstChild.getAttribute!=="undefined"&&a.firstChild.getAttribute("href")!=="#"&&(j.attrHandle.href=function(a){return a.getAttribute("href",2)}),a=null}(),c.querySelectorAll&&function(){var a=i,b=c.createElement("div"),d="__sizzle__";b.innerHTML="
";if(!b.querySelectorAll||b.querySelectorAll(".TEST").length!==0){i=function(b,e,f,g){e=e||c;if(!g&&!i.isXML(e)){var h=/^(\w+$)|^\.([\w\-]+$)|^#([\w\-]+$)/.exec(b);if(h&&(e.nodeType===1||e.nodeType===9)){if(h[1])return n(e.getElementsByTagName(b),f);if(h[2]&&j.find.CLASS&&e.getElementsByClassName)return n(e.getElementsByClassName(h[2]),f)}if(e.nodeType===9){if(b==="body"&&e.body)return n([e.body],f);if(h&&h[3]){var k=e.getElementById(h[3]);if(!k||!k.parentNode)return n([],f);if(k.id===h[3])return n([k],f)}try{return n(e.querySelectorAll(b),f)}catch(l){}}else if(e.nodeType===1&&e.nodeName.toLowerCase()!=="object"){var m=e.getAttribute("id"),o=m||d,p=e.parentNode,q=/^\s*[+~]/.test(b);m?o=o.replace(/'/g,"\\$&"):e.setAttribute("id",o),q&&p&&(e=e.parentNode);try{if(!q||p)return n(e.querySelectorAll("[id='"+o+"'] "+b),f)}catch(r){}finally{m||e.removeAttribute("id")}}}return a(b,e,f,g)};for(var e in a)i[e]=a[e];b=null}}(),function(){var a=c.documentElement,b=a.matchesSelector||a.mozMatchesSelector||a.webkitMatchesSelector||a.msMatchesSelector,d=!1;try{b.call(c.documentElement,"[test!='']:sizzle")}catch(e){d=!0}b&&(i.matchesSelector=function(a,c){c=c.replace(/\=\s*([^'"\]]*)\s*\]/g,"='$1']");if(!i.isXML(a))try{if(d||!j.match.PSEUDO.test(c)&&!/!=/.test(c))return b.call(a,c)}catch(e){}return i(c,null,null,[a]).length>0})}(),function(){var a=c.createElement("div");a.innerHTML="
";if(a.getElementsByClassName&&a.getElementsByClassName("e").length!==0){a.lastChild.className="e";if(a.getElementsByClassName("e").length===1)return;j.order.splice(1,0,"CLASS"),j.find.CLASS=function(a,b,c){if(typeof b.getElementsByClassName!=="undefined"&&!c)return b.getElementsByClassName(a[1])},a=null}}(),c.documentElement.contains?i.contains=function(a,b){return a!==b&&(a.contains?a.contains(b):!0)}:c.documentElement.compareDocumentPosition?i.contains=function(a,b){return!!(a.compareDocumentPosition(b)&16)}:i.contains=function(){return!1},i.isXML=function(a){var b=(a?a.ownerDocument||a:0).documentElement;return b?b.nodeName!=="HTML":!1};var t=function(a,b){var c,d=[],e="",f=b.nodeType?[b]:b;while(c=j.match.PSEUDO.exec(a))e+=c[0],a=a.replace(j.match.PSEUDO,"");a=j.relative[a]?a+"*":a;for(var g=0,h=f.length;g0)for(var g=c;g0},closest:function(a,b){var c=[],e,f,g=this[0];if(d.isArray(a)){var h,i,j={},k=1;if(g&&a.length){for(e=0,f=a.length;e-1:d(g).is(h))&&c.push({selector:i,elem:g,level:k});g=g.parentNode,k++}}return c}var l=L.test(a)?d(a,b||this.context):null;for(e=0,f=this.length;e-1:d.find.matchesSelector(g,a)){c.push(g);break}g=g.parentNode;if(!g||!g.ownerDocument||g===b)break}}c=c.length>1?d.unique(c):c;return this.pushStack(c,"closest",a)},index:function(a){if(!a||typeof a==="string")return d.inArray(this[0],a?d(a):this.parent().children());return d.inArray(a.jquery?a[0]:a,this)},add:function(a,b){var c=typeof a==="string"?d(a,b):d.makeArray(a),e=d.merge(this.get(),c);return this.pushStack(N(c[0])||N(e[0])?e:d.unique(e))},andSelf:function(){return this.add(this.prevObject)}}),d.each({parent:function(a){var b=a.parentNode;return b&&b.nodeType!==11?b:null},parents:function(a){return d.dir(a,"parentNode")},parentsUntil:function(a,b,c){return d.dir(a,"parentNode",c)},next:function(a){return d.nth(a,2,"nextSibling")},prev:function(a){return d.nth(a,2,"previousSibling")},nextAll:function(a){return d.dir(a,"nextSibling")},prevAll:function(a){return d.dir(a,"previousSibling")},nextUntil:function(a,b,c){return d.dir(a,"nextSibling",c)},prevUntil:function(a,b,c){return d.dir(a,"previousSibling",c)},siblings:function(a){return d.sibling(a.parentNode.firstChild,a)},children:function(a){return d.sibling(a.firstChild)},contents:function(a){return d.nodeName(a,"iframe")?a.contentDocument||a.contentWindow.document:d.makeArray(a.childNodes)}},function(a,b){d.fn[a]=function(c,e){var f=d.map(this,b,c),g=K.call(arguments);G.test(a)||(e=c),e&&typeof e==="string"&&(f=d.filter(e,f)),f=this.length>1&&!M[a]?d.unique(f):f,(this.length>1||I.test(e))&&H.test(a)&&(f=f.reverse());return this.pushStack(f,a,g.join(","))}}),d.extend({filter:function(a,b,c){c&&(a=":not("+a+")");return b.length===1?d.find.matchesSelector(b[0],a)?[b[0]]:[]:d.find.matches(a,b)},dir:function(a,c,e){var f=[],g=a[c];while(g&&g.nodeType!==9&&(e===b||g.nodeType!==1||!d(g).is(e)))g.nodeType===1&&f.push(g),g=g[c];return f},nth:function(a,b,c,d){b=b||1;var e=0;for(;a;a=a[c])if(a.nodeType===1&&++e===b)break;return a},sibling:function(a,b){var c=[];for(;a;a=a.nextSibling)a.nodeType===1&&a!==b&&c.push(a);return c}});var P=/ jQuery\d+="(?:\d+|null)"/g,Q=/^\s+/,R=/<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig,S=/<([\w:]+)/,T=/",""],legend:[1,""," "],thead:[1,""],tr:[2,""],td:[3,""],col:[2,""],area:[1,""," "],_default:[0,"",""]};X.optgroup=X.option,X.tbody=X.tfoot=X.colgroup=X.caption=X.thead,X.th=X.td,d.support.htmlSerialize||(X._default=[1,"div","
"]),d.fn.extend({text:function(a){if(d.isFunction(a))return this.each(function(b){var c=d(this);c.text(a.call(this,b,c.text()))});if(typeof a!=="object"&&a!==b)return this.empty().append((this[0]&&this[0].ownerDocument||c).createTextNode(a));return d.text(this)},wrapAll:function(a){if(d.isFunction(a))return this.each(function(b){d(this).wrapAll(a.call(this,b))});if(this[0]){var b=d(a,this[0].ownerDocument).eq(0).clone(!0);this[0].parentNode&&b.insertBefore(this[0]),b.map(function(){var a=this;while(a.firstChild&&a.firstChild.nodeType===1)a=a.firstChild;return a}).append(this)}return this},wrapInner:function(a){if(d.isFunction(a))return this.each(function(b){d(this).wrapInner(a.call(this,b))});return this.each(function(){var b=d(this),c=b.contents();c.length?c.wrapAll(a):b.append(a)})},wrap:function(a){return this.each(function(){d(this).wrapAll(a)})},unwrap:function(){return this.parent().each(function(){d.nodeName(this,"body")||d(this).replaceWith(this.childNodes)}).end()},append:function(){return this.domManip(arguments,!0,function(a){this.nodeType===1&&this.appendChild(a)})},prepend:function(){return this.domManip(arguments,!0,function(a){this.nodeType===1&&this.insertBefore(a,this.firstChild)})},before:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this)});if(arguments.length){var a=d(arguments[0]);a.push.apply(a,this.toArray());return this.pushStack(a,"before",arguments)}},after:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this.nextSibling)});if(arguments.length){var a=this.pushStack(this,"after",arguments);a.push.apply(a,d(arguments[0]).toArray());return a}},remove:function(a,b){for(var c=0,e;(e=this[c])!=null;c++)if(!a||d.filter(a,[e]).length)!b&&e.nodeType===1&&(d.cleanData(e.getElementsByTagName("*")),d.cleanData([e])),e.parentNode&&e.parentNode.removeChild(e);return this},empty:function(){for(var a=0,b;(b=this[a])!=null;a++){b.nodeType===1&&d.cleanData(b.getElementsByTagName("*"));while(b.firstChild)b.removeChild(b.firstChild)}return this},clone:function(a,b){a=a==null?!0:a,b=b==null?a:b;return this.map(function(){return d.clone(this,a,b)})},html:function(a){if(a===b)return this[0]&&this[0].nodeType===1?this[0].innerHTML.replace(P,""):null;if(typeof a!=="string"||V.test(a)||!d.support.leadingWhitespace&&Q.test(a)||X[(S.exec(a)||["",""])[1].toLowerCase()])d.isFunction(a)?this.each(function(b){var c=d(this);c.html(a.call(this,b,c.html()))}):this.empty().append(a);else{a=a.replace(R,"<$1>$2>");try{for(var c=0,e=this.length;c1&&l0?this.clone(!0):this).get();d(f[h])[b](j),e=e.concat(j)}return this.pushStack(e,a,f.selector)}}),d.extend({clone:function(a,b,c){var e=a.cloneNode(!0),f,g,h;if(!d.support.noCloneEvent&&(a.nodeType===1||a.nodeType===11)&&!d.isXMLDoc(a)){f=a.getElementsByTagName("*"),g=e.getElementsByTagName("*");for(h=0;f[h];++h)$(f[h],g[h]);$(a,e)}if(b){Z(a,e);if(c&&"getElementsByTagName"in a){f=a.getElementsByTagName("*"),g=e.getElementsByTagName("*");if(f.length)for(h=0;f[h];++h)Z(f[h],g[h])}}return e},clean:function(a,b,e,f){b=b||c,typeof b.createElement==="undefined"&&(b=b.ownerDocument||b[0]&&b[0].ownerDocument||c);var g=[];for(var h=0,i;(i=a[h])!=null;h++){typeof i==="number"&&(i+="");if(!i)continue;if(typeof i!=="string"||U.test(i)){if(typeof i==="string"){i=i.replace(R,"<$1>$2>");var j=(S.exec(i)||["",""])[1].toLowerCase(),k=X[j]||X._default,l=k[0],m=b.createElement("div");m.innerHTML=k[1]+i+k[2];while(l--)m=m.lastChild;if(!d.support.tbody){var n=T.test(i),o=j==="table"&&!n?m.firstChild&&m.firstChild.childNodes:k[1]===""&&!n?m.childNodes:[];for(var p=o.length-1;p>=0;--p)d.nodeName(o[p],"tbody")&&!o[p].childNodes.length&&o[p].parentNode.removeChild(o[p])}!d.support.leadingWhitespace&&Q.test(i)&&m.insertBefore(b.createTextNode(Q.exec(i)[0]),m.firstChild),i=m.childNodes}}else i=b.createTextNode(i);i.nodeType?g.push(i):g=d.merge(g,i)}if(e)for(h=0;g[h];h++)!f||!d.nodeName(g[h],"script")||g[h].type&&g[h].type.toLowerCase()!=="text/javascript"?(g[h].nodeType===1&&g.splice.apply(g,[h+1,0].concat(d.makeArray(g[h].getElementsByTagName("script")))),e.appendChild(g[h])):f.push(g[h].parentNode?g[h].parentNode.removeChild(g[h]):g[h]);return g},cleanData:function(a){var b,c,e=d.cache,f=d.expando,g=d.event.special,h=d.support.deleteExpando;for(var i=0,j;(j=a[i])!=null;i++){if(j.nodeName&&d.noData[j.nodeName.toLowerCase()])continue;c=j[d.expando];if(c){b=e[c]&&e[c][f];if(b&&b.events){for(var k in b.events)g[k]?d.event.remove(j,k):d.removeEvent(j,k,b.handle);b.handle&&(b.handle.elem=null)}h?delete j[d.expando]:j.removeAttribute&&j.removeAttribute(d.expando),delete e[c]}}}});var ba=/alpha\([^)]*\)/i,bb=/opacity=([^)]*)/,bc=/-([a-z])/ig,bd=/([A-Z])/g,be=/^-?\d+(?:px)?$/i,bf=/^-?\d/,bg={position:"absolute",visibility:"hidden",display:"block"},bh=["Left","Right"],bi=["Top","Bottom"],bj,bk,bl,bm=function(a,b){return b.toUpperCase()};d.fn.css=function(a,c){if(arguments.length===2&&c===b)return this;return d.access(this,a,c,!0,function(a,c,e){return e!==b?d.style(a,c,e):d.css(a,c)})},d.extend({cssHooks:{opacity:{get:function(a,b){if(b){var c=bj(a,"opacity","opacity");return c===""?"1":c}return a.style.opacity}}},cssNumber:{zIndex:!0,fontWeight:!0,opacity:!0,zoom:!0,lineHeight:!0},cssProps:{"float":d.support.cssFloat?"cssFloat":"styleFloat"},style:function(a,c,e,f){if(a&&a.nodeType!==3&&a.nodeType!==8&&a.style){var g,h=d.camelCase(c),i=a.style,j=d.cssHooks[h];c=d.cssProps[h]||h;if(e===b){if(j&&"get"in j&&(g=j.get(a,!1,f))!==b)return g;return i[c]}if(typeof e==="number"&&isNaN(e)||e==null)return;typeof e==="number"&&!d.cssNumber[h]&&(e+="px");if(!j||!("set"in j)||(e=j.set(a,e))!==b)try{i[c]=e}catch(k){}}},css:function(a,c,e){var f,g=d.camelCase(c),h=d.cssHooks[g];c=d.cssProps[g]||g;if(h&&"get"in h&&(f=h.get(a,!0,e))!==b)return f;if(bj)return bj(a,c,g)},swap:function(a,b,c){var d={};for(var e in b)d[e]=a.style[e],a.style[e]=b[e];c.call(a);for(e in b)a.style[e]=d[e]},camelCase:function(a){return a.replace(bc,bm)}}),d.curCSS=d.css,d.each(["height","width"],function(a,b){d.cssHooks[b]={get:function(a,c,e){var f;if(c){a.offsetWidth!==0?f=bn(a,b,e):d.swap(a,bg,function(){f=bn(a,b,e)});if(f<=0){f=bj(a,b,b),f==="0px"&&bl&&(f=bl(a,b,b));if(f!=null)return f===""||f==="auto"?"0px":f}if(f<0||f==null){f=a.style[b];return f===""||f==="auto"?"0px":f}return typeof f==="string"?f:f+"px"}},set:function(a,b){if(!be.test(b))return b;b=parseFloat(b);if(b>=0)return b+"px"}}}),d.support.opacity||(d.cssHooks.opacity={get:function(a,b){return bb.test((b&&a.currentStyle?a.currentStyle.filter:a.style.filter)||"")?parseFloat(RegExp.$1)/100+"":b?"1":""},set:function(a,b){var c=a.style;c.zoom=1;var e=d.isNaN(b)?"":"alpha(opacity="+b*100+")",f=c.filter||"";c.filter=ba.test(f)?f.replace(ba,e):c.filter+" "+e}}),c.defaultView&&c.defaultView.getComputedStyle&&(bk=function(a,c,e){var f,g,h;e=e.replace(bd,"-$1").toLowerCase();if(!(g=a.ownerDocument.defaultView))return b;if(h=g.getComputedStyle(a,null))f=h.getPropertyValue(e),f===""&&!d.contains(a.ownerDocument.documentElement,a)&&(f=d.style(a,e));return f}),c.documentElement.currentStyle&&(bl=function(a,b){var c,d=a.currentStyle&&a.currentStyle[b],e=a.runtimeStyle&&a.runtimeStyle[b],f=a.style;!be.test(d)&&bf.test(d)&&(c=f.left,e&&(a.runtimeStyle.left=a.currentStyle.left),f.left=b==="fontSize"?"1em":d||0,d=f.pixelLeft+"px",f.left=c,e&&(a.runtimeStyle.left=e));return d===""?"auto":d}),bj=bk||bl,d.expr&&d.expr.filters&&(d.expr.filters.hidden=function(a){var b=a.offsetWidth,c=a.offsetHeight;return b===0&&c===0||!d.support.reliableHiddenOffsets&&(a.style.display||d.css(a,"display"))==="none"},d.expr.filters.visible=function(a){return!d.expr.filters.hidden(a)});var bo=/%20/g,bp=/\[\]$/,bq=/\r?\n/g,br=/#.*$/,bs=/^(.*?):\s*(.*?)\r?$/mg,bt=/^(?:color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i,bu=/^(?:GET|HEAD)$/,bv=/^\/\//,bw=/\?/,bx=/