Url.js
2 KB
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
var isWhiteSpace = require('../../tokenizer').isWhiteSpace;
var cmpStr = require('../../tokenizer').cmpStr;
var TYPE = require('../../tokenizer').TYPE;
var FUNCTION = TYPE.Function;
var URL = TYPE.Url;
var RIGHTPARENTHESIS = TYPE.RightParenthesis;
// <url-token> | <function-token> <string> )
module.exports = {
name: 'Url',
structure: {
value: ['String', 'Raw']
},
parse: function() {
var start = this.scanner.tokenStart;
var value;
switch (this.scanner.tokenType) {
case URL:
var rawStart = start + 4;
var rawEnd = this.scanner.tokenEnd - 1;
while (rawStart < rawEnd && isWhiteSpace(this.scanner.source.charCodeAt(rawStart))) {
rawStart++;
}
while (rawStart < rawEnd && isWhiteSpace(this.scanner.source.charCodeAt(rawEnd - 1))) {
rawEnd--;
}
value = {
type: 'Raw',
loc: this.getLocation(rawStart, rawEnd),
value: this.scanner.source.substring(rawStart, rawEnd)
};
this.eat(URL);
break;
case FUNCTION:
if (!cmpStr(this.scanner.source, this.scanner.tokenStart, this.scanner.tokenEnd, 'url(')) {
this.error('Function name must be `url`');
}
this.eat(FUNCTION);
this.scanner.skipSC();
value = this.String();
this.scanner.skipSC();
this.eat(RIGHTPARENTHESIS);
break;
default:
this.error('Url or Function is expected');
}
return {
type: 'Url',
loc: this.getLocation(start, this.scanner.tokenStart),
value: value
};
},
generate: function(node) {
this.chunk('url');
this.chunk('(');
this.node(node.value);
this.chunk(')');
}
};