@@ -15,7 +15,7 @@ if (typeof window === 'undefined') {
1515const 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 }
0 commit comments