source: imaps-frontend/node_modules/clean-css/lib/reader/rewrite-url.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.7 KB
Line 
1var path = require('path');
2var url = require('url');
3
4var isDataUriResource = require('../utils/is-data-uri-resource');
5
6var DOUBLE_QUOTE = '"';
7var SINGLE_QUOTE = '\'';
8var URL_PREFIX = 'url(';
9var URL_SUFFIX = ')';
10
11var PROTOCOL_LESS_PREFIX_PATTERN = /^[^\w\d]*\/\//;
12var QUOTE_PREFIX_PATTERN = /^["']/;
13var QUOTE_SUFFIX_PATTERN = /["']$/;
14var ROUND_BRACKETS_PATTERN = /[()]/;
15var URL_PREFIX_PATTERN = /^url\(/i;
16var URL_SUFFIX_PATTERN = /\)$/;
17var WHITESPACE_PATTERN = /\s/;
18
19var isWindows = process.platform == 'win32';
20
21function rebase(uri, rebaseConfig) {
22 if (!rebaseConfig) {
23 return uri;
24 }
25
26 if (isAbsolute(uri) && !isRemote(rebaseConfig.toBase)) {
27 return uri;
28 }
29
30 if (isRemote(uri) || isSVGMarker(uri) || isInternal(uri) || isDataUriResource(uri)) {
31 return uri;
32 }
33
34 if (isRemote(rebaseConfig.toBase)) {
35 return url.resolve(rebaseConfig.toBase, uri);
36 }
37
38 return rebaseConfig.absolute
39 ? normalize(absolute(uri, rebaseConfig))
40 : normalize(relative(uri, rebaseConfig));
41}
42
43function isAbsolute(uri) {
44 return path.isAbsolute(uri);
45}
46
47function isSVGMarker(uri) {
48 return uri[0] == '#';
49}
50
51function isInternal(uri) {
52 return /^\w+:\w+/.test(uri);
53}
54
55function isRemote(uri) {
56 return /^[^:]+?:\/\//.test(uri) || PROTOCOL_LESS_PREFIX_PATTERN.test(uri);
57}
58
59function absolute(uri, rebaseConfig) {
60 return path
61 .resolve(path.join(rebaseConfig.fromBase || '', uri))
62 .replace(rebaseConfig.toBase, '');
63}
64
65function relative(uri, rebaseConfig) {
66 return path.relative(rebaseConfig.toBase, path.join(rebaseConfig.fromBase || '', uri));
67}
68
69function normalize(uri) {
70 return isWindows ? uri.replace(/\\/g, '/') : uri;
71}
72
73function quoteFor(unquotedUrl) {
74 if (unquotedUrl.indexOf(SINGLE_QUOTE) > -1) {
75 return DOUBLE_QUOTE;
76 } if (unquotedUrl.indexOf(DOUBLE_QUOTE) > -1) {
77 return SINGLE_QUOTE;
78 } if (hasWhitespace(unquotedUrl) || hasRoundBrackets(unquotedUrl)) {
79 return SINGLE_QUOTE;
80 }
81 return '';
82}
83
84function hasWhitespace(url) {
85 return WHITESPACE_PATTERN.test(url);
86}
87
88function hasRoundBrackets(url) {
89 return ROUND_BRACKETS_PATTERN.test(url);
90}
91
92function rewriteUrl(originalUrl, rebaseConfig, pathOnly) {
93 var strippedUrl = originalUrl
94 .replace(URL_PREFIX_PATTERN, '')
95 .replace(URL_SUFFIX_PATTERN, '')
96 .trim();
97
98 var unquotedUrl = strippedUrl
99 .replace(QUOTE_PREFIX_PATTERN, '')
100 .replace(QUOTE_SUFFIX_PATTERN, '')
101 .trim();
102
103 var quote = strippedUrl[0] == SINGLE_QUOTE || strippedUrl[0] == DOUBLE_QUOTE
104 ? strippedUrl[0]
105 : quoteFor(unquotedUrl);
106
107 return pathOnly
108 ? rebase(unquotedUrl, rebaseConfig)
109 : URL_PREFIX + quote + rebase(unquotedUrl, rebaseConfig) + quote + URL_SUFFIX;
110}
111
112module.exports = rewriteUrl;
Note: See TracBrowser for help on using the repository browser.