aboutsummaryrefslogtreecommitdiff
path: root/point/libs/require-css/normalize.js
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2014-08-10 17:28:37 +0200
committerPacien TRAN-GIRARD2014-08-10 17:28:37 +0200
commite7bf5952d0729b37e677168b6e8fbd1ce58ed1a2 (patch)
tree189988e3e272b806262d1df6b87f1da089ef4af8 /point/libs/require-css/normalize.js
parenta32e898c8d7ad3774f5654e88bb24d5c26482137 (diff)
downloadwhatsthepoint-master.tar.gz
First versionHEADmaster
Diffstat (limited to 'point/libs/require-css/normalize.js')
-rw-r--r--point/libs/require-css/normalize.js142
1 files changed, 142 insertions, 0 deletions
diff --git a/point/libs/require-css/normalize.js b/point/libs/require-css/normalize.js
new file mode 100644
index 0000000..ee82b2c
--- /dev/null
+++ b/point/libs/require-css/normalize.js
@@ -0,0 +1,142 @@
1//>>excludeStart('excludeRequireCss', pragmas.excludeRequireCss)
2/*
3 * css.normalize.js
4 *
5 * CSS Normalization
6 *
7 * CSS paths are normalized based on an optional basePath and the RequireJS config
8 *
9 * Usage:
10 * normalize(css, fromBasePath, toBasePath);
11 *
12 * css: the stylesheet content to normalize
13 * fromBasePath: the absolute base path of the css relative to any root (but without ../ backtracking)
14 * toBasePath: the absolute new base path of the css relative to the same root
15 *
16 * Absolute dependencies are left untouched.
17 *
18 * Urls in the CSS are picked up by regular expressions.
19 * These will catch all statements of the form:
20 *
21 * url(*)
22 * url('*')
23 * url("*")
24 *
25 * @import '*'
26 * @import "*"
27 *
28 * (and so also @import url(*) variations)
29 *
30 * For urls needing normalization
31 *
32 */
33
34define(function () {
35
36 // regular expression for removing double slashes
37 // eg http://www.example.com//my///url/here -> http://www.example.com/my/url/here
38 var slashes = /([^:])\/+/g
39 var removeDoubleSlashes = function (uri) {
40 return uri.replace(slashes, '$1/');
41 }
42
43 // given a relative URI, and two absolute base URIs, convert it from one base to another
44 var protocolRegEx = /[^\:\/]*:\/\/([^\/])*/;
45 var absUrlRegEx = /^(\/|data:)/;
46
47 function convertURIBase(uri, fromBase, toBase) {
48 if (uri.match(absUrlRegEx) || uri.match(protocolRegEx))
49 return uri;
50 uri = removeDoubleSlashes(uri);
51 // if toBase specifies a protocol path, ensure this is the same protocol as fromBase, if not
52 // use absolute path at fromBase
53 var toBaseProtocol = toBase.match(protocolRegEx);
54 var fromBaseProtocol = fromBase.match(protocolRegEx);
55 if (fromBaseProtocol && (!toBaseProtocol || toBaseProtocol[1] != fromBaseProtocol[1] || toBaseProtocol[2] != fromBaseProtocol[2]))
56 return absoluteURI(uri, fromBase);
57
58 else {
59 return relativeURI(absoluteURI(uri, fromBase), toBase);
60 }
61 };
62
63 // given a relative URI, calculate the absolute URI
64 function absoluteURI(uri, base) {
65 if (uri.substr(0, 2) == './')
66 uri = uri.substr(2);
67
68 // absolute urls are left in tact
69 if (uri.match(absUrlRegEx) || uri.match(protocolRegEx))
70 return uri;
71
72 var baseParts = base.split('/');
73 var uriParts = uri.split('/');
74
75 baseParts.pop();
76
77 while (curPart = uriParts.shift())
78 if (curPart == '..')
79 baseParts.pop();
80 else
81 baseParts.push(curPart);
82
83 return baseParts.join('/');
84 };
85
86
87 // given an absolute URI, calculate the relative URI
88 function relativeURI(uri, base) {
89
90 // reduce base and uri strings to just their difference string
91 var baseParts = base.split('/');
92 baseParts.pop();
93 base = baseParts.join('/') + '/';
94 i = 0;
95 while (base.substr(i, 1) == uri.substr(i, 1))
96 i++;
97 while (base.substr(i, 1) != '/')
98 i--;
99 base = base.substr(i + 1);
100 uri = uri.substr(i + 1);
101
102 // each base folder difference is thus a backtrack
103 baseParts = base.split('/');
104 var uriParts = uri.split('/');
105 out = '';
106 while (baseParts.shift())
107 out += '../';
108
109 // finally add uri parts
110 while (curPart = uriParts.shift())
111 out += curPart + '/';
112
113 return out.substr(0, out.length - 1);
114 };
115
116 var normalizeCSS = function (source, fromBase, toBase) {
117
118 fromBase = removeDoubleSlashes(fromBase);
119 toBase = removeDoubleSlashes(toBase);
120
121 var urlRegEx = /@import\s*("([^"]*)"|'([^']*)')|url\s*\(\s*(\s*"([^"]*)"|'([^']*)'|[^\)]*\s*)\s*\)/ig;
122 var result, url, source;
123
124 while (result = urlRegEx.exec(source)) {
125 url = result[3] || result[2] || result[5] || result[6] || result[4];
126 var newUrl;
127 newUrl = convertURIBase(url, fromBase, toBase);
128 var quoteLen = result[5] || result[6] ? 1 : 0;
129 source = source.substr(0, urlRegEx.lastIndex - url.length - quoteLen - 1) + newUrl + source.substr(urlRegEx.lastIndex - quoteLen - 1);
130 urlRegEx.lastIndex = urlRegEx.lastIndex + (newUrl.length - url.length);
131 }
132
133 return source;
134 };
135
136 normalizeCSS.convertURIBase = convertURIBase;
137 normalizeCSS.absoluteURI = absoluteURI;
138 normalizeCSS.relativeURI = relativeURI;
139
140 return normalizeCSS;
141});
142//>>excludeEnd('excludeRequireCss')