Skip to content

Commit 6beccbb

Browse files
committed
Update opencc-data to 1.0.5
1 parent 666f3e1 commit 6beccbb

File tree

7 files changed

+96
-75
lines changed

7 files changed

+96
-75
lines changed

README-en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Pure JavaScript implementation of OpenCC
77
In HTML file:
88

99
```html
10-
<script src="https://cdn.jsdelivr.net/npm/opencc-js@0.3.3"></script>
10+
<script src="https://cdn.jsdelivr.net/npm/opencc-js@0.3.4"></script>
1111
```
1212

1313
Or in Node.js:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
在 HTML 中引入:
1010

1111
```html
12-
<script src="https://cdn.jsdelivr.net/npm/opencc-js@0.3.3"></script>
12+
<script src="https://cdn.jsdelivr.net/npm/opencc-js@0.3.4"></script>
1313
```
1414

1515
或在 Node.js 中引入:

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "opencc-js",
3-
"version": "0.3.3",
3+
"version": "0.3.4",
44
"description": "Pure JavaScript implementation of OpenCC",
55
"main": "src/main.js",
66
"scripts": {
@@ -29,7 +29,7 @@
2929
},
3030
"homepage": "https://github.com/nk2028/opencc-js#readme",
3131
"dependencies": {
32-
"opencc-data": "^1.0.4"
32+
"opencc-data": "^1.0.5"
3333
},
3434
"devDependencies": {
3535
"chai": "^4.2.0"

src/main.js

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if (typeof window === 'undefined') {
1515
const OpenCC = {
1616
/**
1717
* 將一組資料加入字典樹
18-
*
18+
*
1919
* @param {Map Object} t 字典樹
2020
* @param {String} s 來源字串
2121
* @param {String} v 替換的字詞
@@ -61,10 +61,11 @@ const OpenCC = {
6161
const getDict = (typeof window === 'undefined') ? getDictTextNode : getDictText;
6262

6363
let DICTS;
64-
if (type == 'from')
64+
if (type === 'from') {
6565
DICTS = DICT_FROM[s];
66-
else if (type == 'to')
66+
} else if (type === 'to') {
6767
DICTS = DICT_TO[s];
68+
}
6869
const t = new Map();
6970
for (const DICT of DICTS) {
7071
const txt = await getDict(DICT);
@@ -81,7 +82,7 @@ const OpenCC = {
8182

8283
/**
8384
* 使用字典樹轉換一段文字
84-
*
85+
*
8586
* @param {Map Object} t 字典樹
8687
* @param {String} s 要被轉換的文字
8788
* @returns {String} 轉換後的字串
@@ -131,30 +132,35 @@ const OpenCC = {
131132

132133
async Converter(fromVariant, toVariant) {
133134
let dictFrom, dictTo;
134-
if (fromVariant != 't')
135+
if (fromVariant !== 't') {
135136
dictFrom = await this._load_dict(fromVariant, 'from');
136-
if (toVariant != 't')
137+
}
138+
if (toVariant !== 't') {
137139
dictTo = await this._load_dict(toVariant, 'to');
138-
return s => {
139-
if (fromVariant != 't')
140-
s = this._convert(dictFrom, s);
141-
if (toVariant != 't')
142-
s = this._convert(dictTo, s);
143-
return s;
144-
};
140+
}
141+
return (s) => {
142+
if (fromVariant !== 't') {
143+
s = this._convert(dictFrom, s);
144+
}
145+
if (toVariant !== 't') {
146+
s = this._convert(dictTo, s);
147+
}
148+
return s;
149+
};
145150
},
146151

147152
CustomConverter(dict) {
148153
const t = new Map();
149-
for (const [k, v] of Object.entries(dict))
154+
for (const [k, v] of Object.entries(dict)) {
150155
this._addWord(t, k, v);
151-
return s => this._convert(t, s);
156+
}
157+
return (s) => this._convert(t, s);
152158
},
153159

154160
HTMLConverter(convertFunc, startNode, fromLangTag, toLangTag) {
155161
function convert() {
156162
function _inner(currentNode, langMatched) {
157-
if (currentNode.lang == fromLangTag) {
163+
if (currentNode.lang === fromLangTag) {
158164
langMatched = true;
159165
currentNode.shouldChangeLang = true; // 記住 lang 屬性被修改了,以便恢復
160166
currentNode.lang = toLangTag;
@@ -164,67 +170,79 @@ const OpenCC = {
164170

165171
if (langMatched) {
166172
/* Do not convert these elements */
167-
if (currentNode.tagName == 'SCRIPT')
168-
return;
169-
else if (currentNode.tagName == 'STYLE')
170-
return;
173+
if (currentNode.tagName === 'SCRIPT') return;
174+
if (currentNode.tagName === 'STYLE') return;
171175

172176
/* 處理特殊屬性 */
173-
else if (currentNode.tagName == 'META' && currentNode.name == 'description') {
174-
if (currentNode.originalContent === undefined)
177+
else if (currentNode.tagName === 'META' && currentNode.name === 'description') {
178+
if (currentNode.originalContent === undefined) {
175179
currentNode.originalContent = currentNode.content;
180+
}
176181
currentNode.content = convertFunc(currentNode.originalContent);
177-
} else if (currentNode.tagName == 'META' && currentNode.name == 'keywords') {
178-
if (currentNode.originalContent === undefined)
182+
} else if (currentNode.tagName === 'META' && currentNode.name === 'keywords') {
183+
if (currentNode.originalContent === undefined) {
179184
currentNode.originalContent = currentNode.content;
185+
}
180186
currentNode.content = convertFunc(currentNode.originalContent);
181-
} else if (currentNode.tagName == 'IMG') {
182-
if (currentNode.originalAlt === undefined)
187+
} else if (currentNode.tagName === 'IMG') {
188+
if (currentNode.originalAlt === undefined) {
183189
currentNode.originalAlt = currentNode.alt;
190+
}
184191
currentNode.alt = convertFunc(currentNode.originalAlt);
185-
} else if (currentNode.tagName == 'INPUT' && currentNode.type == 'button'){
186-
if (currentNode.originalValue === undefined)
192+
} else if (currentNode.tagName === 'INPUT' && currentNode.type === 'button') {
193+
if (currentNode.originalValue === undefined) {
187194
currentNode.originalValue = currentNode.value;
195+
}
188196
currentNode.value = convertFunc(currentNode.originalValue);
189197
}
190198
}
191199

192-
for (const node of currentNode.childNodes)
193-
if (node.nodeType == Node.TEXT_NODE && langMatched) {
194-
if (node.originalString === undefined)
200+
for (const node of currentNode.childNodes) {
201+
if (node.nodeType === Node.TEXT_NODE && langMatched) {
202+
if (node.originalString === undefined) {
195203
node.originalString = node.nodeValue; // 存儲原始字串,以便恢復
204+
}
196205
node.nodeValue = convertFunc(node.originalString);
197-
} else
206+
} else {
198207
_inner(node, langMatched);
208+
}
209+
}
199210
}
200211
_inner(startNode, false);
201212
}
202213

203214
function restore() {
204215
function _inner(currentNode) {
205-
if (currentNode.shouldChangeLang)
216+
if (currentNode.shouldChangeLang) {
206217
currentNode.lang = fromLangTag;
218+
}
207219

208-
if (currentNode.originalString !== undefined)
220+
if (currentNode.originalString !== undefined) {
209221
currentNode.nodeValue = currentNode.originalString;
222+
}
210223

211224
/* 處理特殊屬性 */
212-
if (currentNode.tagName == 'META' && currentNode.name == 'description') {
213-
if (currentNode.originalContent !== undefined)
225+
if (currentNode.tagName === 'META' && currentNode.name === 'description') {
226+
if (currentNode.originalContent !== undefined) {
214227
currentNode.content = currentNode.originalContent;
215-
} else if (currentNode.tagName == 'META' && currentNode.name == 'keywords') {
216-
if (currentNode.originalContent !== undefined)
228+
}
229+
} else if (currentNode.tagName === 'META' && currentNode.name === 'keywords') {
230+
if (currentNode.originalContent !== undefined) {
217231
currentNode.content = currentNode.originalContent;
218-
} else if (currentNode.tagName == 'IMG') {
219-
if (currentNode.originalAlt !== undefined)
232+
}
233+
} else if (currentNode.tagName === 'IMG') {
234+
if (currentNode.originalAlt !== undefined) {
220235
currentNode.alt = currentNode.originalAlt;
221-
} else if (currentNode.tagName == 'INPUT' && currentNode.type == 'button'){
222-
if (currentNode.originalValue !== undefined)
236+
}
237+
} else if (currentNode.tagName === 'INPUT' && currentNode.type === 'button'){
238+
if (currentNode.originalValue !== undefined) {
223239
currentNode.value = currentNode.originalValue;
240+
}
224241
}
225242

226-
for (const node of currentNode.childNodes)
243+
for (const node of currentNode.childNodes) {
227244
_inner(node);
245+
}
228246
}
229247
_inner(startNode);
230248
}

test/index.html

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,20 @@
66
<meta name="description" content="測試 JavaScript 簡繁轉換"/>
77
<meta name="keywords" content="中文轉換, 測試"/>
88
<script src="../src/main.js"></script>
9-
<script>
10-
let HTMLConvertHandler;
11-
async function start加載() {
12-
const convert = await OpenCC.Converter('hk', 'cn');
13-
const startNode = document.documentElement;
14-
HTMLConvertHandler = OpenCC.HTMLConverter(convert, startNode, 'zh-HK', 'zh-CN');
15-
output2.innerText = '加載完成';
16-
}
17-
async function start轉換() {
18-
HTMLConvertHandler.convert();
19-
}
20-
async function start恢復() {
21-
HTMLConvertHandler.restore();
22-
}
23-
function alert繁體() {
24-
output.innerText = '繁體';
25-
}
26-
</script>
9+
<script src="index.js"></script>
2710
</head>
2811
<body>
29-
<h1><span lang="en-HK">opencc-js</span> 測試頁面</h1>
30-
<p>此處應該轉為簡體<span>測試遞歸轉換</span>,標題也應該轉換</p>
31-
<p><img alt="此處應該轉為簡體" src="https://example.com/404.img"/></p>
32-
<p><input type="button" onclick="alert繁體(); return false" value="點擊後應該出現繁體,不應轉換"/> <span id="output"></span></p>
33-
<p lang="zh-TW">臺灣文本不用轉換</p>
12+
<h1><span lang="en-HK">opencc-js</span> 測試頁面,標題應該轉換為簡體。</h1>
13+
<p>這句話應該轉換為簡體<span>span 也應該遞歸轉換為簡體</span></p>
14+
<p><img alt="此處應該轉換為簡體" src="https://example.com/some/path/404.jpg"/></p>
15+
<p><input type="button" onclick="alert繁體(); return false" value="點擊後應該出現繁體,不應轉換"/> <span id="output"></span></p>
16+
<p lang="zh-TW">這句話為 zh-TW,lang 不匹配,不用轉換為簡體</p>
3417
<hr/>
3518
<p>
3619
<input type="button" onclick="start加載(); return false" value="開始加載"/>
3720
<input type="button" onclick="start轉換(); return false" value="開始轉換"/>
3821
<input type="button" onclick="start恢復(); return false" value="開始恢復"/>
3922
</p>
40-
<p id="output2"></p>
23+
<p id="output2">點擊「開始加載」後,請等待加載完成。</p>
4124
</body>
4225
</html>

test/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let HTMLConvertHandler;
2+
3+
async function start加載() {
4+
const convert = await OpenCC.Converter('hk', 'cn');
5+
const startNode = document.documentElement;
6+
HTMLConvertHandler = OpenCC.HTMLConverter(convert, startNode, 'zh-HK', 'zh-CN');
7+
output2.innerText = '加載完成';
8+
}
9+
10+
async function start轉換() {
11+
HTMLConvertHandler.convert();
12+
}
13+
14+
async function start恢復() {
15+
HTMLConvertHandler.restore();
16+
}
17+
18+
function alert繁體() {
19+
output.innerText = '繁體';
20+
}

0 commit comments

Comments
 (0)