-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
80 lines (69 loc) · 2.59 KB
/
parse.js
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
/** @license MIT License (c) copyright 2014 original authors */
/** @author Brian Cavalier */
/** @author John Hann */
exports.find = findUrls;
exports.toUrl = toUrl;
// Note: this could fail if there are quotes inside the url
// TODO: capture media queries in @imports
var findUrlsRx = /@import\s+(?:(?:url\s*\()(['"]?)(.*?)\1\)|(['"]?)(.*?)\3)[^;]*?;|url\s*\((['"]?)(.*?)\5\)|("')|(\/\*)|(\*\/)/g;
/**
* Finds css-style urls or @imports in a string and replaces them with the
* result of a translator function. Urls must be wrapped in `url()`, `url('')`
* or `url("")`. Urls as plain strings (non-standard) can not be reliably
* detected, so they are not translated.
* @param {string} cssText
* @param {function(url:string, complete:string, q:string)} onUrl - event for
* urls. The url is provided in the url param. The complete property,
* including the url() wrapper, is provided in the complete param. The
* quote used in the url is provided by the q param.
* @param {function(url:string, complete:string, q:string)} onImport - event
* for @imports. The import url is provided by the url param. The full text
* of the import is provided by the complete param. The quote used
* in the url is provided by the q param.
* @param {function(text:string)} onOther - event for other css text;
* @returns {string} original cssText, unchanged.
*/
function findUrls (cssText, onUrl, onImport, onOther) {
var pos, m, comment, quote;
if (!onUrl) onUrl = bitBucket;
if (!onImport) onImport = bitBucket;
if (!onOther) onOther = bitBucket;
pos = 0;
while (m = findUrlsRx.exec(cssText)) {
processMatch(m[0], m.index, m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9]);
// update our current position
pos = m.index + m[0].length;
}
if (cssText.length > pos) onOther(cssText.slice(pos));
return cssText;
function processMatch (match, index, iq1, iUrl1, iq2, iUrl2, uq, url, q, sc, ec) {
// check for quotes and comments first
if (comment) {
if (ec === comment) comment = false;
}
else if (quote) {
if (q === quote) quote = false;
}
else if (q) {
quote = q;
}
else if (sc) {
// yes, line comments aren't valid css (yet!)
comment = sc === '//' ? '\n' : '*/';
}
// check for import rules
else if (iUrl1 || iUrl2) {
if (index > pos) onOther(cssText.slice(pos, index));
onImport(iUrl1 || iUrl2, match, iq1 || iq2 || '');
}
// check for url property values
else if (url) {
if (index > pos) onOther(cssText.slice(pos, index));
onUrl(url, match, uq || '');
}
}
}
function toUrl (address) {
return 'url("' + address + '")';
}
function bitBucket () {}