Skip to content

Commit 444ff32

Browse files
committed
skeleton t2s library.
1 parent 413ee7a commit 444ff32

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/**
2+
* jquery.t2s.js
3+
*
4+
* A Traditional Chinese to Simplified Chinese conversion library based on jquery-s2t and OpenCC data.
5+
* Copyright Rime Developers
6+
* ---
7+
* jquery-s2t v0.1.0
8+
*
9+
* https://github.com/hustlzp/jquery-s2t
10+
* A jQuery plugin to convert between Simplified Chinese and Traditional Chinese.
11+
* Tested in IE6+, Chrome, Firefox.
12+
*
13+
* Copyright 2013-2014 hustlzp
14+
* Released under the MIT license
15+
*/
16+
17+
(function($) {
18+
19+
/**
20+
* 简体字
21+
* @const
22+
*/
23+
var S = '';
24+
25+
/**
26+
* 繁体字
27+
* @const
28+
*/
29+
var T = '';
30+
31+
/**
32+
* 转换文本
33+
* @param {String} str - 待转换的文本
34+
* @param {Boolean} toT - 是否转换成繁体
35+
* @returns {String} - 转换结果
36+
*/
37+
function tranStr(str, toT) {
38+
var i;
39+
var letter;
40+
var code;
41+
var isChinese;
42+
var index;
43+
var src, des;
44+
var result = '';
45+
46+
if (toT) {
47+
src = S;
48+
des = T;
49+
} else {
50+
src = T;
51+
des = S;
52+
}
53+
54+
if (typeof str !== "string") {
55+
return str;
56+
}
57+
58+
for (i = 0; i < str.length; i++) {
59+
letter = str.charAt(i);
60+
code = str.charCodeAt(i);
61+
62+
// 根据字符的Unicode判断是否为汉字,以提高性能
63+
// 参考:
64+
// [1] http://www.unicode.org
65+
// [2] http://zh.wikipedia.org/wiki/Unicode%E5%AD%97%E7%AC%A6%E5%88%97%E8%A1%A8
66+
// [3] http://xylonwang.iteye.com/blog/519552
67+
isChinese = (code > 0x3400 && code < 0x9FC3) || (code > 0xF900 && code < 0xFA6A);
68+
69+
if (!isChinese) {
70+
result += letter;
71+
continue;
72+
}
73+
74+
index = src.indexOf(letter);
75+
76+
if (index !== -1) {
77+
result += des.charAt(index);
78+
} else {
79+
result += letter;
80+
}
81+
}
82+
83+
return result;
84+
}
85+
86+
/**
87+
* 转换HTML Element属性
88+
* @param {Element} element - 待转换的HTML Element节点
89+
* @param {String|Array} attr - 待转换的属性/属性列表
90+
* @param {Boolean} toT - 是否转换成繁体
91+
*/
92+
function tranAttr(element, attr, toT) {
93+
var i, attrValue;
94+
95+
if (attr instanceof Array) {
96+
for(i = 0; i < attr.length; i++) {
97+
tranAttr(element, attr[i], toT);
98+
}
99+
} else {
100+
attrValue = element.getAttribute(attr);
101+
102+
if (attrValue !== "" && attrValue !== null) {
103+
element.setAttribute(attr, tranStr(attrValue, toT));
104+
}
105+
}
106+
}
107+
108+
/**
109+
* 转换HTML Element节点
110+
* @param {Element} element - 待转换的HTML Element节点
111+
* @param {Boolean} toT - 是否转换成繁体
112+
*/
113+
function tranElement(element, toT) {
114+
var i;
115+
var childNodes;
116+
117+
if (element.nodeType !== 1) {
118+
return;
119+
}
120+
121+
childNodes = element.childNodes;
122+
123+
for (i = 0; i < childNodes.length; i++) {
124+
var childNode = childNodes.item(i);
125+
126+
// 若为HTML Element节点
127+
if (childNode.nodeType === 1) {
128+
// 对以下标签不做处理
129+
if ("|BR|HR|TEXTAREA|SCRIPT|OBJECT|EMBED|".indexOf("|" + childNode.tagName + "|") !== -1) {
130+
continue;
131+
}
132+
133+
tranAttr(childNode, ['title', 'data-original-title', 'alt', 'placeholder'], toT);
134+
135+
// input 标签
136+
// 对text类型的input输入框不做处理
137+
if (childNode.tagName === "INPUT"
138+
&& childNode.value !== ""
139+
&& childNode.type !== "text"
140+
&& childNode.type !== "hidden")
141+
{
142+
childNode.value = tranStr(childNode.value, toT);
143+
}
144+
145+
// 继续递归调用
146+
tranElement(childNode, toT);
147+
} else if (childNode.nodeType === 3) { // 若为文本节点
148+
childNode.data = tranStr(childNode.data, toT);
149+
}
150+
}
151+
}
152+
153+
// 扩展jQuery全局方法
154+
$.extend({
155+
/**
156+
* 文本简转繁
157+
* @param {String} str - 待转换的文本
158+
* @returns {String} 转换结果
159+
*/
160+
s2t: function(str) {
161+
return tranStr(str, true);
162+
},
163+
164+
/**
165+
* 文本繁转简
166+
* @param {String} str - 待转换的文本
167+
* @returns {String} 转换结果
168+
*/
169+
t2s: function(str) {
170+
return tranStr(str, false);
171+
}
172+
});
173+
174+
// 扩展jQuery对象方法
175+
$.fn.extend({
176+
/**
177+
* jQuery Objects简转繁
178+
* @this {jQuery Objects} 待转换的jQuery Objects
179+
*/
180+
s2t: function() {
181+
return this.each(function() {
182+
tranElement(this, true);
183+
});
184+
},
185+
186+
/**
187+
* jQuery Objects繁转简
188+
* @this {jQuery Objects} 待转换的jQuery Objects
189+
*/
190+
t2s: function() {
191+
return this.each(function() {
192+
tranElement(this, false);
193+
});
194+
}
195+
});
196+
}) (jQuery);

0 commit comments

Comments
 (0)