source: frontend/node_modules/postcss-normalize-url/src/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[9af201e]1'use strict';
2const path = require('path');
3const valueParser = require('postcss-value-parser');
4const normalize = require('normalize-url');
5
6const multiline = /\\[\r\n]/;
7// eslint-disable-next-line no-useless-escape
8const escapeChars = /([\s\(\)"'])/g;
9
10// Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
11// Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
12const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/;
13// Windows paths like `c:\`
14const WINDOWS_PATH_REGEX = /^[a-zA-Z]:\\/;
15
16/**
17 * Originally in sindresorhus/is-absolute-url
18 *
19 * @param {string} url
20 */
21function isAbsolute(url) {
22 if (WINDOWS_PATH_REGEX.test(url)) {
23 return false;
24 }
25 return ABSOLUTE_URL_REGEX.test(url);
26}
27
28/**
29 * @param {string} url
30 * @param {normalize.Options} options
31 * @return {string}
32 */
33function convert(url, options) {
34 if (isAbsolute(url) || url.startsWith('//')) {
35 let normalizedURL;
36
37 try {
38 normalizedURL = normalize(url, options);
39 } catch (e) {
40 normalizedURL = url;
41 }
42
43 return normalizedURL;
44 }
45
46 // `path.normalize` always returns backslashes on Windows, need replace in `/`
47 return path.normalize(url).replace(new RegExp('\\' + path.sep, 'g'), '/');
48}
49
50/**
51 * @param {import('postcss').AtRule} rule
52 * @return {void}
53 */
54function transformNamespace(rule) {
55 rule.params = valueParser(rule.params)
56 .walk((node) => {
57 if (
58 node.type === 'function' &&
59 node.value.toLowerCase() === 'url' &&
60 node.nodes.length
61 ) {
62 /** @type {valueParser.Node} */ (node).type = 'string';
63 /** @type {any} */ (node).quote =
64 node.nodes[0].type === 'string' ? node.nodes[0].quote : '"';
65 node.value = node.nodes[0].value;
66 }
67 if (node.type === 'string') {
68 node.value = node.value.trim();
69 }
70 return false;
71 })
72 .toString();
73}
74
75/**
76 * @param {import('postcss').Declaration} decl
77 * @param {normalize.Options} opts
78 * @return {void}
79 */
80function transformDecl(decl, opts) {
81 decl.value = valueParser(decl.value)
82 .walk((node) => {
83 if (node.type !== 'function' || node.value.toLowerCase() !== 'url') {
84 return false;
85 }
86
87 node.before = node.after = '';
88
89 if (!node.nodes.length) {
90 return false;
91 }
92 let url = node.nodes[0];
93 let escaped;
94
95 url.value = url.value.trim().replace(multiline, '');
96
97 // Skip empty URLs
98 // Empty URL function equals request to current stylesheet where it is declared
99 if (url.value.length === 0) {
100 /** @type {any} */ (url).quote = '';
101
102 return false;
103 }
104
105 if (/^data:(.*)?,/i.test(url.value)) {
106 return false;
107 }
108
109 if (!/^.+-extension:\//i.test(url.value)) {
110 url.value = convert(url.value, opts);
111 }
112
113 if (escapeChars.test(url.value) && url.type === 'string') {
114 escaped = url.value.replace(escapeChars, '\\$1');
115
116 if (escaped.length < url.value.length + 2) {
117 url.value = escaped;
118 /** @type {valueParser.Node} */ (url).type = 'word';
119 }
120 } else {
121 url.type = 'word';
122 }
123
124 return false;
125 })
126 .toString();
127}
128
129/** @typedef {normalize.Options} Options */
130/**
131 * @type {import('postcss').PluginCreator<Options>}
132 * @param {Options} opts
133 * @return {import('postcss').Plugin}
134 */
135function pluginCreator(opts) {
136 opts = Object.assign(
137 {},
138 {
139 normalizeProtocol: false,
140 sortQueryParameters: false,
141 stripHash: false,
142 stripWWW: false,
143 stripTextFragment: false,
144 },
145 opts
146 );
147
148 return {
149 postcssPlugin: 'postcss-normalize-url',
150
151 OnceExit(css) {
152 css.walk((node) => {
153 if (node.type === 'decl') {
154 return transformDecl(node, opts);
155 } else if (
156 node.type === 'atrule' &&
157 node.name.toLowerCase() === 'namespace'
158 ) {
159 return transformNamespace(node);
160 }
161 });
162 },
163 };
164}
165
166pluginCreator.postcss = true;
167module.exports = pluginCreator;
Note: See TracBrowser for help on using the repository browser.