Skip to content

Commit ce756cc

Browse files
committed
add new test cases #10
1 parent a59a29d commit ce756cc

File tree

24 files changed

+3451
-3429
lines changed

24 files changed

+3451
-3429
lines changed

dist/index-umd-web.js

Lines changed: 1578 additions & 1607 deletions
Large diffs are not rendered by default.

dist/index.cjs

Lines changed: 1578 additions & 1607 deletions
Large diffs are not rendered by default.

dist/index.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ interface CDOCommentToken {
119119
val: string;
120120
}
121121
interface BadCDOCommentToken {
122-
typ: 'BADCDO';
122+
typ: 'Bad-cdo';
123123
val: string;
124124
}
125125
interface IncludesToken {
@@ -642,6 +642,7 @@ declare function isResolution(dimension: DimensionToken): boolean;
642642
declare function isAngle(dimension: DimensionToken): boolean;
643643
declare function isTime(dimension: DimensionToken): boolean;
644644
declare function isFrequency(dimension: DimensionToken): boolean;
645+
declare function isColor(token: Token): boolean;
645646
declare function isIdentStart(codepoint: number): boolean;
646647
declare function isDigit(codepoint: number): boolean;
647648
declare function isIdentCodepoint(codepoint: number): boolean;
@@ -661,8 +662,10 @@ declare function isWhiteSpace(codepoint: number): boolean;
661662

662663
declare const getConfig: () => PropertiesConfig;
663664

665+
declare const funcList: string[];
664666
declare function matchType(val: Token, properties: PropertyMapType): boolean;
665667

668+
declare const colorsFunc: string[];
666669
declare function render(data: AstNode, opt?: RenderOptions): RenderResult;
667670
declare function renderToken(token: Token, options?: RenderOptions, reducer?: (acc: string, curr: Token) => string): string;
668671

@@ -695,4 +698,4 @@ declare function resolve(url: string, currentDirectory: string, cwd?: string): {
695698
declare function parse(iterator: string, opt?: ParserOptions): Promise<ParseResult>;
696699
declare function transform(css: string, options?: TransformOptions): Promise<TransformResult>;
697700

698-
export { combinators, dirname, getConfig, hasDeclaration, isAngle, isAtKeyword, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, load, matchType, matchUrl, minify, minifyRule, parse, parseDimension, parseString, reduceSelector, render, renderToken, resolve, tokenize, transform, urlTokenMatcher, walk };
701+
export { colorsFunc, combinators, dirname, funcList, getConfig, hasDeclaration, isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, load, matchType, matchUrl, minify, minifyRule, parse, parseDimension, parseString, reduceSelector, render, renderToken, resolve, tokenize, transform, urlTokenMatcher, walk };

dist/lib/parser/declaration/list.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PropertySet } from './set.js';
2+
import '../../renderer/utils/color.js';
23
import { getConfig } from '../utils/config.js';
34
import { PropertyMap } from './map.js';
45
import { parseString } from '../parse.js';

dist/lib/parser/declaration/map.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { eq } from '../utils/eq.js';
2+
import { renderToken } from '../../renderer/render.js';
3+
import '../../renderer/utils/color.js';
24
import { getConfig } from '../utils/config.js';
35
import { matchType } from '../utils/type.js';
4-
import { renderToken } from '../../renderer/render.js';
56
import { parseString } from '../parse.js';
67
import { PropertySet } from './set.js';
78

dist/lib/parser/parse.js

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isPseudo, isAtKeyword, isFunction, isNumber, isDimension, parseDimension, isPercentage, isIdent, isHexColor, isHash, isIdentStart } from './utils/syntax.js';
1+
import { isPseudo, isAtKeyword, isFunction, isNumber, isDimension, parseDimension, isPercentage, isIdent, isHexColor, isHash, isIdentStart, isColor } from './utils/syntax.js';
22
import { renderToken } from '../renderer/render.js';
33
import { COLORS_NAMES } from '../renderer/utils/color.js';
44
import { minify, combinators } from '../ast/minify.js';
@@ -328,6 +328,11 @@ async function parse(iterator, opt = {}) {
328328
if (item == null) {
329329
break;
330330
}
331+
// console.debug({item});
332+
if (item.hint != null && item.hint.startsWith('Bad-')) {
333+
// bad token
334+
continue;
335+
}
331336
tokens.push(item);
332337
bytesIn = item.bytesIn;
333338
if (item.token == ';' || item.token == '{') {
@@ -637,41 +642,33 @@ function parseTokens(tokens, options = {}) {
637642
// @ts-ignore
638643
t.chi.pop();
639644
}
640-
let isColor = true;
641645
// @ts-ignore
642-
if (options.parseColor && ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk'].includes(t.val)) {
646+
if (options.parseColor && t.typ == 'Func' && isColor(t)) {
647+
// if (isColor) {
643648
// @ts-ignore
644-
for (const v of t.chi) {
645-
if (v.typ == 'Func' && v.val == 'var') {
646-
isColor = false;
647-
break;
648-
}
649-
}
650-
if (isColor) {
651-
// @ts-ignore
652-
t.typ = 'Color';
653-
// @ts-ignore
654-
t.kin = t.val;
649+
t.typ = 'Color';
650+
// @ts-ignore
651+
t.kin = t.val;
652+
// @ts-ignore
653+
let m = t.chi.length;
654+
while (m-- > 0) {
655655
// @ts-ignore
656-
let m = t.chi.length;
657-
while (m-- > 0) {
656+
if (['Literal'].concat(trimWhiteSpace).includes(t.chi[m].typ)) {
658657
// @ts-ignore
659-
if (['Literal'].concat(trimWhiteSpace).includes(t.chi[m].typ)) {
658+
if (t.chi[m + 1]?.typ == 'Whitespace') {
660659
// @ts-ignore
661-
if (t.chi[m + 1]?.typ == 'Whitespace') {
662-
// @ts-ignore
663-
t.chi.splice(m + 1, 1);
664-
}
660+
t.chi.splice(m + 1, 1);
661+
}
662+
// @ts-ignore
663+
if (t.chi[m - 1]?.typ == 'Whitespace') {
665664
// @ts-ignore
666-
if (t.chi[m - 1]?.typ == 'Whitespace') {
667-
// @ts-ignore
668-
t.chi.splice(m - 1, 1);
669-
m--;
670-
}
665+
t.chi.splice(m - 1, 1);
666+
m--;
671667
}
672668
}
673-
continue;
674669
}
670+
continue;
671+
// }
675672
}
676673
if (t.typ == 'UrlFunc') {
677674
// @ts-ignore
@@ -696,7 +693,7 @@ function parseTokens(tokens, options = {}) {
696693
// @ts-ignore
697694
if (t.chi.length > 0) {
698695
// @ts-ignore
699-
parseTokens(t.chi, t.typ);
696+
parseTokens(t.chi, options);
700697
if (t.typ == 'Pseudo-class-func' && t.val == ':is' && options.minify) {
701698
//
702699
const count = t.chi.filter(t => t.typ != 'Comment').length;
@@ -715,7 +712,7 @@ function parseTokens(tokens, options = {}) {
715712
if (t.typ == 'Iden') {
716713
// named color
717714
const value = t.val.toLowerCase();
718-
if (COLORS_NAMES[value] != null) {
715+
if (value in COLORS_NAMES) {
719716
Object.assign(t, {
720717
typ: 'Color',
721718
val: COLORS_NAMES[value].length < value.length ? COLORS_NAMES[value] : value,

dist/lib/parser/tokenize.js

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -154,31 +154,18 @@ function* tokenize(iterator) {
154154
return char;
155155
}
156156
while (value = next()) {
157-
if (ind >= iterator.length) {
158-
if (buffer.length > 0) {
159-
yield pushToken(buffer);
160-
buffer = '';
161-
}
162-
break;
163-
}
164157
if (isWhiteSpace(value.charCodeAt(0))) {
165158
if (buffer.length > 0) {
166159
yield pushToken(buffer);
167160
buffer = '';
168161
}
169162
while (value = next()) {
170-
if (ind >= iterator.length) {
171-
break;
172-
}
173163
if (!isWhiteSpace(value.charCodeAt(0))) {
174164
break;
175165
}
176166
}
177167
yield pushToken('', 'Whitespace');
178168
buffer = '';
179-
if (ind >= iterator.length) {
180-
break;
181-
}
182169
}
183170
switch (value) {
184171
case '/':
@@ -192,34 +179,12 @@ function* tokenize(iterator) {
192179
}
193180
buffer += value;
194181
if (peek() == '*') {
195-
buffer += '*';
196-
// i++;
197-
next();
182+
buffer += next();
198183
while (value = next()) {
199-
if (ind >= iterator.length) {
200-
yield pushToken(buffer, 'Bad-comment');
201-
break;
202-
}
203-
if (value == '\\') {
204-
buffer += value;
205-
value = next();
206-
if (ind >= iterator.length) {
207-
yield pushToken(buffer, 'Bad-comment');
208-
break;
209-
}
210-
buffer += value;
211-
continue;
212-
}
213184
if (value == '*') {
214185
buffer += value;
215-
value = next();
216-
if (ind >= iterator.length) {
217-
yield pushToken(buffer, 'Bad-comment');
218-
break;
219-
}
220-
buffer += value;
221-
if (value == '/') {
222-
yield pushToken(buffer, 'Comment');
186+
if (peek() == '/') {
187+
yield pushToken(buffer + next(), 'Comment');
223188
buffer = '';
224189
break;
225190
}
@@ -228,6 +193,8 @@ function* tokenize(iterator) {
228193
buffer += value;
229194
}
230195
}
196+
yield pushToken(buffer, 'Bad-comment');
197+
buffer = '';
231198
}
232199
break;
233200
case '<':
@@ -241,15 +208,9 @@ function* tokenize(iterator) {
241208
break;
242209
}
243210
buffer += value;
244-
value = next();
245-
if (ind >= iterator.length) {
246-
break;
247-
}
248211
if (peek(3) == '!--') {
212+
buffer += next(3);
249213
while (value = next()) {
250-
if (ind >= iterator.length) {
251-
break;
252-
}
253214
buffer += value;
254215
if (value == '>' && prev(2) == '--') {
255216
yield pushToken(buffer, 'CDOCOMM');
@@ -258,15 +219,14 @@ function* tokenize(iterator) {
258219
}
259220
}
260221
}
261-
if (ind >= iterator.length) {
262-
yield pushToken(buffer, 'BADCDO');
263-
buffer = '';
264-
}
222+
// if (!peek()) {
223+
yield pushToken(buffer, 'Bad-cdo');
224+
buffer = '';
225+
// }
265226
break;
266227
case '\\':
267-
value = next();
268228
// EOF
269-
if (ind + 1 >= iterator.length) {
229+
if (!(value = next())) {
270230
// end of stream ignore \\
271231
yield pushToken(buffer);
272232
buffer = '';
@@ -285,8 +245,7 @@ function* tokenize(iterator) {
285245
buffer = '';
286246
}
287247
buffer += value;
288-
value = next();
289-
if (ind >= iterator.length) {
248+
if (!(value = next())) {
290249
yield pushToken(buffer);
291250
buffer = '';
292251
break;
@@ -453,8 +412,7 @@ function* tokenize(iterator) {
453412
yield pushToken(buffer);
454413
buffer = '';
455414
}
456-
const important = peek(9);
457-
if (important == 'important') {
415+
if (peek(9) == 'important') {
458416
yield pushToken('', 'Important');
459417
next(9);
460418
buffer = '';

dist/lib/parser/utils/syntax.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { colorsFunc } from '../../renderer/render.js';
2+
import { COLORS_NAMES } from '../../renderer/utils/color.js';
3+
14
// https://www.w3.org/TR/CSS21/syndata.html#syntax
25
// https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-ident-token
36
// '\\'
@@ -23,6 +26,25 @@ function isTime(dimension) {
2326
function isFrequency(dimension) {
2427
return 'unit' in dimension && ['hz', 'khz'].includes(dimension.unit.toLowerCase());
2528
}
29+
function isColor(token) {
30+
if (token.typ == 'Color') {
31+
return true;
32+
}
33+
if (token.typ == 'Iden') {
34+
// named color
35+
return token.val.toLowerCase() in COLORS_NAMES;
36+
}
37+
if (token.typ == 'Func' && token.chi.length > 0 && colorsFunc.includes(token.val)) {
38+
// @ts-ignore
39+
for (const v of token.chi) {
40+
if (!['Number', 'Perc', 'Comma', 'Whitespace'].includes(v.typ)) {
41+
return false;
42+
}
43+
}
44+
return true;
45+
}
46+
return false;
47+
}
2648
function isLetter(codepoint) {
2749
// lowercase
2850
return (codepoint >= 0x61 && codepoint <= 0x7a) ||
@@ -75,19 +97,12 @@ function isIdent(name) {
7597
return true;
7698
}
7799
function isPseudo(name) {
78-
if (name.charAt(0) != ':') {
79-
return false;
80-
}
81-
if (name.endsWith('(')) {
82-
return isIdent(name.charAt(1) == ':' ? name.slice(2, -1) : name.slice(1, -1));
83-
}
84-
return isIdent(name.charAt(1) == ':' ? name.slice(2) : name.slice(1));
100+
return name.charAt(0) == ':' &&
101+
((name.endsWith('(') && isIdent(name.charAt(1) == ':' ? name.slice(2, -1) : name.slice(1, -1))) ||
102+
isIdent(name.charAt(1) == ':' ? name.slice(2) : name.slice(1)));
85103
}
86104
function isHash(name) {
87-
if (name.charAt(0) != '#') {
88-
return false;
89-
}
90-
return isIdent(name.charAt(1));
105+
return name.charAt(0) == '#' && isIdent(name.charAt(1));
91106
}
92107
function isNumber(name) {
93108
if (name.length == 0) {
@@ -256,4 +271,4 @@ function isWhiteSpace(codepoint) {
256271
codepoint == 0xa || codepoint == 0xc || codepoint == 0xd;
257272
}
258273

259-
export { isAngle, isAtKeyword, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension };
274+
export { isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension };

dist/lib/parser/utils/type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ function matchType(val, properties) {
1313
return false;
1414
}
1515

16-
export { matchType };
16+
export { funcList, matchType };

dist/lib/renderer/render.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getAngle, COLORS_NAMES, rgb2Hex, hsl2Hex, hwb2hex, cmyk2hex, NAMES_COLORS } from './utils/color.js';
22

3+
const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk'];
34
function reduceNumber(val) {
45
val = (+val).toString();
56
if (val === '0') {
@@ -304,4 +305,4 @@ function renderToken(token, options = {}, reducer) {
304305
return '';
305306
}
306307

307-
export { render, renderToken };
308+
export { colorsFunc, render, renderToken };

0 commit comments

Comments
 (0)