日期:2014-05-16  浏览次数:20343 次

关于 Javascript Tidy 的中文编码问题

Javascript   Tidy(只支持Firefox)

来自:
http://rick.measham.id.au/paste/jstidy.htm
http://www.howtocreate.co.uk/tutorials/jsexamples/JSTidy.html

我改进了一下:
http://www.wjmhxx.com/shortway/Tools/JsTidy1.htm
http://www.wjmhxx.com/shortway/Tools/JsTidy2.htm

改进说明:
1、代码缩进;
2、//注释的保留。我的保留方法很简陋粗鄙,有更好的么?(因为我没有系统学习Javascript的)。/*...*/形式的注释尚未思考。
3、中文字符的编码问题未解决。
比如:
alert( "你好! ");
转换后成了:
alert( "\u4F60\u597D\uFF01 ");
不利于阅读。我只能在EmEditor中自编一个宏进行替换。

附   http://www.wjmhxx.com/shortway/Tools/JsTidy1.htm   源代码(因为网络问题,有时打不开):

===============================================

<HTML>

<HEAD>
<META   NAME= "keywords "   CONTENT= "Javascript   Tidy;Js   Tidy;JsTidy ">
<META   HTTP-EQUIV= "Content-Type "   CONTENT= "text/html;   charset=utf-8 ">
<SCRIPT>

function   tidy()
{
var   D   =   document.getElementById( "A ").value;
D   =   D.replace(/^(\s*)(.*?)\/\/\s*?([^\s].*?)$/gm,   "$1$2\n$1var   rem   =   new   Function(\ "$3\ ");\n ");

D   =   D.replace(/(var   rem   =   new   Function\(\ ".*?)\ "(.*?)\ "(.*?)\ "(.*?)\ "(.*?\ "\);)$/gm,   "$1&rem;$2&rem;$3&rem;$4&rem;$5 ");
D   =   D.replace(/(var   rem   =   new   Function\(\ ".*?)\ "(.*?)\ "(.*?)\ "(.*?\ "\);)$/gm,   "$1&rem;$2&rem;$3&rem;$4 ");
D   =   D.replace(/(var   rem   =   new   Function\(\ ".*?)\ "(.*?)\ "(.*?)\ "(.*?\ "\);)$/gm,   "$1&rem;$2&rem;$3&rem;$4 ");
D   =   D.replace(/(var   rem   =   new   Function\(\ ".*?)\ "(.*?)\ "(.*?\ "\);)$/gm,   "$1&rem;$2&rem;$3 ");
D   =   D.replace(/(var   rem   =   new   Function\(\ ".*?)\ "(.*?)\ "(.*?\ "\);)$/gm,   "$1&rem;$2&rem;$3 ");
D   =   D.replace(/(var   rem   =   new   Function\(\ ".*?)\ "(.*?)\ "(.*?\ "\);)$/gm,   "$1&rem;$2&rem;$3 ");
D   =   D.replace(/(var   rem   =   new   Function\(\ ".*?)\ "(.*?\ "\);)$/gm,   "$1&rem;$2 ");


//Debug
//document.getElementById( "B ").value   =   D;   Break();//Debug

eval( "function   plzTidy(){ "   +   D   +   "} ");
store   =   document.getElementById( "store ");
store.value   =   plzTidy;
var   X   =   store.value;

//Debug
//document.getElementById( "B ").value   =   X;   Break();

X   =   X.replace(/function   plzTidy\(\)   \{/,   " ").replace(/\}\s*$/,   " ").replace(/([\r\n]+)/g,   "$1 ");

//   Four   spaces   to   a   Tab(\\t)
while   (X.search(/^(\t*)   {4}/gim)   >   -1)
{
X   =   X.replace(/^(\t*)   {4}/gim,   "$1\t ");
}

// "{ "   and   "} "   at   a   new   li