[JavaScript] 格式化js源代码 →→→→→进入此内容的聊天室

来自 , 2019-12-23, 写在 JavaScript, 查看 106 次.
URL http://www.code666.cn/view/aba53da2
  1. // javascript format script
  2. // copyright Stephen Chapman 22 April 2007
  3. var kw = ['abstract', 'as', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class', 'const', 'continue', 'default', 'delete', 'do', 'double', 'else', 'enum', 'export', 'extends', 'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'long', 'namespace', 'native', 'new', 'null', 'package', 'private', 'protected', 'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'use', 'var', 'void', 'volatile', 'while', 'with'];
  4. var ob = ['Anchor', 'anchors', 'Applet', 'applets', 'Area', 'Array', 'Body', 'Button', 'Checkbox', 'Date', 'document', 'FileUpload', 'Form', 'forms', 'Frame', 'frames', 'Hidden', 'History', 'history', 'Image', 'images', 'Layer', 'layers', 'Link', 'links', 'location', 'Math', 'MimeType', 'mimeTypes', 'navigator', 'Number', 'Option', 'options', 'Password', 'Plugin', 'plugins', 'Radio', 'RegExp', 'Reset', 'screen', 'Script', 'Select', 'String', 'Style', 'StyleSheet', 'Submit', 'Text', 'Textarea', 'window'];
  5. var sym = '-+*<=>?:&|/!%';
  6. function JSfmt(s) {
  7.    this.i = this.r = this.lvl = this.pr = 0;
  8.    this.row = [''];
  9.    this.lW = this.nC = this.pC = '';
  10.    this.decode = function() {
  11.       s = s.replace(/(\r\n|\r|\n)/g, '\n');
  12.       while (this.i < s.length) {
  13.          var c = s.charAt(this.i);
  14.          if (s.length - 1 == this.i) this.nC = '';
  15.          else this.nC = s.charAt(this.i + 1);
  16.          if (/\w/.test(c)) {if (this.lW) this.lW += c;
  17.          else this.lW = c;
  18.          this.row[this.r] += c;
  19.          }
  20.       else switch (c) {
  21.          case '\n': break;
  22.          case ' ': case '\t': this.hl();
  23.          this.row[this.r] += ' ';
  24.          break;
  25.          case '.': this.hl();
  26.          this.row[this.r] += '.';
  27.          break;
  28.          case '{': this.hl();
  29.          var currentLine = this.row[this.r];
  30.          if (currentLine.length) {
  31.             var lastChar = currentLine[currentLine.length - 1];
  32.             if (lastChar != '&nbsp;' && lastChar != '\t') this.row[this.r] += ' {';
  33.             else this.row[this.r] += '{';
  34.             }
  35.          else this.row[this.r] += '{';
  36.          this.lvl++;
  37.          this.wl();
  38.          break;
  39.          case '}': this.hl();
  40.          this.lvl--;
  41.          this.row[this.r] += '}';
  42.          if (';' != this.nC) this.wl();
  43.          break;
  44.          case ';': this.hl();
  45.          this.row[this.r] += '; ';
  46.          if (this.pr == 0) this.wl();
  47.          break;
  48.          case '(': this.pr++;
  49.          this.hl();
  50.          this.row[this.r] += '(';
  51.          break;
  52.          case ')': this.pr--;
  53.          this.hl();
  54.          this.row[this.r] += ')';
  55.          break;
  56.          case '"': case "'" : this.hl();
  57.          var escaped = false;
  58.          this.row[this.r] += '<span class="literal">' + c;
  59.          while (this.i < s.length - 1) {
  60.             this.i++;
  61.             var ch = s.charAt(this.i);
  62.             if (ch == '\\') escaped = !escaped;
  63.             if (ch == '&') ch = '&';
  64.             if (ch == '<') ch = '<';
  65.             if (ch == '>') ch = '>';
  66.             this.row[this.r] += ch;
  67.             if (c == ch && !escaped) {
  68.                this.row[this.r] += '<\/span>';
  69.                break;
  70.                }
  71.             if (ch != '\\') escaped = false;
  72.             }
  73.          break;
  74.          case '/': if ('/' == this.nC) {
  75.             this.row[this.r] += '<span class="comment">//';
  76.             this.i++;
  77.             while (this.i < s.length - 1) {
  78.                this.i++;
  79.                var c = s.charAt(this.i);
  80.                if (c == '&') c = '&';
  81.                if (c == '<') c = '<';
  82.                if (c == '>') c = '>';
  83.                if (c == '\n') {
  84.                   this.row[this.r] += '<\/span>';
  85.                   this.wl();
  86.                   break;
  87.                   }
  88.                this.row[this.r] += c;
  89.                }
  90.             }
  91.          else if (this.nC == '*') {
  92.             this.row[this.r] += '<span class="comment">/*';
  93.             this.i++;
  94.             var c = '';
  95.             var prevC = '';
  96.             while (this.i < s.length - 1) {
  97.                this.i++;
  98.                prevC = c;
  99.                c = s.charAt(this.i);
  100.                if (c == '&nbsp;' || c == '\t' || c == '\n') {
  101.                   if (c == '&nbsp;') {
  102.                      if (this.row[this.r]) this.row[this.r] += '&nbsp;';
  103.                      }
  104.                   else if (c == '\t') {
  105.                      if (this.row[this.r]) this.row[this.r] += '&nbsp;&nbsp; ';
  106.                      }
  107.                   else if (c == '\n') this.wl();
  108.                   }
  109.                else this.row[this.r] += c;
  110.                if (c == '/' && prevC == '*') {
  111.                   this.row[this.r] += '<\/span>';
  112.                   break;
  113.                   }
  114.                }
  115.             this.wl();
  116.             }
  117.          else if (this.lW) {
  118.             this.hl();
  119.             if (this.nC == '=') this.row[this.r] += ' /';
  120.             else this.row[this.r] += ' / ';
  121.             }
  122.          else if (this.pC == '*') this.row[this.r] += '/ ';
  123.          else if (this.pC == ')') this.row[this.r] += ' / ';
  124.          else {
  125.             if (this.pC == '=') this.row[this.r] += ' /';
  126.             else this.row[this.r] += '/';
  127.             while (this.i < s.length - 1) {
  128.                this.i++;
  129.                var c = s.charAt(this.i);
  130.                if (c == '(') this.pr++;
  131.                if (c == ')') this.pr--;
  132.                if (c == '\\') escaped = !escaped;
  133.                this.row[this.r] += c;
  134.                if (c == ';' && this.pr == 0) {
  135.                   this.wl();
  136.                   break;
  137.                   }
  138.                if (c == '/' && this.pr == 0) if (!escaped) break;
  139.                if (c != '\\') escaped = false;
  140.                }
  141.             }
  142.          break;
  143.          case ',': this.hl();
  144.          this.row[this.r] += ', ';
  145.          break;
  146.          case '-': case '+': case '*': case '%': case '<': case '=': case '>': case '?': case ':': case '&': case '|': case '!': this.hl();
  147.          if (c == '!' && this.nC != '=') {
  148.             this.row[this.r] += c;
  149.             break;
  150.             }
  151.          if (c == ':' && this.pC == "'") {
  152.             this.row[this.r] += c;
  153.             break;
  154.             }
  155.          if (c == '+' || c == '-') if (c == this.nC || c == this.pC) {
  156.             this.row[this.r] += c;
  157.             break;
  158.             }
  159.          if (sym.indexOf(this.pC) != - 1) {
  160.             if (sym.indexOf(this.nC) != - 1) this.row[this.r] += c;
  161.             else this.row[this.r] += c + ' ';
  162.             }
  163.          else {
  164.             if (sym.indexOf(this.nC) != - 1) this.row[this.r] += ' ' + c;
  165.             else this.row[this.r] += ' ' + c + ' ';
  166.             }
  167.          break;
  168.          default : this.hl();
  169.          this.row[this.r] += c;
  170.          break;
  171.          }
  172.       if (!/\w/.test(c)) if (c != ' ' && c != '\t') {this.hl();
  173.       this.lW = '';
  174.       }
  175.    this.pC = c;
  176.    this.i++;
  177.    }
  178. return this.row.join('<br />');
  179. }
  180. this.hl = function() {
  181. if (this.lW && kw.indexOf(this.lW) != - 1) {
  182.    this.row[this.r] = this.row[this.r].substr(0, this.row[this.r].lastIndexOf(this.lW)) + '<span class="keyword">' + this.lW + '<\/span>';
  183.    this.lW = '';
  184.    }
  185. else if (this.lW && ob.indexOf(this.lW) != - 1) {
  186.    this.row[this.r] = this.row[this.r].substr(0, this.row[this.r].lastIndexOf(this.lW)) + '<span class="object">' + this.lW + '<\/span>';
  187.    this.lW = '';
  188.    }
  189. }
  190. this.wl = function() {
  191. this.row.push('');
  192. this.r++;
  193. var i = 0;
  194. while (i < this.lvl) {
  195.    this.row[this.r] += '&nbsp;&nbsp; ';
  196.    i++;
  197.    }
  198. }
  199. }
  200. if (typeof Array.prototype.indexOf == 'undefined') {
  201. Array.prototype.indexOf = function(item) {
  202. for (var i = 0; i < this.length; i++) {
  203.    if ((typeof this[i] == typeof item) && (this[i] == item)) {
  204.       return i;
  205.       }
  206.    }
  207. return - 1;
  208. }
  209. }
  210. function decode() {
  211. var jsformat = new JSfmt(document.getElementById('in').value);
  212. res(jsformat.decode());
  213. }
  214. function res(t) {
  215. var mh = screen.height - 150;
  216. var mw = screen.width - 20;
  217. TheResWin = window.open('', 'format', 'height=' + mh + ',width=' + mw + ',toolbar=no,directories=no,status=no,' + 'menubar=no,scrollbars=yes,resizable=yes');
  218. TheResWin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http:\/\/www.w3.org\/1999\/xhtml">\n<head><title>Results<\/title><style type="text/css">#out {font: normal 10pt Courier,"Courier New",monospace; background: #f5f5f5;}\n.keyword {color:#0000ff;}\n.object {color:#ff00ff;}\n.literal {color:#cc9966;}\n.comment {color:#999999;}\n</style><\/head>\n<body ><p align="right"><a href="#" onclick="self.close();return false;">Close Window<\/a><\/p><div id="out">' + t + '</div><p align="right"><a href="#" onclick="self.close();return false;">Close Window<\/a><\/p><\/body><\/html>');
  219. TheResWin.document.close();
  220. TheResWin.moveTo(0, 0);
  221. TheResWin.focus();
  222. }

回复 "格式化js源代码"

这儿你可以回复上面这条便签

captcha