normalize-url 4.4.0 → 4.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +11 -11
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -6,8 +6,8 @@ const testParameter = (name, filters) => {
6
6
  return filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);
7
7
  };
8
8
 
9
- const normalizeDataURL = urlString => {
10
- const parts = urlString.trim().match(/^data:(.*?),(.*)$/);
9
+ const normalizeDataURL = (urlString, {stripHash}) => {
10
+ const parts = urlString.match(/^data:(.*?),(.*?)(?:#(.*))?$/);
11
11
 
12
12
  if (!parts) {
13
13
  throw new Error(`Invalid URL: ${urlString}`);
@@ -15,6 +15,7 @@ const normalizeDataURL = urlString => {
15
15
 
16
16
  const mediaType = parts[1].split(';');
17
17
  const body = parts[2];
18
+ const hash = stripHash ? '' : parts[3];
18
19
 
19
20
  let base64 = false;
20
21
 
@@ -35,7 +36,7 @@ const normalizeDataURL = urlString => {
35
36
  value = value.toLowerCase();
36
37
  }
37
38
 
38
- return `${key}=${value}`;
39
+ return `${key}${value ? `=${value}` : ''}`;
39
40
  });
40
41
 
41
42
  const normalizedMediaType = [
@@ -50,7 +51,7 @@ const normalizeDataURL = urlString => {
50
51
  normalizedMediaType.unshift(mimeType);
51
52
  }
52
53
 
53
- return `data:${normalizedMediaType.join(';')},${base64 ? body.trim() : body}`;
54
+ return `data:${normalizedMediaType.join(';')},${base64 ? body.trim() : body}${hash ? `#${hash}` : ''}`;
54
55
  };
55
56
 
56
57
  const normalizeUrl = (urlString, options) => {
@@ -84,11 +85,16 @@ const normalizeUrl = (urlString, options) => {
84
85
 
85
86
  urlString = urlString.trim();
86
87
 
88
+ // Data URL
89
+ if (/^data:/i.test(urlString)) {
90
+ return normalizeDataURL(urlString, options);
91
+ }
92
+
87
93
  const hasRelativeProtocol = urlString.startsWith('//');
88
94
  const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
89
95
 
90
96
  // Prepend protocol
91
- if (!isRelativeUrl && !/^data:/i.test(urlString)) {
97
+ if (!isRelativeUrl) {
92
98
  urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, options.defaultProtocol);
93
99
  }
94
100
 
@@ -177,12 +183,6 @@ const normalizeUrl = (urlString, options) => {
177
183
  urlObj.searchParams.sort();
178
184
  }
179
185
 
180
- // Data URL
181
- if (urlObj.protocol === 'data:') {
182
- const url = normalizeDataURL(`${urlObj.protocol}${urlObj.pathname}`);
183
- return `${url}${urlObj.search}${urlObj.hash}`;
184
- }
185
-
186
186
  if (options.removeTrailingSlash) {
187
187
  urlObj.pathname = urlObj.pathname.replace(/\/$/, '');
188
188
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "normalize-url",
3
- "version": "4.4.0",
3
+ "version": "4.4.1",
4
4
  "description": "Normalize a URL",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/normalize-url",