summaryrefslogtreecommitdiff
path: root/node_modules/postcss-normalize-url/src/normalize.js
blob: e6597e0de286ce4f2ffd3b51d2dfb66616490f68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
/* Derived from normalize-url https://github.com/sindresorhus/normalize-url/main/index.js by Sindre Sorhus */

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
const DATA_URL_DEFAULT_MIME_TYPE = 'text/plain';
const DATA_URL_DEFAULT_CHARSET = 'us-ascii';

const supportedProtocols = new Set(['https:', 'http:', 'file:']);

/**
 * @param {string} urlString
 * @return {boolean} */
function hasCustomProtocol(urlString) {
  try {
    const { protocol } = new URL(urlString);
    return protocol.endsWith(':') && !supportedProtocols.has(protocol);
  } catch {
    return false;
  }
}

/**
 * @param {string} urlString
 * @return {string} */
function normalizeDataURL(urlString) {
  const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(
    urlString
  );

  if (!match) {
    throw new Error(`Invalid URL: ${urlString}`);
  }

  let { type, data, hash } =
    /** @type {{type: string, data: string, hash: string}} */ (match.groups);
  const mediaType = type.split(';');

  let isBase64 = false;
  if (mediaType[mediaType.length - 1] === 'base64') {
    mediaType.pop();
    isBase64 = true;
  }

  // Lowercase MIME type
  const mimeType = mediaType.shift()?.toLowerCase() ?? '';
  const attributes = mediaType
    .map(
      /** @type {(string: string) => string} */ (attribute) => {
        let [key, value = ''] = attribute
          .split('=')
          .map(
            /** @type {(string: string) => string} */ (string) => string.trim()
          );

        // Lowercase `charset`
        if (key === 'charset') {
          value = value.toLowerCase();

          if (value === DATA_URL_DEFAULT_CHARSET) {
            return '';
          }
        }

        return `${key}${value ? `=${value}` : ''}`;
      }
    )
    .filter(Boolean);

  const normalizedMediaType = [...attributes];

  if (isBase64) {
    normalizedMediaType.push('base64');
  }

  if (
    normalizedMediaType.length > 0 ||
    (mimeType && mimeType !== DATA_URL_DEFAULT_MIME_TYPE)
  ) {
    normalizedMediaType.unshift(mimeType);
  }

  return `data:${normalizedMediaType.join(';')},${
    isBase64 ? data.trim() : data
  }${hash ? `#${hash}` : ''}`;
}

/**
 * @param {string} urlString
 * @return {string}
 */
function normalizeUrl(urlString) {
  urlString = urlString.trim();

  // Data URL
  if (/^data:/i.test(urlString)) {
    return normalizeDataURL(urlString);
  }

  if (hasCustomProtocol(urlString)) {
    return urlString;
  }

  const hasRelativeProtocol = urlString.startsWith('//');
  const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);

  // Prepend protocol
  if (!isRelativeUrl) {
    urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, 'http:');
  }

  const urlObject = new URL(urlString);

  // Remove duplicate slashes if not preceded by a protocol
  if (urlObject.pathname) {
    urlObject.pathname = urlObject.pathname.replace(
      /(?<!\b[a-z][a-z\d+\-.]{1,50}:)\/{2,}/g,
      '/'
    );
  }

  // Decode URI octets
  if (urlObject.pathname) {
    try {
      urlObject.pathname = decodeURI(urlObject.pathname);
    } catch {
      /* Do nothing */
    }
  }

  if (urlObject.hostname) {
    // Remove trailing dot
    urlObject.hostname = urlObject.hostname.replace(/\.$/, '');
  }

  urlObject.pathname = urlObject.pathname.replace(/\/$/, '');

  // Take advantage of many of the Node `url` normalizations
  urlString = urlObject.toString();

  // Remove ending `/`
  if (urlObject.pathname === '/' && urlObject.hash === '') {
    urlString = urlString.replace(/\/$/, '');
  }

  // Restore relative protocol
  if (hasRelativeProtocol) {
    urlString = urlString.replace(/^http:\/\//, '//');
  }

  return urlString;
}

module.exports = normalizeUrl;