source: trip-planner-front/node_modules/postcss-normalize-url/dist/index.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.2 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _path = _interopRequireDefault(require("path"));
9
10var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
11
12var _normalizeUrl = _interopRequireDefault(require("normalize-url"));
13
14var _isAbsoluteUrl = _interopRequireDefault(require("is-absolute-url"));
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18const multiline = /\\[\r\n]/; // eslint-disable-next-line no-useless-escape
19
20const escapeChars = /([\s\(\)"'])/g;
21
22function convert(url, options) {
23 if ((0, _isAbsoluteUrl.default)(url) || url.startsWith('//')) {
24 let normalizedURL = null;
25
26 try {
27 normalizedURL = (0, _normalizeUrl.default)(url, options);
28 } catch (e) {
29 normalizedURL = url;
30 }
31
32 return normalizedURL;
33 } // `path.normalize` always returns backslashes on Windows, need replace in `/`
34
35
36 return _path.default.normalize(url).replace(new RegExp('\\' + _path.default.sep, 'g'), '/');
37}
38
39function transformNamespace(rule) {
40 rule.params = (0, _postcssValueParser.default)(rule.params).walk(node => {
41 if (node.type === 'function' && node.value.toLowerCase() === 'url' && node.nodes.length) {
42 node.type = 'string';
43 node.quote = node.nodes[0].quote || '"';
44 node.value = node.nodes[0].value;
45 }
46
47 if (node.type === 'string') {
48 node.value = node.value.trim();
49 }
50
51 return false;
52 }).toString();
53}
54
55function transformDecl(decl, opts) {
56 decl.value = (0, _postcssValueParser.default)(decl.value).walk(node => {
57 if (node.type !== 'function' || node.value.toLowerCase() !== 'url') {
58 return false;
59 }
60
61 node.before = node.after = '';
62
63 if (!node.nodes.length) {
64 return false;
65 }
66
67 let url = node.nodes[0];
68 let escaped;
69 url.value = url.value.trim().replace(multiline, ''); // Skip empty URLs
70 // Empty URL function equals request to current stylesheet where it is declared
71
72 if (url.value.length === 0) {
73 url.quote = '';
74 return false;
75 }
76
77 if (/^data:(.*)?,/i.test(url.value)) {
78 return false;
79 }
80
81 if (!/^.+-extension:\//i.test(url.value)) {
82 url.value = convert(url.value, opts);
83 }
84
85 if (escapeChars.test(url.value) && url.type === 'string') {
86 escaped = url.value.replace(escapeChars, '\\$1');
87
88 if (escaped.length < url.value.length + 2) {
89 url.value = escaped;
90 url.type = 'word';
91 }
92 } else {
93 url.type = 'word';
94 }
95
96 return false;
97 }).toString();
98}
99
100function pluginCreator(opts) {
101 opts = Object.assign({}, {
102 normalizeProtocol: false,
103 stripHash: false,
104 stripWWW: false,
105 stripTextFragment: false
106 }, opts);
107 return {
108 postcssPlugin: 'postcss-normalize-url',
109
110 OnceExit(css) {
111 css.walk(node => {
112 if (node.type === 'decl') {
113 return transformDecl(node, opts);
114 } else if (node.type === 'atrule' && node.name.toLowerCase() === 'namespace') {
115 return transformNamespace(node);
116 }
117 });
118 }
119
120 };
121}
122
123pluginCreator.postcss = true;
124var _default = pluginCreator;
125exports.default = _default;
126module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.