diff options
Diffstat (limited to 'devtools/shared/jsbeautify/lib')
-rw-r--r-- | devtools/shared/jsbeautify/lib/moz.build | 10 | ||||
-rw-r--r-- | devtools/shared/jsbeautify/lib/sanitytest.js | 137 | ||||
-rw-r--r-- | devtools/shared/jsbeautify/lib/urlencode_unpacker.js | 73 |
3 files changed, 220 insertions, 0 deletions
diff --git a/devtools/shared/jsbeautify/lib/moz.build b/devtools/shared/jsbeautify/lib/moz.build new file mode 100644 index 000000000..8fc89a102 --- /dev/null +++ b/devtools/shared/jsbeautify/lib/moz.build @@ -0,0 +1,10 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +DevToolsModules( + 'sanitytest.js', + 'urlencode_unpacker.js', +) diff --git a/devtools/shared/jsbeautify/lib/sanitytest.js b/devtools/shared/jsbeautify/lib/sanitytest.js new file mode 100644 index 000000000..f072a74b1 --- /dev/null +++ b/devtools/shared/jsbeautify/lib/sanitytest.js @@ -0,0 +1,137 @@ +// +// simple testing interface +// written by Einar Lielmanis, einar@jsbeautifier.org +// +// usage: +// +// var t = new SanityTest(function (x) { return x; }, 'my function'); +// t.expect('input', 'output'); +// t.expect('a', 'a'); +// output_somewhere(t.results()); // good for <pre>, html safe-ish +// alert(t.results_raw()); // html unescaped + + +function SanityTest (func, name_of_test) { + + var test_func = func || function (x) { + return x; + }; + + var test_name = name_of_test || ''; + + var n_failed = 0; + var n_succeeded = 0; + + this.failures = []; + this.successes = []; + + this.test_function = function(func, name) { + test_func = func; + test_name = name || ''; + }; + + this.get_exitcode = function() { + return n_succeeded === 0 || n_failed !== 0 ? 1 : 0; + }; + + this.expect = function(parameters, expected_value) { + // multi-parameter calls not supported (I don't need them now). + var result = test_func(parameters); + // proper array checking is a pain. i'll maybe do it later, compare strings representations instead + if ((result === expected_value) || (expected_value instanceof Array && result.join(', ') == expected_value.join(', '))) { + n_succeeded += 1; + this.successes.push([test_name, parameters, expected_value, result]); + } else { + n_failed += 1; + this.failures.push([test_name, parameters, expected_value, result]); + } + }; + + + this.results_raw = function() { + var results = ''; + if (n_failed === 0) { + if (n_succeeded === 0) { + results = 'No tests run.'; + } else { + results = 'All ' + n_succeeded + ' tests passed.'; + } + } else { + for (var i = 0 ; i < this.failures.length; i++) { + var f = this.failures[i]; + if (f[0]) { + f[0] = f[0] + ' '; + } + results += '---- ' + f[0] + 'input -------\n' + this.prettyprint(f[1]) + '\n'; + results += '---- ' + f[0] + 'expected ----\n' + this.prettyprint(f[2]) + '\n'; + results += '---- ' + f[0] + 'output ------\n' + this.prettyprint(f[3]) + '\n\n'; + + } + results += n_failed + ' tests failed.\n'; + } + return results; + }; + + + this.results = function() { + return this.lazy_escape(this.results_raw()); + }; + + + this.prettyprint = function(something, quote_strings) { + var type = typeof something; + switch(type.toLowerCase()) { + case 'string': + if (quote_strings) { + return "'" + something.replace("'", "\\'") + "'"; + } else { + return something; + } + case 'number': + return '' + something; + case 'boolean': + return something ? 'true' : 'false'; + case 'undefined': + return 'undefined'; + case 'object': + if (something instanceof Array) { + var x = []; + var expected_index = 0; + for (var k in something) { + if (k == expected_index) { + x.push(this.prettyprint(something[k], true)); + expected_index += 1; + } else { + x.push('\n' + k + ': ' + this.prettyprint(something[k], true)); + } + } + return '[' + x.join(', ') + ']'; + } else { + return 'object: ' + something; + } + default: + return type + ': ' + something; + } + }; + + + this.lazy_escape = function (str) { + return str.replace(/</g, '<').replace(/\>/g, '>').replace(/\n/g, '<br />'); + }; + + + this.log = function () { + if (window.console) { + if (console.firebug) { + console.log.apply(console, Array.prototype.slice.call(arguments)); + } else { + console.log.call(console, Array.prototype.slice.call(arguments)); + } + } + }; + +} + +if (typeof module !== 'undefined' && module.exports) { + module.exports = SanityTest; +} diff --git a/devtools/shared/jsbeautify/lib/urlencode_unpacker.js b/devtools/shared/jsbeautify/lib/urlencode_unpacker.js new file mode 100644 index 000000000..b45cd15c0 --- /dev/null +++ b/devtools/shared/jsbeautify/lib/urlencode_unpacker.js @@ -0,0 +1,73 @@ +/*global unescape */ +/*jshint curly: false, scripturl: true */ +// +// trivial bookmarklet/escaped script detector for the javascript beautifier +// written by Einar Lielmanis <einar@jsbeautifier.org> +// +// usage: +// +// if (Urlencoded.detect(some_string)) { +// var unpacked = Urlencoded.unpack(some_string); +// } +// +// + +var isNode = (typeof module !== 'undefined' && module.exports); +if (isNode) { + var SanityTest = require("devtools/shared/jsbeautify/lib/sanitytest"); +} + +var Urlencoded = { + detect: function (str) { + // the fact that script doesn't contain any space, but has %20 instead + // should be sufficient check for now. + if (str.indexOf(' ') == -1) { + if (str.indexOf('%2') != -1) return true; + if (str.replace(/[^%]+/g, '').length > 3) return true; + } + return false; + }, + + unpack: function (str) { + if (Urlencoded.detect(str)) { + if (str.indexOf('%2B') != -1 || str.indexOf('%2b') != -1) { + // "+" escaped as "%2B" + return unescape(str.replace(/\+/g, '%20')); + } else { + return unescape(str); + } + } + return str; + }, + + + + run_tests: function (sanity_test) { + var t = sanity_test || new SanityTest(); + t.test_function(Urlencoded.detect, "Urlencoded.detect"); + t.expect('', false); + t.expect('var a = b', false); + t.expect('var%20a+=+b', true); + t.expect('var%20a=b', true); + t.expect('var%20%21%22', true); + t.expect('javascript:(function(){var%20whatever={init:function(){alert(%22a%22+%22b%22)}};whatever.init()})();', true); + t.test_function(Urlencoded.unpack, 'Urlencoded.unpack'); + + t.expect('javascript:(function(){var%20whatever={init:function(){alert(%22a%22+%22b%22)}};whatever.init()})();', + 'javascript:(function(){var whatever={init:function(){alert("a"+"b")}};whatever.init()})();' + ); + t.expect('', ''); + t.expect('abcd', 'abcd'); + t.expect('var a = b', 'var a = b'); + t.expect('var%20a=b', 'var a=b'); + t.expect('var%20a=b+1', 'var a=b+1'); + t.expect('var%20a=b%2b1', 'var a=b+1'); + return t; + } + + +}; + +if (isNode) { + module.exports = Urlencoded; +} |