source: trip-planner-front/node_modules/css-tree/lib/lexer/generic-an-plus-b.js@ bdd6491

Last change on this file since bdd6491 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.6 KB
Line 
1var isDigit = require('../tokenizer').isDigit;
2var cmpChar = require('../tokenizer').cmpChar;
3var TYPE = require('../tokenizer').TYPE;
4
5var DELIM = TYPE.Delim;
6var WHITESPACE = TYPE.WhiteSpace;
7var COMMENT = TYPE.Comment;
8var IDENT = TYPE.Ident;
9var NUMBER = TYPE.Number;
10var DIMENSION = TYPE.Dimension;
11var PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
12var HYPHENMINUS = 0x002D; // U+002D HYPHEN-MINUS (-)
13var N = 0x006E; // U+006E LATIN SMALL LETTER N (n)
14var DISALLOW_SIGN = true;
15var ALLOW_SIGN = false;
16
17function isDelim(token, code) {
18 return token !== null && token.type === DELIM && token.value.charCodeAt(0) === code;
19}
20
21function skipSC(token, offset, getNextToken) {
22 while (token !== null && (token.type === WHITESPACE || token.type === COMMENT)) {
23 token = getNextToken(++offset);
24 }
25
26 return offset;
27}
28
29function checkInteger(token, valueOffset, disallowSign, offset) {
30 if (!token) {
31 return 0;
32 }
33
34 var code = token.value.charCodeAt(valueOffset);
35
36 if (code === PLUSSIGN || code === HYPHENMINUS) {
37 if (disallowSign) {
38 // Number sign is not allowed
39 return 0;
40 }
41 valueOffset++;
42 }
43
44 for (; valueOffset < token.value.length; valueOffset++) {
45 if (!isDigit(token.value.charCodeAt(valueOffset))) {
46 // Integer is expected
47 return 0;
48 }
49 }
50
51 return offset + 1;
52}
53
54// ... <signed-integer>
55// ... ['+' | '-'] <signless-integer>
56function consumeB(token, offset_, getNextToken) {
57 var sign = false;
58 var offset = skipSC(token, offset_, getNextToken);
59
60 token = getNextToken(offset);
61
62 if (token === null) {
63 return offset_;
64 }
65
66 if (token.type !== NUMBER) {
67 if (isDelim(token, PLUSSIGN) || isDelim(token, HYPHENMINUS)) {
68 sign = true;
69 offset = skipSC(getNextToken(++offset), offset, getNextToken);
70 token = getNextToken(offset);
71
72 if (token === null && token.type !== NUMBER) {
73 return 0;
74 }
75 } else {
76 return offset_;
77 }
78 }
79
80 if (!sign) {
81 var code = token.value.charCodeAt(0);
82 if (code !== PLUSSIGN && code !== HYPHENMINUS) {
83 // Number sign is expected
84 return 0;
85 }
86 }
87
88 return checkInteger(token, sign ? 0 : 1, sign, offset);
89}
90
91// An+B microsyntax https://www.w3.org/TR/css-syntax-3/#anb
92module.exports = function anPlusB(token, getNextToken) {
93 /* eslint-disable brace-style*/
94 var offset = 0;
95
96 if (!token) {
97 return 0;
98 }
99
100 // <integer>
101 if (token.type === NUMBER) {
102 return checkInteger(token, 0, ALLOW_SIGN, offset); // b
103 }
104
105 // -n
106 // -n <signed-integer>
107 // -n ['+' | '-'] <signless-integer>
108 // -n- <signless-integer>
109 // <dashndashdigit-ident>
110 else if (token.type === IDENT && token.value.charCodeAt(0) === HYPHENMINUS) {
111 // expect 1st char is N
112 if (!cmpChar(token.value, 1, N)) {
113 return 0;
114 }
115
116 switch (token.value.length) {
117 // -n
118 // -n <signed-integer>
119 // -n ['+' | '-'] <signless-integer>
120 case 2:
121 return consumeB(getNextToken(++offset), offset, getNextToken);
122
123 // -n- <signless-integer>
124 case 3:
125 if (token.value.charCodeAt(2) !== HYPHENMINUS) {
126 return 0;
127 }
128
129 offset = skipSC(getNextToken(++offset), offset, getNextToken);
130 token = getNextToken(offset);
131
132 return checkInteger(token, 0, DISALLOW_SIGN, offset);
133
134 // <dashndashdigit-ident>
135 default:
136 if (token.value.charCodeAt(2) !== HYPHENMINUS) {
137 return 0;
138 }
139
140 return checkInteger(token, 3, DISALLOW_SIGN, offset);
141 }
142 }
143
144 // '+'? n
145 // '+'? n <signed-integer>
146 // '+'? n ['+' | '-'] <signless-integer>
147 // '+'? n- <signless-integer>
148 // '+'? <ndashdigit-ident>
149 else if (token.type === IDENT || (isDelim(token, PLUSSIGN) && getNextToken(offset + 1).type === IDENT)) {
150 // just ignore a plus
151 if (token.type !== IDENT) {
152 token = getNextToken(++offset);
153 }
154
155 if (token === null || !cmpChar(token.value, 0, N)) {
156 return 0;
157 }
158
159 switch (token.value.length) {
160 // '+'? n
161 // '+'? n <signed-integer>
162 // '+'? n ['+' | '-'] <signless-integer>
163 case 1:
164 return consumeB(getNextToken(++offset), offset, getNextToken);
165
166 // '+'? n- <signless-integer>
167 case 2:
168 if (token.value.charCodeAt(1) !== HYPHENMINUS) {
169 return 0;
170 }
171
172 offset = skipSC(getNextToken(++offset), offset, getNextToken);
173 token = getNextToken(offset);
174
175 return checkInteger(token, 0, DISALLOW_SIGN, offset);
176
177 // '+'? <ndashdigit-ident>
178 default:
179 if (token.value.charCodeAt(1) !== HYPHENMINUS) {
180 return 0;
181 }
182
183 return checkInteger(token, 2, DISALLOW_SIGN, offset);
184 }
185 }
186
187 // <ndashdigit-dimension>
188 // <ndash-dimension> <signless-integer>
189 // <n-dimension>
190 // <n-dimension> <signed-integer>
191 // <n-dimension> ['+' | '-'] <signless-integer>
192 else if (token.type === DIMENSION) {
193 var code = token.value.charCodeAt(0);
194 var sign = code === PLUSSIGN || code === HYPHENMINUS ? 1 : 0;
195
196 for (var i = sign; i < token.value.length; i++) {
197 if (!isDigit(token.value.charCodeAt(i))) {
198 break;
199 }
200 }
201
202 if (i === sign) {
203 // Integer is expected
204 return 0;
205 }
206
207 if (!cmpChar(token.value, i, N)) {
208 return 0;
209 }
210
211 // <n-dimension>
212 // <n-dimension> <signed-integer>
213 // <n-dimension> ['+' | '-'] <signless-integer>
214 if (i + 1 === token.value.length) {
215 return consumeB(getNextToken(++offset), offset, getNextToken);
216 } else {
217 if (token.value.charCodeAt(i + 1) !== HYPHENMINUS) {
218 return 0;
219 }
220
221 // <ndash-dimension> <signless-integer>
222 if (i + 2 === token.value.length) {
223 offset = skipSC(getNextToken(++offset), offset, getNextToken);
224 token = getNextToken(offset);
225
226 return checkInteger(token, 0, DISALLOW_SIGN, offset);
227 }
228 // <ndashdigit-dimension>
229 else {
230 return checkInteger(token, i + 2, DISALLOW_SIGN, offset);
231 }
232 }
233 }
234
235 return 0;
236};
Note: See TracBrowser for help on using the repository browser.