PageRenderTime 887ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/data/addon/scripts/jscolorizer.js

https://github.com/jinbo51/DiscuzX
JavaScript | 275 lines | 239 code | 13 blank | 23 comment | 59 complexity | 048a22ba2e20e4ea229c847861a41a6d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * DO NOT REMOVE THIS NOTICE
  3. *
  4. * PROJECT: JsDecoder
  5. * VERSION: 1.1.0
  6. * COPYRIGHT: (c) 2004-2008 Cezary Tomczak
  7. * LINK: http://code.gosu.pl
  8. * LICENSE: GPL
  9. */
  10. function JsColorizer() {
  11. this.color = {
  12. "keyword": "#0000FF",
  13. "object": "#FF0000",
  14. "quotation": "#FF00FF",
  15. "comment": "#008000"
  16. };
  17. this.s = ""; // code to colorize
  18. this.i = 0;
  19. this.len = 0;
  20. this.ret = ""; // colorized code
  21. this.lastWord = ""; // last alphanumeric word
  22. this.nextChar = "";
  23. this.prevChar = "";
  24. this.code = [""];
  25. this.row = 0;
  26. this.times = {
  27. quotation: 0, quotation_calls: 0,
  28. lineComment: 0, lineComment_calls: 0,
  29. comment: 0, comment_calls: 0,
  30. slash: 0, slash_calls: 0,
  31. word: 0, word_calls: 0
  32. };
  33. this.write = function (s)
  34. {
  35. this.code[this.row] += s;
  36. if (s.length == 1) {
  37. this.prevChar = s;
  38. } else {
  39. this.prevCharInit();
  40. }
  41. };
  42. this.writeLine = function ()
  43. {
  44. this.code.push("");
  45. this.row++;
  46. this.prevChar = "\n";
  47. };
  48. this.prevCharInit = function ()
  49. {
  50. this.prevChar = this.code[this.row].charAt(this.code[this.row].length - 1);
  51. };
  52. this.showTimes = function ()
  53. {
  54. var ret = '';
  55. for (var f in this.times) {
  56. var t = this.times[f];
  57. if (/_calls/.test(f)) {
  58. ret += f+': '+t+"\n";
  59. } else {
  60. ret += f+': '+time_round(t)+" sec\n";
  61. }
  62. }
  63. return ret;
  64. };
  65. this.colorize = function()
  66. {
  67. this.len = this.s.length;
  68. while (this.i < this.len)
  69. {
  70. var c = this.s.charAt(this.i);
  71. if (this.len - 1 == this.i) {
  72. this.nextChar = "";
  73. } else {
  74. this.nextChar = this.s.charAt(this.i + 1);
  75. }
  76. switch (c) {
  77. case "\n":
  78. if (this.lastWord.length) { this.word(); }
  79. this.lastWord = '';
  80. this.writeLine();
  81. break;
  82. case "'":
  83. case '"':
  84. if (this.lastWord.length) { this.word(); }
  85. this.lastWord = '';
  86. this.quotation(c);
  87. break;
  88. case "/":
  89. if (this.lastWord.length) { this.word(); }
  90. this.lastWord = '';
  91. if ("/" == this.nextChar) {
  92. this.lineComment();
  93. } else if ("*" == this.nextChar) {
  94. this.comment();
  95. } else {
  96. this.slash();
  97. }
  98. break;
  99. default:
  100. if (/^\w$/.test(c)) {
  101. this.lastWord += c;
  102. } else {
  103. if (this.lastWord.length) { this.word(); }
  104. this.lastWord = '';
  105. this.write(c);
  106. }
  107. break;
  108. }
  109. this.i++;
  110. }
  111. this.write(this.lastWord);
  112. return this.code.join("\n");
  113. };
  114. this.quotation = function(quotation)
  115. {
  116. //var time = time_start();
  117. var s = quotation;
  118. var escaped = false;
  119. while (this.i < this.len - 1) {
  120. this.i++;
  121. var c = this.s.charAt(this.i);
  122. if ("\\" == c) {
  123. escaped = (escaped ? false : true);
  124. }
  125. s += c;
  126. if (c == quotation) {
  127. if (!escaped) {
  128. break;
  129. }
  130. }
  131. if ("\\" != c) {
  132. escaped = false;
  133. }
  134. }
  135. this.write('<font color="'+this.color.quotation+'">' + s + '</font>');
  136. //this.times.quotation += time_get(time);
  137. //this.times.quotation_calls++;
  138. };
  139. this.lineComment = function()
  140. {
  141. //var time = time_start();
  142. var s = "//";
  143. this.i++;
  144. while (this.i < this.len - 1) {
  145. this.i++;
  146. var c = this.s.charAt(this.i);
  147. s += c;
  148. if ("\n" == c) {
  149. break;
  150. }
  151. }
  152. this.write('<font color="'+this.color.comment+'">' + s + '</font>');
  153. //this.times.lineComment += time_get(time);
  154. //this.times.lineComment_calls++;
  155. };
  156. this.comment = function()
  157. {
  158. //var time = time_start();
  159. var s = "/*";
  160. this.i++;
  161. var c = "";
  162. var prevC = "";
  163. while (this.i < this.len - 1) {
  164. this.i++;
  165. prevC = c;
  166. c = this.s.charAt(this.i);
  167. s += c;
  168. if ("/" == c && "*" == prevC) {
  169. break;
  170. }
  171. }
  172. this.write('<font color="'+this.color.comment+'">' + s + '</font>');
  173. //this.times.comment += time_get(time);
  174. //this.times.comment_calls++;
  175. };
  176. /* SLASH
  177. * divisor /= or *\/ (4/5 , a/5)
  178. * regexp /\w/ (//.test() , var asd = /some/;) */
  179. this.slash = function()
  180. {
  181. //var time = time_start();
  182. var a_i = this.i - 1;
  183. var a_c = this.s.charAt(a_i);
  184. for (a_i = this.i - 1; a_i >= 0; a_i--) {
  185. var c2 = this.s.charAt(a_i);
  186. if (" " == c2 || "\t" == c2) {
  187. continue;
  188. }
  189. a_c = this.s.charAt(a_i);
  190. break;
  191. }
  192. var a = /^\w+$/.test(a_c) || ']' == a_c || ')' == a_c;
  193. var b = ("*" == this.prevChar);
  194. if (a || b) {
  195. if (a) {
  196. if ("=" == this.nextChar) {
  197. this.write("/");
  198. } else {
  199. this.write("/");
  200. }
  201. } else if (b) {
  202. this.write("/");
  203. }
  204. } else if (')' == this.prevChar) {
  205. this.write('/');
  206. } else {
  207. var ret = '';
  208. if ("=" == this.prevChar) {
  209. ret += "/";
  210. } else {
  211. ret += "/";
  212. }
  213. var escaped = false;
  214. while (this.i < this.len - 1) {
  215. this.i++;
  216. var c = this.s.charAt(this.i);
  217. if ("\\" == c) {
  218. escaped = (escaped ? false : true);
  219. }
  220. ret += c;
  221. if ("/" == c) {
  222. if (!escaped) {
  223. break;
  224. }
  225. }
  226. if ("\\" != c) {
  227. escaped = false;
  228. }
  229. }
  230. this.write('<font color="'+this.color.quotation+'">' + ret + '</font>');
  231. }
  232. //this.times.slash += time_get(time);
  233. //this.times.slash_calls++;
  234. };
  235. this.word = function()
  236. {
  237. //var time = time_start();
  238. if (this.keywords.indexOf(this.lastWord) != -1) {
  239. this.write('<font color="'+this.color.keyword+'">' + this.lastWord + '</font>');
  240. } else if (this.objects.indexOf(this.lastWord) != -1) {
  241. this.write('<font color="'+this.color.object+'">' + this.lastWord + '</font>');
  242. } else {
  243. this.write(this.lastWord);
  244. }
  245. //this.times.word += time_get(time);
  246. //this.times.word_calls++;
  247. };
  248. this.keywords = ["abstract", "boolean", "break", "byte", "case", "catch", "char", "class",
  249. "const", "continue", "default", "delete", "do", "double", "else", "extends", "false",
  250. "final", "finally", "float", "for", "function", "goto", "if", "implements", "import",
  251. "in", "instanceof", "int", "interface", "long", "native", "new", "null", "package",
  252. "private", "protected", "public", "return", "short", "static", "super", "switch",
  253. "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof", "var",
  254. "void", "while", "with"];
  255. this.objects = ["Anchor", "anchors", "Applet", "applets", "Area", "Array", "Button", "Checkbox",
  256. "Date", "document", "FileUpload", "Form", "forms", "Frame", "frames", "Hidden", "history",
  257. "Image", "images", "Link", "links", "Area", "location", "Math", "MimeType", "mimeTypes",
  258. "navigator", "options", "Password", "Plugin", "plugins", "Radio", "Reset", "Select",
  259. "String", "Submit", "Text", "Textarea", "window"];
  260. }