1 | var isDigit = require('../../tokenizer').isDigit;
|
---|
2 | var TYPE = require('../../tokenizer').TYPE;
|
---|
3 |
|
---|
4 | var NUMBER = TYPE.Number;
|
---|
5 | var DELIM = TYPE.Delim;
|
---|
6 | var SOLIDUS = 0x002F; // U+002F SOLIDUS (/)
|
---|
7 | var FULLSTOP = 0x002E; // U+002E FULL STOP (.)
|
---|
8 |
|
---|
9 | // Terms of <ratio> should be a positive numbers (not zero or negative)
|
---|
10 | // (see https://drafts.csswg.org/mediaqueries-3/#values)
|
---|
11 | // However, -o-min-device-pixel-ratio takes fractional values as a ratio's term
|
---|
12 | // and this is using by various sites. Therefore we relax checking on parse
|
---|
13 | // to test a term is unsigned number without an exponent part.
|
---|
14 | // Additional checking may be applied on lexer validation.
|
---|
15 | function consumeNumber() {
|
---|
16 | this.scanner.skipWS();
|
---|
17 |
|
---|
18 | var value = this.consume(NUMBER);
|
---|
19 |
|
---|
20 | for (var i = 0; i < value.length; i++) {
|
---|
21 | var code = value.charCodeAt(i);
|
---|
22 | if (!isDigit(code) && code !== FULLSTOP) {
|
---|
23 | this.error('Unsigned number is expected', this.scanner.tokenStart - value.length + i);
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | if (Number(value) === 0) {
|
---|
28 | this.error('Zero number is not allowed', this.scanner.tokenStart - value.length);
|
---|
29 | }
|
---|
30 |
|
---|
31 | return value;
|
---|
32 | }
|
---|
33 |
|
---|
34 | // <positive-integer> S* '/' S* <positive-integer>
|
---|
35 | module.exports = {
|
---|
36 | name: 'Ratio',
|
---|
37 | structure: {
|
---|
38 | left: String,
|
---|
39 | right: String
|
---|
40 | },
|
---|
41 | parse: function() {
|
---|
42 | var start = this.scanner.tokenStart;
|
---|
43 | var left = consumeNumber.call(this);
|
---|
44 | var right;
|
---|
45 |
|
---|
46 | this.scanner.skipWS();
|
---|
47 |
|
---|
48 | if (!this.scanner.isDelim(SOLIDUS)) {
|
---|
49 | this.error('Solidus is expected');
|
---|
50 | }
|
---|
51 | this.eat(DELIM);
|
---|
52 | right = consumeNumber.call(this);
|
---|
53 |
|
---|
54 | return {
|
---|
55 | type: 'Ratio',
|
---|
56 | loc: this.getLocation(start, this.scanner.tokenStart),
|
---|
57 | left: left,
|
---|
58 | right: right
|
---|
59 | };
|
---|
60 | },
|
---|
61 | generate: function(node) {
|
---|
62 | this.chunk(node.left);
|
---|
63 | this.chunk('/');
|
---|
64 | this.chunk(node.right);
|
---|
65 | }
|
---|
66 | };
|
---|