• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3/**
4 * @see https://encoding.spec.whatwg.org/#concept-encoding-get
5 * @param {string|undefined} label
6 */
7function getEncoding (label) {
8  if (!label) {
9    return 'failure'
10  }
11
12  // 1. Remove any leading and trailing ASCII whitespace from label.
13  // 2. If label is an ASCII case-insensitive match for any of the
14  //    labels listed in the table below, then return the
15  //    corresponding encoding; otherwise return failure.
16  switch (label.trim().toLowerCase()) {
17    case 'unicode-1-1-utf-8':
18    case 'unicode11utf8':
19    case 'unicode20utf8':
20    case 'utf-8':
21    case 'utf8':
22    case 'x-unicode20utf8':
23      return 'UTF-8'
24    case '866':
25    case 'cp866':
26    case 'csibm866':
27    case 'ibm866':
28      return 'IBM866'
29    case 'csisolatin2':
30    case 'iso-8859-2':
31    case 'iso-ir-101':
32    case 'iso8859-2':
33    case 'iso88592':
34    case 'iso_8859-2':
35    case 'iso_8859-2:1987':
36    case 'l2':
37    case 'latin2':
38      return 'ISO-8859-2'
39    case 'csisolatin3':
40    case 'iso-8859-3':
41    case 'iso-ir-109':
42    case 'iso8859-3':
43    case 'iso88593':
44    case 'iso_8859-3':
45    case 'iso_8859-3:1988':
46    case 'l3':
47    case 'latin3':
48      return 'ISO-8859-3'
49    case 'csisolatin4':
50    case 'iso-8859-4':
51    case 'iso-ir-110':
52    case 'iso8859-4':
53    case 'iso88594':
54    case 'iso_8859-4':
55    case 'iso_8859-4:1988':
56    case 'l4':
57    case 'latin4':
58      return 'ISO-8859-4'
59    case 'csisolatincyrillic':
60    case 'cyrillic':
61    case 'iso-8859-5':
62    case 'iso-ir-144':
63    case 'iso8859-5':
64    case 'iso88595':
65    case 'iso_8859-5':
66    case 'iso_8859-5:1988':
67      return 'ISO-8859-5'
68    case 'arabic':
69    case 'asmo-708':
70    case 'csiso88596e':
71    case 'csiso88596i':
72    case 'csisolatinarabic':
73    case 'ecma-114':
74    case 'iso-8859-6':
75    case 'iso-8859-6-e':
76    case 'iso-8859-6-i':
77    case 'iso-ir-127':
78    case 'iso8859-6':
79    case 'iso88596':
80    case 'iso_8859-6':
81    case 'iso_8859-6:1987':
82      return 'ISO-8859-6'
83    case 'csisolatingreek':
84    case 'ecma-118':
85    case 'elot_928':
86    case 'greek':
87    case 'greek8':
88    case 'iso-8859-7':
89    case 'iso-ir-126':
90    case 'iso8859-7':
91    case 'iso88597':
92    case 'iso_8859-7':
93    case 'iso_8859-7:1987':
94    case 'sun_eu_greek':
95      return 'ISO-8859-7'
96    case 'csiso88598e':
97    case 'csisolatinhebrew':
98    case 'hebrew':
99    case 'iso-8859-8':
100    case 'iso-8859-8-e':
101    case 'iso-ir-138':
102    case 'iso8859-8':
103    case 'iso88598':
104    case 'iso_8859-8':
105    case 'iso_8859-8:1988':
106    case 'visual':
107      return 'ISO-8859-8'
108    case 'csiso88598i':
109    case 'iso-8859-8-i':
110    case 'logical':
111      return 'ISO-8859-8-I'
112    case 'csisolatin6':
113    case 'iso-8859-10':
114    case 'iso-ir-157':
115    case 'iso8859-10':
116    case 'iso885910':
117    case 'l6':
118    case 'latin6':
119      return 'ISO-8859-10'
120    case 'iso-8859-13':
121    case 'iso8859-13':
122    case 'iso885913':
123      return 'ISO-8859-13'
124    case 'iso-8859-14':
125    case 'iso8859-14':
126    case 'iso885914':
127      return 'ISO-8859-14'
128    case 'csisolatin9':
129    case 'iso-8859-15':
130    case 'iso8859-15':
131    case 'iso885915':
132    case 'iso_8859-15':
133    case 'l9':
134      return 'ISO-8859-15'
135    case 'iso-8859-16':
136      return 'ISO-8859-16'
137    case 'cskoi8r':
138    case 'koi':
139    case 'koi8':
140    case 'koi8-r':
141    case 'koi8_r':
142      return 'KOI8-R'
143    case 'koi8-ru':
144    case 'koi8-u':
145      return 'KOI8-U'
146    case 'csmacintosh':
147    case 'mac':
148    case 'macintosh':
149    case 'x-mac-roman':
150      return 'macintosh'
151    case 'iso-8859-11':
152    case 'iso8859-11':
153    case 'iso885911':
154    case 'tis-620':
155    case 'windows-874':
156      return 'windows-874'
157    case 'cp1250':
158    case 'windows-1250':
159    case 'x-cp1250':
160      return 'windows-1250'
161    case 'cp1251':
162    case 'windows-1251':
163    case 'x-cp1251':
164      return 'windows-1251'
165    case 'ansi_x3.4-1968':
166    case 'ascii':
167    case 'cp1252':
168    case 'cp819':
169    case 'csisolatin1':
170    case 'ibm819':
171    case 'iso-8859-1':
172    case 'iso-ir-100':
173    case 'iso8859-1':
174    case 'iso88591':
175    case 'iso_8859-1':
176    case 'iso_8859-1:1987':
177    case 'l1':
178    case 'latin1':
179    case 'us-ascii':
180    case 'windows-1252':
181    case 'x-cp1252':
182      return 'windows-1252'
183    case 'cp1253':
184    case 'windows-1253':
185    case 'x-cp1253':
186      return 'windows-1253'
187    case 'cp1254':
188    case 'csisolatin5':
189    case 'iso-8859-9':
190    case 'iso-ir-148':
191    case 'iso8859-9':
192    case 'iso88599':
193    case 'iso_8859-9':
194    case 'iso_8859-9:1989':
195    case 'l5':
196    case 'latin5':
197    case 'windows-1254':
198    case 'x-cp1254':
199      return 'windows-1254'
200    case 'cp1255':
201    case 'windows-1255':
202    case 'x-cp1255':
203      return 'windows-1255'
204    case 'cp1256':
205    case 'windows-1256':
206    case 'x-cp1256':
207      return 'windows-1256'
208    case 'cp1257':
209    case 'windows-1257':
210    case 'x-cp1257':
211      return 'windows-1257'
212    case 'cp1258':
213    case 'windows-1258':
214    case 'x-cp1258':
215      return 'windows-1258'
216    case 'x-mac-cyrillic':
217    case 'x-mac-ukrainian':
218      return 'x-mac-cyrillic'
219    case 'chinese':
220    case 'csgb2312':
221    case 'csiso58gb231280':
222    case 'gb2312':
223    case 'gb_2312':
224    case 'gb_2312-80':
225    case 'gbk':
226    case 'iso-ir-58':
227    case 'x-gbk':
228      return 'GBK'
229    case 'gb18030':
230      return 'gb18030'
231    case 'big5':
232    case 'big5-hkscs':
233    case 'cn-big5':
234    case 'csbig5':
235    case 'x-x-big5':
236      return 'Big5'
237    case 'cseucpkdfmtjapanese':
238    case 'euc-jp':
239    case 'x-euc-jp':
240      return 'EUC-JP'
241    case 'csiso2022jp':
242    case 'iso-2022-jp':
243      return 'ISO-2022-JP'
244    case 'csshiftjis':
245    case 'ms932':
246    case 'ms_kanji':
247    case 'shift-jis':
248    case 'shift_jis':
249    case 'sjis':
250    case 'windows-31j':
251    case 'x-sjis':
252      return 'Shift_JIS'
253    case 'cseuckr':
254    case 'csksc56011987':
255    case 'euc-kr':
256    case 'iso-ir-149':
257    case 'korean':
258    case 'ks_c_5601-1987':
259    case 'ks_c_5601-1989':
260    case 'ksc5601':
261    case 'ksc_5601':
262    case 'windows-949':
263      return 'EUC-KR'
264    case 'csiso2022kr':
265    case 'hz-gb-2312':
266    case 'iso-2022-cn':
267    case 'iso-2022-cn-ext':
268    case 'iso-2022-kr':
269    case 'replacement':
270      return 'replacement'
271    case 'unicodefffe':
272    case 'utf-16be':
273      return 'UTF-16BE'
274    case 'csunicode':
275    case 'iso-10646-ucs-2':
276    case 'ucs-2':
277    case 'unicode':
278    case 'unicodefeff':
279    case 'utf-16':
280    case 'utf-16le':
281      return 'UTF-16LE'
282    case 'x-user-defined':
283      return 'x-user-defined'
284    default: return 'failure'
285  }
286}
287
288module.exports = {
289  getEncoding
290}
291