1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | module.exports = preferredCharsets;
|
17 | module.exports.preferredCharsets = preferredCharsets;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | var simpleCharsetRegExp = /^\s*([^\s;]+)\s*(?:;(.*))?$/;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function parseAcceptCharset(accept) {
|
32 | var accepts = accept.split(',');
|
33 |
|
34 | for (var i = 0, j = 0; i < accepts.length; i++) {
|
35 | var charset = parseCharset(accepts[i].trim(), i);
|
36 |
|
37 | if (charset) {
|
38 | accepts[j++] = charset;
|
39 | }
|
40 | }
|
41 |
|
42 |
|
43 | accepts.length = j;
|
44 |
|
45 | return accepts;
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function parseCharset(str, i) {
|
54 | var match = simpleCharsetRegExp.exec(str);
|
55 | if (!match) return null;
|
56 |
|
57 | var charset = match[1];
|
58 | var q = 1;
|
59 | if (match[2]) {
|
60 | var params = match[2].split(';')
|
61 | for (var j = 0; j < params.length; j++) {
|
62 | var p = params[j].trim().split('=');
|
63 | if (p[0] === 'q') {
|
64 | q = parseFloat(p[1]);
|
65 | break;
|
66 | }
|
67 | }
|
68 | }
|
69 |
|
70 | return {
|
71 | charset: charset,
|
72 | q: q,
|
73 | i: i
|
74 | };
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | function getCharsetPriority(charset, accepted, index) {
|
83 | var priority = {o: -1, q: 0, s: 0};
|
84 |
|
85 | for (var i = 0; i < accepted.length; i++) {
|
86 | var spec = specify(charset, accepted[i], index);
|
87 |
|
88 | if (spec && (priority.s - spec.s || priority.q - spec.q || priority.o - spec.o) < 0) {
|
89 | priority = spec;
|
90 | }
|
91 | }
|
92 |
|
93 | return priority;
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function specify(charset, spec, index) {
|
102 | var s = 0;
|
103 | if(spec.charset.toLowerCase() === charset.toLowerCase()){
|
104 | s |= 1;
|
105 | } else if (spec.charset !== '*' ) {
|
106 | return null
|
107 | }
|
108 |
|
109 | return {
|
110 | i: index,
|
111 | o: spec.i,
|
112 | q: spec.q,
|
113 | s: s
|
114 | }
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | function preferredCharsets(accept, provided) {
|
123 |
|
124 | var accepts = parseAcceptCharset(accept === undefined ? '*' : accept || '');
|
125 |
|
126 | if (!provided) {
|
127 |
|
128 | return accepts
|
129 | .filter(isQuality)
|
130 | .sort(compareSpecs)
|
131 | .map(getFullCharset);
|
132 | }
|
133 |
|
134 | var priorities = provided.map(function getPriority(type, index) {
|
135 | return getCharsetPriority(type, accepts, index);
|
136 | });
|
137 |
|
138 |
|
139 | return priorities.filter(isQuality).sort(compareSpecs).map(function getCharset(priority) {
|
140 | return provided[priorities.indexOf(priority)];
|
141 | });
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | function compareSpecs(a, b) {
|
150 | return (b.q - a.q) || (b.s - a.s) || (a.o - b.o) || (a.i - b.i) || 0;
|
151 | }
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | function getFullCharset(spec) {
|
159 | return spec.charset;
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | function isQuality(spec) {
|
168 | return spec.q > 0;
|
169 | }
|