diff options
author | Derrell Lipman <derrell@samba.org> | 2007-01-03 20:17:37 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 14:36:09 -0500 |
commit | 626bb8efb0c825f332c937ffaaadc9b402079539 (patch) | |
tree | 1c95f69d157b24f64edff470143f5f55a09cfca6 /webapps/scripting/client/encoder.js | |
parent | eeddcf8cc8eb655d7c40f1fd5f7fd422529f4f98 (diff) | |
download | samba-626bb8efb0c825f332c937ffaaadc9b402079539.tar.gz samba-626bb8efb0c825f332c937ffaaadc9b402079539.tar.bz2 samba-626bb8efb0c825f332c937ffaaadc9b402079539.zip |
r20517: re-add cleaned-up webapps
(This used to be commit 5a3d6ad0b7cf0ecf8b57b4088b19f7d4291c990b)
Diffstat (limited to 'webapps/scripting/client/encoder.js')
-rw-r--r-- | webapps/scripting/client/encoder.js | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/webapps/scripting/client/encoder.js b/webapps/scripting/client/encoder.js new file mode 100644 index 0000000000..4aa4cc0954 --- /dev/null +++ b/webapps/scripting/client/encoder.js @@ -0,0 +1,84 @@ +/* + client side js functions for encoding/decoding objects into linear strings + + Copyright Andrew Tridgell 2005 + released under the GNU GPL Version 2 or later +*/ +/* + usage: + + enc = encodeObject(obj); + obj = decodeObject(enc); + + The encoded format of the object is a string that is safe to + use in URLs + + Note that only data elements are encoded, not functions +*/ + +function count_members(o) { + var i, count = 0; + for (i in o) { + count++; + } + return count; +} + +function encodeObject(o) { + var i, r = count_members(o) + ":"; + for (i in o) { + var t = typeof(o[i]); + if (t == 'object') { + r = r + "" + i + ":" + t + ":" + encodeObject(o[i]); + } else if (t == 'string') { + var s = encodeURIComponent(o[i]).replace(/%/g,'#'); + r = r + "" + i + ":" + t + ":" + s + ":"; + } else if (t == 'boolean' || t == 'number') { + r = r + "" + i + ":" + t + ":" + o[i] + ":"; + } else if (t == 'undefined' || t == 'null') { + r = r + "" + i + ":" + t + ":"; + } else if (t != 'function') { + alert("Unable to encode type " + t); + } + } + return r; +} + +function decodeObjectArray(a) { + var o = new Object(); + var i, count = a[a.i]; a.i++; + for (i=0;i<count;i++) { + var name = a[a.i]; a.i++; + var type = a[a.i]; a.i++; + var value; + if (type == 'object') { + o[name] = decodeObjectArray(a); + } else if (type == "string") { + value = decodeURIComponent(a[a.i].replace(/#/g,'%')); a.i++; + o[name] = value; + } else if (type == "boolean") { + value = a[a.i]; a.i++; + if (value == 'true') { + o[name] = true; + } else { + o[name] = false; + } + } else if (type == "undefined") { + o[name] = undefined; + } else if (type == "null") { + o[name] = null; + } else if (type == "number") { + value = a[a.i]; a.i++; + o[name] = value * 1; + } else { + alert("Unable to delinearise type " + type); + } + } + return o; +} + +function decodeObject(str) { + var a = str.split(':'); + a.i = 0; + return decodeObjectArray(a); +} |