@@ -5,6 +5,12 @@ const { isValidHTTPToken, isomorphicDecode } = require('./util')
55
66const encoder = new TextEncoder ( )
77
8+ // Regex
9+ const HTTP_TOKEN_CODEPOINTS = / ^ [ ! # $ % & ' * + - . ^ _ | ~ A - z 0 - 9 ] + $ /
10+ const HTTP_WHITESPACE_REGEX = / ( \u000A | \u000D | \u0009 | \u0020 ) / // eslint-disable-line
11+ // https://mimesniff.spec.whatwg.org/#http-quoted-string-token-code-point
12+ const HTTP_QUOTED_STRING_TOKENS = / ^ ( \u0009 | \x { 0020 } - \x { 007 E} | \x { 0080 } - \x { 00 FF} ) + $ / // eslint-disable-line
13+
814// https://fetch.spec.whatwg.org/#data-url-processor
915/** @param {URL } dataURL */
1016function dataURLProcessor ( dataURL ) {
@@ -217,7 +223,7 @@ function parseMIMEType (input) {
217223 // 4. If type is the empty string or does not solely
218224 // contain HTTP token code points, then return failure.
219225 // https://mimesniff.spec.whatwg.org/#http-token-code-point
220- if ( type . length === 0 || ! / ^ [ ! # $ % & ' * + - . ^ _ | ~ A - z 0 - 9 ] + $ / . test ( type ) ) {
226+ if ( type . length === 0 || ! HTTP_TOKEN_CODEPOINTS . test ( type ) ) {
221227 return 'failure'
222228 }
223229
@@ -244,7 +250,7 @@ function parseMIMEType (input) {
244250
245251 // 9. If subtype is the empty string or does not solely
246252 // contain HTTP token code points, then return failure.
247- if ( subtype . length === 0 || ! / ^ [ ! # $ % & ' * + - . ^ _ | ~ A - z 0 - 9 ] + $ / . test ( subtype ) ) {
253+ if ( subtype . length === 0 || ! HTTP_TOKEN_CODEPOINTS . test ( subtype ) ) {
248254 return 'failure'
249255 }
250256
@@ -258,9 +264,7 @@ function parseMIMEType (input) {
258264 /** @type {Map<string, string> } */
259265 parameters : new Map ( ) ,
260266 // https://mimesniff.spec.whatwg.org/#mime-type-essence
261- get essence ( ) {
262- return `${ this . type } /${ this . subtype } `
263- }
267+ essence : `${ type } /${ subtype } `
264268 }
265269
266270 // 11. While position is not past the end of input:
@@ -272,7 +276,7 @@ function parseMIMEType (input) {
272276 // whitespace from input given position.
273277 collectASequenceOfCodePoints (
274278 // https://fetch.spec.whatwg.org/#http-whitespace
275- ( char ) => / ( \u000A | \u000D | \u0009 | \u0020 ) / . test ( char ) , // eslint-disable-line
279+ char => HTTP_WHITESPACE_REGEX . test ( char ) ,
276280 input ,
277281 position
278282 )
@@ -355,9 +359,8 @@ function parseMIMEType (input) {
355359 // then set mimeType’s parameters[parameterName] to parameterValue.
356360 if (
357361 parameterName . length !== 0 &&
358- / ^ [ ! # $ % & ' * + - . ^ _ | ~ A - z 0 - 9 ] + $ / . test ( parameterName ) &&
359- // https://mimesniff.spec.whatwg.org/#http-quoted-string-token-code-point
360- ! / ^ ( \u0009 | \x { 0020 } - \x { 007 E} | \x { 0080 } - \x { 00 FF} ) + $ / . test ( parameterValue ) && // eslint-disable-line
362+ HTTP_TOKEN_CODEPOINTS . test ( parameterName ) &&
363+ ! HTTP_QUOTED_STRING_TOKENS . test ( parameterValue ) &&
361364 ! mimeType . parameters . has ( parameterName )
362365 ) {
363366 mimeType . parameters . set ( parameterName , parameterValue )
0 commit comments