MediaFeature.js
1.9 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
70
71
72
73
74
75
76
var TYPE = require('../../tokenizer').TYPE;
var IDENT = TYPE.Ident;
var NUMBER = TYPE.Number;
var DIMENSION = TYPE.Dimension;
var LEFTPARENTHESIS = TYPE.LeftParenthesis;
var RIGHTPARENTHESIS = TYPE.RightParenthesis;
var COLON = TYPE.Colon;
var DELIM = TYPE.Delim;
module.exports = {
name: 'MediaFeature',
structure: {
name: String,
value: ['Identifier', 'Number', 'Dimension', 'Ratio', null]
},
parse: function() {
var start = this.scanner.tokenStart;
var name;
var value = null;
this.eat(LEFTPARENTHESIS);
this.scanner.skipSC();
name = this.consume(IDENT);
this.scanner.skipSC();
if (this.scanner.tokenType !== RIGHTPARENTHESIS) {
this.eat(COLON);
this.scanner.skipSC();
switch (this.scanner.tokenType) {
case NUMBER:
if (this.lookupNonWSType(1) === DELIM) {
value = this.Ratio();
} else {
value = this.Number();
}
break;
case DIMENSION:
value = this.Dimension();
break;
case IDENT:
value = this.Identifier();
break;
default:
this.error('Number, dimension, ratio or identifier is expected');
}
this.scanner.skipSC();
}
this.eat(RIGHTPARENTHESIS);
return {
type: 'MediaFeature',
loc: this.getLocation(start, this.scanner.tokenStart),
name: name,
value: value
};
},
generate: function(node) {
this.chunk('(');
this.chunk(node.name);
if (node.value !== null) {
this.chunk(':');
this.node(node.value);
}
this.chunk(')');
}
};