smparkes-envjs 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. data/bin/envjsrb +157 -73
  2. data/bin/jquery-1.3.2-test.js +11 -4
  3. data/lib/envjs/env.js +935 -11028
  4. data/lib/envjs/net/cgi.rb +94 -0
  5. data/lib/envjs/net/file.rb +4 -4
  6. data/lib/envjs/runtime.rb +80 -24
  7. data/test/call-load-test.js +0 -2
  8. data/test/firebug/errorIcon.png +0 -0
  9. data/test/firebug/firebug.css +209 -0
  10. data/test/firebug/firebug.html +23 -0
  11. data/test/firebug/firebug.js +672 -0
  12. data/test/firebug/firebugx.js +10 -0
  13. data/test/firebug/infoIcon.png +0 -0
  14. data/test/firebug/warningIcon.png +0 -0
  15. data/test/fixtures/html/events.html +171 -0
  16. data/test/fixtures/html/iframe1.html +46 -0
  17. data/test/fixtures/html/iframe1a.html +46 -0
  18. data/test/fixtures/html/iframe2.html +45 -0
  19. data/test/fixtures/html/iframe3.html +28 -0
  20. data/test/fixtures/html/iframeN.html +57 -0
  21. data/test/fixtures/html/malformed.html +181 -0
  22. data/test/fixtures/html/scope.html +81 -0
  23. data/test/fixtures/html/trivial.html +19 -0
  24. data/test/fixtures/html/with_js.html +26 -0
  25. data/test/fixtures/images/icon-blue.png +0 -0
  26. data/test/fixtures/js/external_script.js +1 -0
  27. data/test/fixtures/js/script.js +1 -0
  28. data/test/fixtures/js/script_error.js +2 -0
  29. data/test/html/malformed.html +1 -0
  30. data/test/index.html +21 -19
  31. data/test/{primaryTests.js → primary-tests.js} +1 -3
  32. data/test/{prototype.js → prototype-test.js} +0 -2
  33. data/test/qunit/qunit/qunit.css +0 -0
  34. data/test/qunit/qunit/qunit.js +2 -2
  35. data/test/{qunit/qunit/qunit.js.smp → specs/dist/env.spec.js} +539 -7
  36. data/test/specs/envjs.spec.css +46 -0
  37. data/test/specs/parser/html.js +31 -0
  38. data/test/specs/parser/spec.html +40 -0
  39. data/test/specs/parser/xml.js +31 -0
  40. data/test/specs/qunit.bdd.js +210 -0
  41. data/test/{qunit.smp/qunit → specs}/qunit.css +0 -0
  42. data/test/{qunit.smp/qunit → specs}/qunit.js +2 -2
  43. data/test/specs/template/spec-0.js +31 -0
  44. data/test/specs/template/spec-1.js +31 -0
  45. data/test/specs/template/spec.html +40 -0
  46. data/test/specs/window/css.js +23 -0
  47. data/test/specs/window/dialog.js +25 -0
  48. data/test/specs/window/document.js +23 -0
  49. data/test/specs/window/event.js +25 -0
  50. data/test/specs/window/history.js +34 -0
  51. data/test/specs/window/location.js +34 -0
  52. data/test/specs/window/navigator.js +71 -0
  53. data/test/specs/window/screen.js +42 -0
  54. data/test/specs/window/spec.html +48 -0
  55. data/test/specs/window/timer.js +26 -0
  56. data/test/specs/window/window.js +53 -0
  57. data/test/specs/xhr/spec.html +47 -0
  58. data/test/specs/xhr/xhr.js +31 -0
  59. data/test/test.js +10 -0
  60. data/test/unit/elementmembers.js +31 -0
  61. data/test/unit/nu.validator.js +34 -0
  62. data/test/unit/onload.js +1 -3
  63. data/test/unit/parser.js +106 -98
  64. data/test/unit/prototypecompat.js +2 -2
  65. data/test/vendor/jQuery/1.3.2/ChangeLog.txt +98 -0
  66. data/test/vendor/jQuery/1.3.2/GPL-LICENSE.txt +278 -0
  67. data/test/vendor/jQuery/1.3.2/MIT-LICENSE.txt +20 -0
  68. data/test/vendor/jQuery/1.3.2/Makefile +154 -0
  69. data/test/vendor/jQuery/1.3.2/README +67 -0
  70. data/test/vendor/jQuery/1.3.2/build/ant-contrib-0.6.jar +0 -0
  71. data/test/vendor/jQuery/1.3.2/build/build/min.js +11 -0
  72. data/test/vendor/jQuery/1.3.2/build/build/pack.js +19 -0
  73. data/test/vendor/jQuery/1.3.2/build/js/Packer.js +209 -0
  74. data/test/vendor/jQuery/1.3.2/build/js/ParseMaster.js +106 -0
  75. data/test/vendor/jQuery/1.3.2/build/js/Words.js +62 -0
  76. data/test/vendor/jQuery/1.3.2/build/js/base2.js +978 -0
  77. data/test/vendor/jQuery/1.3.2/build/js/jsmin.js +316 -0
  78. data/test/vendor/jQuery/1.3.2/build/js/json.js +117 -0
  79. data/test/vendor/jQuery/1.3.2/build/js/pack.js +316 -0
  80. data/test/vendor/jQuery/1.3.2/build/js/parse.js +105 -0
  81. data/test/vendor/jQuery/1.3.2/build/js/writeFile.js +19 -0
  82. data/test/vendor/jQuery/1.3.2/build/js/xml.js +25 -0
  83. data/test/vendor/jQuery/1.3.2/build/js.jar +0 -0
  84. data/test/vendor/jQuery/1.3.2/build/runtest/env.js +1 -0
  85. data/test/vendor/jQuery/1.3.2/build/runtest/test.js +106 -0
  86. data/test/vendor/jQuery/1.3.2/build/runtest/testrunner.js +197 -0
  87. data/test/vendor/jQuery/1.3.2/build/speed/benchmarker.css +65 -0
  88. data/test/vendor/jQuery/1.3.2/build/speed/benchmarker.js +181 -0
  89. data/test/vendor/jQuery/1.3.2/build/speed/index.html +72 -0
  90. data/test/vendor/jQuery/1.3.2/build/speed/jquery-basis.js +3549 -0
  91. data/test/vendor/jQuery/1.3.2/build/style.xsl +99 -0
  92. data/test/vendor/jQuery/1.3.2/build/yuicompressor-2.4.2.jar +0 -0
  93. data/test/vendor/jQuery/1.3.2/build.xml +123 -0
  94. data/test/vendor/jQuery/1.3.2/dist/jquery.js +4376 -0
  95. data/test/vendor/jQuery/1.3.2/reminder.txt +3 -0
  96. data/test/vendor/jQuery/1.3.2/src/ajax.js +541 -0
  97. data/test/vendor/jQuery/1.3.2/src/core.js +1255 -0
  98. data/test/vendor/jQuery/1.3.2/src/data.js +145 -0
  99. data/test/vendor/jQuery/1.3.2/src/dimensions.js +49 -0
  100. data/test/vendor/jQuery/1.3.2/src/event.js +684 -0
  101. data/test/vendor/jQuery/1.3.2/src/fx.js +407 -0
  102. data/test/vendor/jQuery/1.3.2/src/intro.js +13 -0
  103. data/test/vendor/jQuery/1.3.2/src/offset.js +155 -0
  104. data/test/vendor/jQuery/1.3.2/src/outro.js +1 -0
  105. data/test/vendor/jQuery/1.3.2/src/selector.js +1015 -0
  106. data/test/vendor/jQuery/1.3.2/src/support.js +112 -0
  107. data/test/vendor/jQuery/1.3.2/test/data/cow.jpg +0 -0
  108. data/test/vendor/jQuery/1.3.2/test/data/dashboard.xml +11 -0
  109. data/test/vendor/jQuery/1.3.2/test/data/echoQuery.php +1 -0
  110. data/test/vendor/jQuery/1.3.2/test/data/iframe.html +8 -0
  111. data/test/vendor/jQuery/1.3.2/test/data/json.php +9 -0
  112. data/test/vendor/jQuery/1.3.2/test/data/json_assigned_obj.js +1 -0
  113. data/test/vendor/jQuery/1.3.2/test/data/json_obj.js +1 -0
  114. data/test/vendor/jQuery/1.3.2/test/data/jsonp.php +10 -0
  115. data/test/vendor/jQuery/1.3.2/test/data/name.html +1 -0
  116. data/test/vendor/jQuery/1.3.2/test/data/name.php +29 -0
  117. data/test/vendor/jQuery/1.3.2/test/data/offset/absolute.html +39 -0
  118. data/test/vendor/jQuery/1.3.2/test/data/offset/body.html +24 -0
  119. data/test/vendor/jQuery/1.3.2/test/data/offset/fixed.html +33 -0
  120. data/test/vendor/jQuery/1.3.2/test/data/offset/relative.html +31 -0
  121. data/test/vendor/jQuery/1.3.2/test/data/offset/scroll.html +39 -0
  122. data/test/vendor/jQuery/1.3.2/test/data/offset/static.html +31 -0
  123. data/test/vendor/jQuery/1.3.2/test/data/offset/table.html +43 -0
  124. data/test/vendor/jQuery/1.3.2/test/data/params_html.php +12 -0
  125. data/test/vendor/jQuery/1.3.2/test/data/test.html +7 -0
  126. data/test/vendor/jQuery/1.3.2/test/data/test.js +3 -0
  127. data/test/vendor/jQuery/1.3.2/test/data/test.php +7 -0
  128. data/test/vendor/jQuery/1.3.2/test/data/test2.html +5 -0
  129. data/test/vendor/jQuery/1.3.2/test/data/test3.html +3 -0
  130. data/test/vendor/jQuery/1.3.2/test/data/testrunner.js +1 -0
  131. data/test/vendor/jQuery/1.3.2/test/data/text.php +12 -0
  132. data/test/vendor/jQuery/1.3.2/test/data/with_fries.xml +25 -0
  133. data/test/vendor/jQuery/1.3.2/test/index.html +229 -0
  134. data/test/vendor/jQuery/1.3.2/test/jqenv-1259090275493.html +247 -0
  135. data/test/vendor/jQuery/1.3.2/test/jqenv-1259091671501.html +247 -0
  136. data/test/vendor/jQuery/1.3.2/test/jqenv-1259175319655.html +247 -0
  137. data/test/vendor/jQuery/1.3.2/test/offset.html +28 -0
  138. data/test/vendor/jQuery/1.3.2/test/otherlibs/jquery/1.2.1/jquery.js +11 -0
  139. data/test/vendor/jQuery/1.3.2/test/otherlibs/jquery/1.2.3/jquery.js +11 -0
  140. data/test/vendor/jQuery/1.3.2/test/otherlibs/mootools/1.11/mootools.js +1577 -0
  141. data/test/vendor/jQuery/1.3.2/test/otherlibs/prototype/1.6.0.2/prototype.js +4221 -0
  142. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.7.0/builder.js +131 -0
  143. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.7.0/controls.js +835 -0
  144. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.7.0/dragdrop.js +944 -0
  145. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.7.0/effects.js +1090 -0
  146. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.7.0/scriptaculous.js +51 -0
  147. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.7.0/slider.js +278 -0
  148. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.7.0/unittest.js +564 -0
  149. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.8.1/builder.js +136 -0
  150. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.8.1/controls.js +965 -0
  151. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.8.1/dragdrop.js +974 -0
  152. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.8.1/effects.js +1122 -0
  153. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.8.1/scriptaculous.js +58 -0
  154. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.8.1/slider.js +275 -0
  155. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.8.1/sound.js +55 -0
  156. data/test/vendor/jQuery/1.3.2/test/otherlibs/scriptaculous/1.8.1/unittest.js +568 -0
  157. data/test/vendor/jQuery/1.3.2/test/polluted.php +64 -0
  158. data/test/vendor/jQuery/1.3.2/test/qunit/testrunner.js +780 -0
  159. data/test/vendor/jQuery/1.3.2/test/qunit/testsuite.css +120 -0
  160. data/test/vendor/jQuery/1.3.2/test/test.js +41 -0
  161. data/test/vendor/jQuery/1.3.2/test/unit/ajax.js +856 -0
  162. data/test/vendor/jQuery/1.3.2/test/unit/core.js +1872 -0
  163. data/test/vendor/jQuery/1.3.2/test/unit/data.js +160 -0
  164. data/test/vendor/jQuery/1.3.2/test/unit/dimensions.js +86 -0
  165. data/test/vendor/jQuery/1.3.2/test/unit/event.js +670 -0
  166. data/test/vendor/jQuery/1.3.2/test/unit/fx.js +451 -0
  167. data/test/vendor/jQuery/1.3.2/test/unit/offset.js +173 -0
  168. data/test/vendor/jQuery/1.3.2/test/unit/selector.js +395 -0
  169. data/test/vendor/jQuery/1.3.2/test/xhtml.php +5 -0
  170. data/test/vendor/jQuery/1.3.2/version.txt +1 -0
  171. metadata +158 -22
  172. data/lib/envjs/runtime.rb.smp +0 -133
  173. data/test/envjs-call-load-test.js +0 -5
  174. data/test/envjs-prototype.js +0 -5
  175. data/test/qunit/package.json +0 -21
  176. data/test/qunit/test/index.html +0 -17
  177. data/test/qunit/test/same.js +0 -1368
  178. data/test/qunit/test/test.js +0 -136
  179. data/test/qunit.smp/package.json +0 -21
  180. data/test/qunit.smp/test/index.html +0 -17
  181. data/test/qunit.smp/test/same.js +0 -1368
  182. data/test/qunit.smp/test/test.js +0 -136
  183. data/test/rhino-call-load-test.js +0 -5
  184. data/test/rhino-prototype.js +0 -5
  185. data/test/test-with-envs-jar.js +0 -8
  186. data/test/test-with-rhino-jar.js +0 -9
@@ -0,0 +1,209 @@
1
+ /*
2
+ Packer version 3.0 (beta 8) - copyright 2004-2007, Dean Edwards
3
+ http://www.opensource.org/licenses/mit-license
4
+ */
5
+
6
+ eval(base2.namespace);
7
+
8
+ var IGNORE = RegGrp.IGNORE;
9
+ var REMOVE = "";
10
+ var SPACE = " ";
11
+ var WORDS = /\w+/g;
12
+
13
+ var Packer = Base.extend({
14
+ minify: function(script) {
15
+ script = script.replace(Packer.CONTINUE, "");
16
+ script = Packer.data.exec(script);
17
+ script = Packer.whitespace.exec(script);
18
+ script = Packer.clean.exec(script);
19
+ return script;
20
+ },
21
+
22
+ pack: function(script, base62, shrink) {
23
+ script = this.minify(script + "\n");
24
+ if (shrink) script = this._shrinkVariables(script);
25
+ if (base62) script = this._base62Encode(script);
26
+ return script;
27
+ },
28
+
29
+ _base62Encode: function(script) {
30
+ var words = new Words(script);
31
+ var encode = function(word) {
32
+ return words.fetch(word).encoded;
33
+ };
34
+
35
+ /* build the packed script */
36
+
37
+ var p = this._escape(script.replace(WORDS, encode));
38
+ var a = Math.min(Math.max(words.count(), 2), 62);
39
+ var c = words.count();
40
+ var k = words;
41
+ var e = Packer["ENCODE" + (a > 10 ? a > 36 ? 62 : 36 : 10)];
42
+ var r = a > 10 ? "e(c)" : "c";
43
+
44
+ // the whole thing
45
+ return format(Packer.UNPACK, p,a,c,k,e,r);
46
+ },
47
+
48
+ _escape: function(script) {
49
+ // single quotes wrap the final string so escape them
50
+ // also escape new lines required by conditional comments
51
+ return script.replace(/([\\'])/g, "\\$1").replace(/[\r\n]+/g, "\\n");
52
+ },
53
+
54
+ _shrinkVariables: function(script) {
55
+ // Windows Scripting Host cannot do regexp.test() on global regexps.
56
+ var global = function(regexp) {
57
+ // This function creates a global version of the passed regexp.
58
+ return new RegExp(regexp.source, "g");
59
+ };
60
+
61
+ var data = []; // encoded strings and regular expressions
62
+ var REGEXP = /^[^'"]\//;
63
+ var store = function(string) {
64
+ var replacement = "#" + data.length;
65
+ if (REGEXP.test(string)) {
66
+ replacement = string.charAt(0) + replacement;
67
+ string = string.slice(1);
68
+ }
69
+ data.push(string);
70
+ return replacement;
71
+ };
72
+
73
+ // Base52 encoding (a-Z)
74
+ var encode52 = function(c) {
75
+ return (c < 52 ? '' : arguments.callee(parseInt(c / 52))) +
76
+ ((c = c % 52) > 25 ? String.fromCharCode(c + 39) : String.fromCharCode(c + 97));
77
+ };
78
+
79
+ // identify blocks, particularly identify function blocks (which define scope)
80
+ var BLOCK = /(function\s*[\w$]*\s*\(\s*([^\)]*)\s*\)\s*)?(\{([^{}]*)\})/;
81
+ var VAR_ = /var\s+/g;
82
+ var VAR_NAME = /var\s+[\w$]+/g;
83
+ var COMMA = /\s*,\s*/;
84
+ var blocks = []; // store program blocks (anything between braces {})
85
+ // encoder for program blocks
86
+ var encode = function(block, func, args) {
87
+ if (func) { // the block is a function block
88
+
89
+ // decode the function block (THIS IS THE IMPORTANT BIT)
90
+ // We are retrieving all sub-blocks and will re-parse them in light
91
+ // of newly shrunk variables
92
+ block = decode(block);
93
+
94
+ // create the list of variable and argument names
95
+ var vars = match(block, VAR_NAME).join(",").replace(VAR_, "");
96
+ var ids = Array2.combine(args.split(COMMA).concat(vars.split(COMMA)));
97
+
98
+ // process each identifier
99
+ var count = 0, shortId;
100
+ forEach (ids, function(id) {
101
+ id = trim(id);
102
+ if (id && id.length > 1) { // > 1 char
103
+ id = rescape(id);
104
+ // find the next free short name (check everything in the current scope)
105
+ do shortId = encode52(count++);
106
+ while (new RegExp("[^\\w$.]" + shortId + "[^\\w$:]").test(block));
107
+ // replace the long name with the short name
108
+ var reg = new RegExp("([^\\w$.])" + id + "([^\\w$:])");
109
+ while (reg.test(block)) block = block.replace(global(reg), "$1" + shortId + "$2");
110
+ var reg = new RegExp("([^{,])" + id + ":", "g");
111
+ block = block.replace(reg, "$1" + shortId + ":");
112
+ }
113
+ });
114
+ }
115
+ var replacement = "~" + blocks.length + "~";
116
+ blocks.push(block);
117
+ return replacement;
118
+ };
119
+
120
+ // decoder for program blocks
121
+ var ENCODED = /~(\d+)~/;
122
+ var decode = function(script) {
123
+ while (ENCODED.test(script)) {
124
+ script = script.replace(global(ENCODED), function(match, index) {
125
+ return blocks[index];
126
+ });
127
+ }
128
+ return script;
129
+ };
130
+
131
+ // encode strings and regular expressions
132
+ script = Packer.data.exec(script, store);
133
+
134
+ // remove closures (this is for base2 namespaces only)
135
+ script = script.replace(/new function\(_\)\s*\{/g, "{;#;");
136
+
137
+ // encode blocks, as we encode we replace variable and argument names
138
+ while (BLOCK.test(script)) {
139
+ script = script.replace(global(BLOCK), encode);
140
+ }
141
+
142
+ // put the blocks back
143
+ script = decode(script);
144
+
145
+ // put back the closure (for base2 namespaces only)
146
+ script = script.replace(/\{;#;/g, "new function(_){");
147
+
148
+ // put strings and regular expressions back
149
+ script = script.replace(/#(\d+)/g, function(match, index) {
150
+ return data[index];
151
+ });
152
+
153
+ return script;
154
+ }
155
+ }, {
156
+ CONTINUE: /\\\r?\n/g,
157
+
158
+ ENCODE10: "String",
159
+ ENCODE36: "function(c){return c.toString(a)}",
160
+ ENCODE62: "function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))}",
161
+
162
+ UNPACK: "eval(function(p,a,c,k,e,r){e=%5;if(!''.replace(/^/,String)){while(c--)r[%6]=k[c]" +
163
+ "||%6;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p." +
164
+ "replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('%1',%2,%3,'%4'.split('|'),0,{}))",
165
+
166
+ init: function() {
167
+ this.data = reduce(this.data, function(data, replacement, expression) {
168
+ data.store(this.javascript.exec(expression), replacement);
169
+ return data;
170
+ }, new RegGrp, this);
171
+ this.clean = this.data.union(this.clean);
172
+ this.whitespace = this.data.union(this.whitespace);
173
+ },
174
+
175
+ clean: {
176
+ "\\(\\s*;\\s*;\\s*\\)": "(;;)", // for (;;) loops
177
+ "throw[^};]+[};]": IGNORE, // a safari 1.3 bug
178
+ ";+\\s*([};])": "$1"
179
+ },
180
+
181
+ data: {
182
+ // strings
183
+ "STRING1": IGNORE,
184
+ 'STRING2': IGNORE,
185
+ "CONDITIONAL": IGNORE, // conditional comments
186
+ "(COMMENT1)\\n\\s*(REGEXP)?": "\n$3",
187
+ "(COMMENT2)\\s*(REGEXP)?": " $3",
188
+ "([\\[(\\^=,{}:;&|!*?])\\s*(REGEXP)": "$1$2"
189
+ },
190
+
191
+ javascript: new RegGrp({
192
+ COMMENT1: /(\/\/|;;;)[^\n]*/.source,
193
+ COMMENT2: /\/\*[^*]*\*+([^\/][^*]*\*+)*\//.source,
194
+ CONDITIONAL: /\/\*@|@\*\/|\/\/@[^\n]*\n/.source,
195
+ REGEXP: /\/(\\[\/\\]|[^*\/])(\\.|[^\/\n\\])*\/[gim]*/.source,
196
+ STRING1: /'(\\.|[^'\\])*'/.source,
197
+ STRING2: /"(\\.|[^"\\])*"/.source
198
+ }),
199
+
200
+ whitespace: {
201
+ "(\\d)\\s+(\\.\\s*[a-z\\$_\\[(])": "$1 $2", // http://dean.edwards.name/weblog/2007/04/packer3/#comment84066
202
+ "([+-])\\s+([+-])": "$1 $2", // c = a++ +b;
203
+ "\\b\\s+\\$\\s+\\b": " $ ", // var $ in
204
+ "\\$\\s+\\b": "$ ", // object$ in
205
+ "\\b\\s+\\$": " $", // return $object
206
+ "\\b\\s+\\b": SPACE,
207
+ "\\s+": REMOVE
208
+ }
209
+ });
@@ -0,0 +1,106 @@
1
+ /*
2
+ ParseMaster, version 1.0.2 (2005-08-19)
3
+ Copyright 2005, Dean Edwards
4
+ License: http://creativecommons.org/licenses/LGPL/2.1/
5
+ */
6
+
7
+ /* a multi-pattern parser */
8
+
9
+ // KNOWN BUG: erroneous behavior when using escapeChar with a replacement value that is a function
10
+
11
+ function ParseMaster() {
12
+ // constants
13
+ var $EXPRESSION = 0, $REPLACEMENT = 1, $LENGTH = 2;
14
+ // used to determine nesting levels
15
+ var $GROUPS = /\(/g, $SUB_REPLACE = /\$\d/, $INDEXED = /^\$\d+$/,
16
+ $TRIM = /(['"])\1\+(.*)\+\1\1$/, $$ESCAPE = /\\./g, $QUOTE = /'/,
17
+ $$DELETED = /\x01[^\x01]*\x01/g;
18
+ var self = this;
19
+ // public
20
+ this.add = function($expression, $replacement) {
21
+ if (!$replacement) $replacement = "";
22
+ // count the number of sub-expressions
23
+ // - add one because each pattern is itself a sub-expression
24
+ var $length = (_internalEscape(String($expression)).match($GROUPS) || "").length + 1;
25
+ // does the pattern deal with sub-expressions?
26
+ if ($SUB_REPLACE.test($replacement)) {
27
+ // a simple lookup? (e.g. "$2")
28
+ if ($INDEXED.test($replacement)) {
29
+ // store the index (used for fast retrieval of matched strings)
30
+ $replacement = parseInt($replacement.slice(1)) - 1;
31
+ } else { // a complicated lookup (e.g. "Hello $2 $1")
32
+ // build a function to do the lookup
33
+ var i = $length;
34
+ var $quote = $QUOTE.test(_internalEscape($replacement)) ? '"' : "'";
35
+ while (i) $replacement = $replacement.split("$" + i--).join($quote + "+a[o+" + i + "]+" + $quote);
36
+ $replacement = new Function("a,o", "return" + $quote + $replacement.replace($TRIM, "$1") + $quote);
37
+ }
38
+ }
39
+ // pass the modified arguments
40
+ _add($expression || "/^$/", $replacement, $length);
41
+ };
42
+ // execute the global replacement
43
+ this.exec = function($string) {
44
+ _escaped.length = 0;
45
+ return _unescape(_escape($string, this.escapeChar).replace(
46
+ new RegExp(_patterns, this.ignoreCase ? "gi" : "g"), _replacement), this.escapeChar).replace($$DELETED, "");
47
+ };
48
+ // clear the patterns collection so that this object may be re-used
49
+ this.reset = function() {
50
+ _patterns.length = 0;
51
+ };
52
+
53
+ // private
54
+ var _escaped = []; // escaped characters
55
+ var _patterns = []; // patterns stored by index
56
+ var _toString = function(){return "(" + String(this[$EXPRESSION]).slice(1, -1) + ")"};
57
+ _patterns.toString = function(){return this.join("|")};
58
+ // create and add a new pattern to the patterns collection
59
+ function _add() {
60
+ arguments.toString = _toString;
61
+ // store the pattern - as an arguments object (i think this is quicker..?)
62
+ _patterns[_patterns.length] = arguments;
63
+ }
64
+ // this is the global replace function (it's quite complicated)
65
+ function _replacement() {
66
+ if (!arguments[0]) return "";
67
+ var i = 1, j = 0, $pattern;
68
+ // loop through the patterns
69
+ while ($pattern = _patterns[j++]) {
70
+ // do we have a result?
71
+ if (arguments[i]) {
72
+ var $replacement = $pattern[$REPLACEMENT];
73
+ switch (typeof $replacement) {
74
+ case "function": return $replacement(arguments, i);
75
+ case "number": return arguments[$replacement + i];
76
+ }
77
+ var $delete = (arguments[i].indexOf(self.escapeChar) == -1) ? "" :
78
+ "\x01" + arguments[i] + "\x01";
79
+ return $delete + $replacement;
80
+ // skip over references to sub-expressions
81
+ } else i += $pattern[$LENGTH];
82
+ }
83
+ };
84
+ // encode escaped characters
85
+ function _escape($string, $escapeChar) {
86
+ return $escapeChar ? $string.replace(new RegExp("\\" + $escapeChar + "(.)", "g"), function($match, $char) {
87
+ _escaped[_escaped.length] = $char;
88
+ return $escapeChar;
89
+ }) : $string;
90
+ };
91
+ // decode escaped characters
92
+ function _unescape($string, $escapeChar) {
93
+ var i = 0;
94
+ return $escapeChar ? $string.replace(new RegExp("\\" + $escapeChar, "g"), function() {
95
+ return $escapeChar + (_escaped[i++] || "");
96
+ }) : $string;
97
+ };
98
+ function _internalEscape($string) {
99
+ return $string.replace($$ESCAPE, "");
100
+ };
101
+ };
102
+ ParseMaster.prototype = {
103
+ constructor: ParseMaster,
104
+ ignoreCase: false,
105
+ escapeChar: ""
106
+ };
@@ -0,0 +1,62 @@
1
+
2
+ var Words = Collection.extend({
3
+ constructor: function(script) {
4
+ this.base();
5
+ forEach (script.match(WORDS), this.add, this);
6
+ this.encode();
7
+ },
8
+
9
+ add: function(word) {
10
+ if (!this.exists(word)) this.base(word);
11
+ word = this.fetch(word);
12
+ word.count++;
13
+ return word;
14
+ },
15
+
16
+ encode: function() {
17
+ // sort by frequency
18
+ this.sort(function(word1, word2) {
19
+ return word2.count - word1.count;
20
+ });
21
+
22
+ eval("var a=62,e=" + Packer.ENCODE62);
23
+ var encode = e;
24
+ var encoded = new Collection; // a dictionary of base62 -> base10
25
+ var count = this.count();
26
+ for (var i = 0; i < count; i++) {
27
+ encoded.store(encode(i), i);
28
+ }
29
+
30
+ var empty = function() {return ""};
31
+ var index = 0;
32
+ forEach (this, function(word) {
33
+ if (encoded.exists(word)) {
34
+ word.index = encoded.fetch(word);
35
+ word.toString = empty;
36
+ } else {
37
+ while (this.exists(encode(index))) index++;
38
+ word.index = index++;
39
+ }
40
+ word.encoded = encode(word.index);
41
+ }, this);
42
+
43
+ // sort by encoding
44
+ this.sort(function(word1, word2) {
45
+ return word1.index - word2.index;
46
+ });
47
+ },
48
+
49
+ toString: function() {
50
+ return this.values().join("|");
51
+ }
52
+ }, {
53
+ Item: {
54
+ constructor: function(word) {
55
+ this.toString = function() {return word};
56
+ },
57
+
58
+ count: 0,
59
+ encoded: "",
60
+ index: -1
61
+ }
62
+ });