[JavaScript] 用js脚本 格式化js代码 高亮js代码 →→→→→进入此内容的聊天室

来自 , 2020-11-03, 写在 JavaScript, 查看 133 次.
URL http://www.code666.cn/view/5fef3eff
  1. // javascript format script
  2. // copyright Stephen Chapman 22 April 2007
  3.  
  4. 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'];
  5. 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'];
  6. var sym = '-+*<=>?:&|/!%';
  7.  
  8. function JSfmt(s) {
  9.         this.i = this.r = this.lvl = this.pr = 0;
  10.         this.row = [''];
  11.         this.lW = this.nC = this.pC = '';
  12.  
  13.         this.decode = function() {
  14.                 s = s.replace(/(\r\n|\r|\n)/g, '\n');
  15.                 while (this.i < s.length) {
  16.                         var c = s.charAt(this.i);
  17.                         if (s.length - 1 == this.i) this.nC = '';
  18.                         else this.nC = s.charAt(this.i + 1);
  19.                         if (/\w/.test(c)) {
  20.                                 if (this.lW) this.lW += c;
  21.                                 else this.lW = c;
  22.                                 this.row[this.r] += c;
  23.                         } else switch (c) {
  24.                         case '\n':
  25.                                 break;
  26.                         case ' ':
  27.                         case '\t':
  28.                                 this.hl();
  29.                                 this.row[this.r] += ' ';
  30.                                 break;
  31.                         case '.':
  32.                                 this.hl();
  33.                                 this.row[this.r] += '.';
  34.                                 break;
  35.                         case '{':
  36.                                 this.hl();
  37.                                 var currentLine = this.row[this.r];
  38.                                 if (currentLine.length) {
  39.                                         var lastChar = currentLine[currentLine.length - 1];
  40.                                         if (lastChar != '&nbsp;' && lastChar != '\t') this.row[this.r] += ' {';
  41.                                         else this.row[this.r] += '{';
  42.                                 } else this.row[this.r] += '{';
  43.                                 this.lvl++;
  44.                                 this.wl();
  45.                                 break;
  46.                         case '}':
  47.                                 this.hl();
  48.                                 this.lvl--;
  49.                                 this.row[this.r] += '}';
  50.                                 if (';' != this.nC) this.wl();
  51.                                 break;
  52.                         case ';':
  53.                                 this.hl();
  54.                                 this.row[this.r] += '; ';
  55.                                 if (this.pr == 0) this.wl();
  56.                                 break;
  57.                         case '(':
  58.                                 this.pr++;
  59.                                 this.hl();
  60.                                 this.row[this.r] += '(';
  61.                                 break;
  62.                         case ')':
  63.                                 this.pr--;
  64.                                 this.hl();
  65.                                 this.row[this.r] += ')';
  66.                                 break;
  67.                         case '"':
  68.                         case "'":
  69.                                 this.hl();
  70.                                 var escaped = false;
  71.                                 this.row[this.r] += '<span class="literal">' + c;
  72.                                 while (this.i < s.length - 1) {
  73.                                         this.i++;
  74.                                         var ch = s.charAt(this.i);
  75.                                         if (ch == '\\') escaped = !escaped;
  76.                                         if (ch == '&') ch = '&';
  77.                                         if (ch == '<') ch = '<';
  78.                                         if (ch == '>') ch = '>';
  79.                                         this.row[this.r] += ch;
  80.                                         if (c == ch && !escaped) {
  81.                                                 this.row[this.r] += '<\/span>';
  82.                                                 break;
  83.                                         }
  84.                                         if (ch != '\\') escaped = false;
  85.                                 }
  86.                                 break;
  87.                         case '/':
  88.                                 if ('/' == this.nC) {
  89.                                         this.row[this.r] += '<span class="comment">//';
  90.                                         this.i++;
  91.                                         while (this.i < s.length - 1) {
  92.                                                 this.i++;
  93.                                                 var c = s.charAt(this.i);
  94.                                                 if (c == '&') c = '&';
  95.                                                 if (c == '<') c = '<';
  96.                                                 if (c == '>') c = '>';
  97.                                                 if (c == '\n') {
  98.                                                         this.row[this.r] += '<\/span>';
  99.                                                         this.wl();
  100.                                                         break;
  101.                                                 }
  102.                                                 this.row[this.r] += c;
  103.                                         }
  104.                                 } else if (this.nC == '*') {
  105.                                         this.row[this.r] += '<span class="comment">/*';
  106.                                         this.i++;
  107.                                         var c = '';
  108.                                         var prevC = '';
  109.                                         while (this.i < s.length - 1) {
  110.                                                 this.i++;
  111.                                                 prevC = c;
  112.                                                 c = s.charAt(this.i);
  113.                                                 if (c == '&nbsp;' || c == '\t' || c == '\n') {
  114.                                                         if (c == '&nbsp;') {
  115.                                                                 if (this.row[this.r]) this.row[this.r] += '&nbsp;';
  116.                                                         } else if (c == '\t') {
  117.                                                                 if (this.row[this.r]) this.row[this.r] += '&nbsp;&nbsp; ';
  118.                                                         } else if (c == '\n') this.wl();
  119.                                                 } else this.row[this.r] += c;
  120.                                                 if (c == '/' && prevC == '*') {
  121.                                                         this.row[this.r] += '<\/span>';
  122.                                                         break;
  123.                                                 }
  124.                                         }
  125.                                         this.wl();
  126.                                 } else if (this.lW) {
  127.                                         this.hl();
  128.                                         if (this.nC == '=') this.row[this.r] += ' /';
  129.                                         else this.row[this.r] += ' / ';
  130.                                 } else if (this.pC == '*') this.row[this.r] += '/ ';
  131.                                 else if (this.pC == ')') this.row[this.r] += ' / ';
  132.                                 else {
  133.                                         if (this.pC == '=') this.row[this.r] += ' /';
  134.                                         else this.row[this.r] += '/';
  135.                                         while (this.i < s.length - 1) {
  136.                                                 this.i++;
  137.                                                 var c = s.charAt(this.i);
  138.                                                 if (c == '(') this.pr++;
  139.                                                 if (c == ')') this.pr--;
  140.                                                 if (c == '\\') escaped = !escaped;
  141.                                                 this.row[this.r] += c;
  142.                                                 if (c == ';' && this.pr == 0) {
  143.                                                         this.wl();
  144.                                                         break;
  145.                                                 }
  146.                                                 if (c == '/' && this.pr == 0) if (!escaped) break;
  147.                                                 if (c != '\\') escaped = false;
  148.                                         }
  149.                                 }
  150.                                 break;
  151.                         case ',':
  152.                                 this.hl();
  153.                                 this.row[this.r] += ', ';
  154.                                 break;
  155.                         case '-':
  156.                         case '+':
  157.                         case '*':
  158.                         case '%':
  159.                         case '<':
  160.                         case '=':
  161.                         case '>':
  162.                         case '?':
  163.                         case ':':
  164.                         case '&':
  165.                         case '|':
  166.                         case '!':
  167.                                 this.hl();
  168.                                 if (c == '!' && this.nC != '=') {
  169.                                         this.row[this.r] += c;
  170.                                         break;
  171.                                 }
  172.                                 if (c == ':' && this.pC == "'") {
  173.                                         this.row[this.r] += c;
  174.                                         break;
  175.                                 }
  176.                                 if (c == '+' || c == '-') if (c == this.nC || c == this.pC) {
  177.                                         this.row[this.r] += c;
  178.                                         break;
  179.                                 }
  180.                                 if (sym.indexOf(this.pC) != -1) {
  181.                                         if (sym.indexOf(this.nC) != -1) this.row[this.r] += c;
  182.                                         else this.row[this.r] += c + ' ';
  183.                                 } else {
  184.                                         if (sym.indexOf(this.nC) != -1) this.row[this.r] += ' ' + c;
  185.                                         else this.row[this.r] += ' ' + c + ' ';
  186.                                 }
  187.                                 break;
  188.                         default:
  189.                                 this.hl();
  190.                                 this.row[this.r] += c;
  191.                                 break;
  192.                         }
  193.                         if (!/\w/.test(c)) if (c != ' ' && c != '\t') {
  194.                                 this.hl();
  195.                                 this.lW = '';
  196.                         }
  197.                         this.pC = c;
  198.                         this.i++;
  199.                 }
  200.                 return this.row.join('<br />');
  201.         }
  202.         this.hl = function() {
  203.                 if (this.lW && kw.indexOf(this.lW) != -1) {
  204.                         this.row[this.r] = this.row[this.r].substr(0, this.row[this.r].lastIndexOf(this.lW)) + '<span class="keyword">' + this.lW + '<\/span>';
  205.                         this.lW = '';
  206.                 } else if (this.lW && ob.indexOf(this.lW) != -1) {
  207.                         this.row[this.r] = this.row[this.r].substr(0, this.row[this.r].lastIndexOf(this.lW)) + '<span class="object">' + this.lW + '<\/span>';
  208.                         this.lW = '';
  209.                 }
  210.         }
  211.         this.wl = function() {
  212.                 this.row.push('');
  213.                 this.r++;
  214.                 var i = 0;
  215.                 while (i < this.lvl) {
  216.                         this.row[this.r] += '&nbsp;&nbsp; ';
  217.                         i++;
  218.                 }
  219.         }
  220. }
  221. if (typeof Array.prototype.indexOf == 'undefined') {
  222.         Array.prototype.indexOf = function(item) {
  223.                 for (var i = 0; i < this.length; i++) {
  224.                         if ((typeof this[i] == typeof item) && (this[i] == item)) {
  225.                                 return i;
  226.                         }
  227.                 }
  228.                 return - 1;
  229.         }
  230. }
  231. function decode() {
  232.         var jsformat = new JSfmt(document.getElementById('in').value);
  233.         res(jsformat.decode());
  234. }
  235.  
  236. function res(t) {
  237.         var mh = screen.height - 150;
  238.         var mw = screen.width - 20;
  239.         TheResWin = window.open('', 'format', 'height=' + mh + ',width=' + mw + ',toolbar=no,directories=no,status=no,' + 'menubar=no,scrollbars=yes,resizable=yes');
  240.         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>');
  241.         TheResWin.document.close();
  242.         TheResWin.moveTo(0, 0);
  243.         TheResWin.focus();
  244. }

回复 "用js脚本 格式化js代码 高亮js代码"

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

captcha