charset.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * negotiator
  3. * Copyright(c) 2012 Isaac Z. Schlueter
  4. * Copyright(c) 2014 Federico Romero
  5. * Copyright(c) 2014-2015 Douglas Christopher Wilson
  6. * MIT Licensed
  7. */
  8. 'use strict';
  9. /**
  10. * Module exports.
  11. * @public
  12. */
  13. module.exports = preferredCharsets;
  14. module.exports.preferredCharsets = preferredCharsets;
  15. /**
  16. * Module variables.
  17. * @private
  18. */
  19. var simpleCharsetRegExp = /^\s*([^\s;]+)\s*(?:;(.*))?$/;
  20. /**
  21. * Parse the Accept-Charset header.
  22. * @private
  23. */
  24. function parseAcceptCharset(accept) {
  25. var accepts = accept.split(',');
  26. for (var i = 0, j = 0; i < accepts.length; i++) {
  27. var charset = parseCharset(accepts[i].trim(), i);
  28. if (charset) {
  29. accepts[j++] = charset;
  30. }
  31. }
  32. // trim accepts
  33. accepts.length = j;
  34. return accepts;
  35. }
  36. /**
  37. * Parse a charset from the Accept-Charset header.
  38. * @private
  39. */
  40. function parseCharset(str, i) {
  41. var match = simpleCharsetRegExp.exec(str);
  42. if (!match) return null;
  43. var charset = match[1];
  44. var q = 1;
  45. if (match[2]) {
  46. var params = match[2].split(';')
  47. for (var j = 0; j < params.length; j++) {
  48. var p = params[j].trim().split('=');
  49. if (p[0] === 'q') {
  50. q = parseFloat(p[1]);
  51. break;
  52. }
  53. }
  54. }
  55. return {
  56. charset: charset,
  57. q: q,
  58. i: i
  59. };
  60. }
  61. /**
  62. * Get the priority of a charset.
  63. * @private
  64. */
  65. function getCharsetPriority(charset, accepted, index) {
  66. var priority = {o: -1, q: 0, s: 0};
  67. for (var i = 0; i < accepted.length; i++) {
  68. var spec = specify(charset, accepted[i], index);
  69. if (spec && (priority.s - spec.s || priority.q - spec.q || priority.o - spec.o) < 0) {
  70. priority = spec;
  71. }
  72. }
  73. return priority;
  74. }
  75. /**
  76. * Get the specificity of the charset.
  77. * @private
  78. */
  79. function specify(charset, spec, index) {
  80. var s = 0;
  81. if(spec.charset.toLowerCase() === charset.toLowerCase()){
  82. s |= 1;
  83. } else if (spec.charset !== '*' ) {
  84. return null
  85. }
  86. return {
  87. i: index,
  88. o: spec.i,
  89. q: spec.q,
  90. s: s
  91. }
  92. }
  93. /**
  94. * Get the preferred charsets from an Accept-Charset header.
  95. * @public
  96. */
  97. function preferredCharsets(accept, provided) {
  98. // RFC 2616 sec 14.2: no header = *
  99. var accepts = parseAcceptCharset(accept === undefined ? '*' : accept || '');
  100. if (!provided) {
  101. // sorted list of all charsets
  102. return accepts
  103. .filter(isQuality)
  104. .sort(compareSpecs)
  105. .map(getFullCharset);
  106. }
  107. var priorities = provided.map(function getPriority(type, index) {
  108. return getCharsetPriority(type, accepts, index);
  109. });
  110. // sorted list of accepted charsets
  111. return priorities.filter(isQuality).sort(compareSpecs).map(function getCharset(priority) {
  112. return provided[priorities.indexOf(priority)];
  113. });
  114. }
  115. /**
  116. * Compare two specs.
  117. * @private
  118. */
  119. function compareSpecs(a, b) {
  120. return (b.q - a.q) || (b.s - a.s) || (a.o - b.o) || (a.i - b.i) || 0;
  121. }
  122. /**
  123. * Get full charset string.
  124. * @private
  125. */
  126. function getFullCharset(spec) {
  127. return spec.charset;
  128. }
  129. /**
  130. * Check if a spec has any quality.
  131. * @private
  132. */
  133. function isQuality(spec) {
  134. return spec.q > 0;
  135. }