index.js
3.93 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict';
const path = require('path');
const valueParser = require('postcss-value-parser');
const normalize = require('normalize-url');
const multiline = /\\[\r\n]/;
// eslint-disable-next-line no-useless-escape
const escapeChars = /([\s\(\)"'])/g;
// Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
// Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/;
// Windows paths like `c:\`
const WINDOWS_PATH_REGEX = /^[a-zA-Z]:\\/;
/**
* Originally in sindresorhus/is-absolute-url
*
* @param {string} url
*/
function isAbsolute(url) {
if (WINDOWS_PATH_REGEX.test(url)) {
return false;
}
return ABSOLUTE_URL_REGEX.test(url);
}
/**
* @param {string} url
* @param {normalize.Options} options
* @return {string}
*/
function convert(url, options) {
if (isAbsolute(url) || url.startsWith('//')) {
let normalizedURL;
try {
normalizedURL = normalize(url, options);
} catch (e) {
normalizedURL = url;
}
return normalizedURL;
}
// `path.normalize` always returns backslashes on Windows, need replace in `/`
return path.normalize(url).replace(new RegExp('\\' + path.sep, 'g'), '/');
}
/**
* @param {import('postcss').AtRule} rule
* @return {void}
*/
function transformNamespace(rule) {
rule.params = valueParser(rule.params)
.walk((node) => {
if (
node.type === 'function' &&
node.value.toLowerCase() === 'url' &&
node.nodes.length
) {
/** @type {valueParser.Node} */ (node).type = 'string';
/** @type {any} */ (node).quote =
node.nodes[0].type === 'string' ? node.nodes[0].quote : '"';
node.value = node.nodes[0].value;
}
if (node.type === 'string') {
node.value = node.value.trim();
}
return false;
})
.toString();
}
/**
* @param {import('postcss').Declaration} decl
* @param {normalize.Options} opts
* @return {void}
*/
function transformDecl(decl, opts) {
decl.value = valueParser(decl.value)
.walk((node) => {
if (node.type !== 'function' || node.value.toLowerCase() !== 'url') {
return false;
}
node.before = node.after = '';
if (!node.nodes.length) {
return false;
}
let url = node.nodes[0];
let escaped;
url.value = url.value.trim().replace(multiline, '');
// Skip empty URLs
// Empty URL function equals request to current stylesheet where it is declared
if (url.value.length === 0) {
/** @type {any} */ (url).quote = '';
return false;
}
if (/^data:(.*)?,/i.test(url.value)) {
return false;
}
if (!/^.+-extension:\//i.test(url.value)) {
url.value = convert(url.value, opts);
}
if (escapeChars.test(url.value) && url.type === 'string') {
escaped = url.value.replace(escapeChars, '\\$1');
if (escaped.length < url.value.length + 2) {
url.value = escaped;
/** @type {valueParser.Node} */ (url).type = 'word';
}
} else {
url.type = 'word';
}
return false;
})
.toString();
}
/** @typedef {normalize.Options} Options */
/**
* @type {import('postcss').PluginCreator<Options>}
* @param {Options} opts
* @return {import('postcss').Plugin}
*/
function pluginCreator(opts) {
opts = Object.assign(
{},
{
normalizeProtocol: false,
sortQueryParameters: false,
stripHash: false,
stripWWW: false,
stripTextFragment: false,
},
opts
);
return {
postcssPlugin: 'postcss-normalize-url',
OnceExit(css) {
css.walk((node) => {
if (node.type === 'decl') {
return transformDecl(node, opts);
} else if (
node.type === 'atrule' &&
node.name.toLowerCase() === 'namespace'
) {
return transformNamespace(node);
}
});
},
};
}
pluginCreator.postcss = true;
module.exports = pluginCreator;