corrade-nucleus-nucleons – Blame information for rev 20

Subversion Repositories:
Rev:
Rev Author Line No. Line
20 office 1 /*global unescape */
2 /*jshint curly: false, scripturl: true */
3 //
4 // trivial bookmarklet/escaped script detector for the javascript beautifier
5 // written by Einar Lielmanis <einar@jsbeautifier.org>
6 //
7 // usage:
8 //
9 // if (Urlencoded.detect(some_string)) {
10 // var unpacked = Urlencoded.unpack(some_string);
11 // }
12 //
13 //
14  
15 var isNode = (typeof module !== 'undefined' && module.exports);
16 if (isNode) {
17 var SanityTest = require(__dirname + '/../../test/sanitytest');
18 }
19  
20 var Urlencoded = {
21 detect: function(str) {
22 // the fact that script doesn't contain any space, but has %20 instead
23 // should be sufficient check for now.
24 if (str.indexOf(' ') === -1) {
25 if (str.indexOf('%2') !== -1) return true;
26 if (str.replace(/[^%]+/g, '').length > 3) return true;
27 }
28 return false;
29 },
30  
31 unpack: function(str) {
32 if (Urlencoded.detect(str)) {
33 if (str.indexOf('%2B') !== -1 || str.indexOf('%2b') !== -1) {
34 // "+" escaped as "%2B"
35 return unescape(str.replace(/\+/g, '%20'));
36 } else {
37 return unescape(str);
38 }
39 }
40 return str;
41 },
42  
43  
44  
45 run_tests: function(sanity_test) {
46 var t = sanity_test || new SanityTest();
47 t.test_function(Urlencoded.detect, "Urlencoded.detect");
48 t.expect('', false);
49 t.expect('var a = b', false);
50 t.expect('var%20a+=+b', true);
51 t.expect('var%20a=b', true);
52 t.expect('var%20%21%22', true);
53 t.expect('javascript:(function(){var%20whatever={init:function(){alert(%22a%22+%22b%22)}};whatever.init()})();', true);
54 t.test_function(Urlencoded.unpack, 'Urlencoded.unpack');
55  
56 t.expect('javascript:(function(){var%20whatever={init:function(){alert(%22a%22+%22b%22)}};whatever.init()})();',
57 'javascript:(function(){var whatever={init:function(){alert("a"+"b")}};whatever.init()})();'
58 );
59 t.expect('', '');
60 t.expect('abcd', 'abcd');
61 t.expect('var a = b', 'var a = b');
62 t.expect('var%20a=b', 'var a=b');
63 t.expect('var%20a=b+1', 'var a=b+1');
64 t.expect('var%20a=b%2b1', 'var a=b+1');
65 return t;
66 }
67  
68  
69 };
70  
71 if (isNode) {
72 module.exports = Urlencoded;
73 }