Skip to content

Commit

Permalink
add new test cases #10
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Aug 17, 2023
1 parent a59a29d commit ce756cc
Show file tree
Hide file tree
Showing 24 changed files with 3,451 additions and 3,429 deletions.
3,185 changes: 1,578 additions & 1,607 deletions dist/index-umd-web.js

Large diffs are not rendered by default.

3,185 changes: 1,578 additions & 1,607 deletions dist/index.cjs

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ interface CDOCommentToken {
val: string;
}
interface BadCDOCommentToken {
typ: 'BADCDO';
typ: 'Bad-cdo';
val: string;
}
interface IncludesToken {
Expand Down Expand Up @@ -642,6 +642,7 @@ declare function isResolution(dimension: DimensionToken): boolean;
declare function isAngle(dimension: DimensionToken): boolean;
declare function isTime(dimension: DimensionToken): boolean;
declare function isFrequency(dimension: DimensionToken): boolean;
declare function isColor(token: Token): boolean;
declare function isIdentStart(codepoint: number): boolean;
declare function isDigit(codepoint: number): boolean;
declare function isIdentCodepoint(codepoint: number): boolean;
Expand All @@ -661,8 +662,10 @@ declare function isWhiteSpace(codepoint: number): boolean;

declare const getConfig: () => PropertiesConfig;

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

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

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

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 };
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 };
1 change: 1 addition & 0 deletions dist/lib/parser/declaration/list.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PropertySet } from './set.js';
import '../../renderer/utils/color.js';
import { getConfig } from '../utils/config.js';
import { PropertyMap } from './map.js';
import { parseString } from '../parse.js';
Expand Down
3 changes: 2 additions & 1 deletion dist/lib/parser/declaration/map.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { eq } from '../utils/eq.js';
import { renderToken } from '../../renderer/render.js';
import '../../renderer/utils/color.js';
import { getConfig } from '../utils/config.js';
import { matchType } from '../utils/type.js';
import { renderToken } from '../../renderer/render.js';
import { parseString } from '../parse.js';
import { PropertySet } from './set.js';

Expand Down
55 changes: 26 additions & 29 deletions dist/lib/parser/parse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPseudo, isAtKeyword, isFunction, isNumber, isDimension, parseDimension, isPercentage, isIdent, isHexColor, isHash, isIdentStart } from './utils/syntax.js';
import { isPseudo, isAtKeyword, isFunction, isNumber, isDimension, parseDimension, isPercentage, isIdent, isHexColor, isHash, isIdentStart, isColor } from './utils/syntax.js';
import { renderToken } from '../renderer/render.js';
import { COLORS_NAMES } from '../renderer/utils/color.js';
import { minify, combinators } from '../ast/minify.js';
Expand Down Expand Up @@ -328,6 +328,11 @@ async function parse(iterator, opt = {}) {
if (item == null) {
break;
}
// console.debug({item});
if (item.hint != null && item.hint.startsWith('Bad-')) {
// bad token
continue;
}
tokens.push(item);
bytesIn = item.bytesIn;
if (item.token == ';' || item.token == '{') {
Expand Down Expand Up @@ -637,41 +642,33 @@ function parseTokens(tokens, options = {}) {
// @ts-ignore
t.chi.pop();
}
let isColor = true;
// @ts-ignore
if (options.parseColor && ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk'].includes(t.val)) {
if (options.parseColor && t.typ == 'Func' && isColor(t)) {
// if (isColor) {
// @ts-ignore
for (const v of t.chi) {
if (v.typ == 'Func' && v.val == 'var') {
isColor = false;
break;
}
}
if (isColor) {
// @ts-ignore
t.typ = 'Color';
// @ts-ignore
t.kin = t.val;
t.typ = 'Color';
// @ts-ignore
t.kin = t.val;
// @ts-ignore
let m = t.chi.length;
while (m-- > 0) {
// @ts-ignore
let m = t.chi.length;
while (m-- > 0) {
if (['Literal'].concat(trimWhiteSpace).includes(t.chi[m].typ)) {
// @ts-ignore
if (['Literal'].concat(trimWhiteSpace).includes(t.chi[m].typ)) {
if (t.chi[m + 1]?.typ == 'Whitespace') {
// @ts-ignore
if (t.chi[m + 1]?.typ == 'Whitespace') {
// @ts-ignore
t.chi.splice(m + 1, 1);
}
t.chi.splice(m + 1, 1);
}
// @ts-ignore
if (t.chi[m - 1]?.typ == 'Whitespace') {
// @ts-ignore
if (t.chi[m - 1]?.typ == 'Whitespace') {
// @ts-ignore
t.chi.splice(m - 1, 1);
m--;
}
t.chi.splice(m - 1, 1);
m--;
}
}
continue;
}
continue;
// }
}
if (t.typ == 'UrlFunc') {
// @ts-ignore
Expand All @@ -696,7 +693,7 @@ function parseTokens(tokens, options = {}) {
// @ts-ignore
if (t.chi.length > 0) {
// @ts-ignore
parseTokens(t.chi, t.typ);
parseTokens(t.chi, options);
if (t.typ == 'Pseudo-class-func' && t.val == ':is' && options.minify) {
//
const count = t.chi.filter(t => t.typ != 'Comment').length;
Expand All @@ -715,7 +712,7 @@ function parseTokens(tokens, options = {}) {
if (t.typ == 'Iden') {
// named color
const value = t.val.toLowerCase();
if (COLORS_NAMES[value] != null) {
if (value in COLORS_NAMES) {
Object.assign(t, {
typ: 'Color',
val: COLORS_NAMES[value].length < value.length ? COLORS_NAMES[value] : value,
Expand Down
68 changes: 13 additions & 55 deletions dist/lib/parser/tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,31 +154,18 @@ function* tokenize(iterator) {
return char;
}
while (value = next()) {
if (ind >= iterator.length) {
if (buffer.length > 0) {
yield pushToken(buffer);
buffer = '';
}
break;
}
if (isWhiteSpace(value.charCodeAt(0))) {
if (buffer.length > 0) {
yield pushToken(buffer);
buffer = '';
}
while (value = next()) {
if (ind >= iterator.length) {
break;
}
if (!isWhiteSpace(value.charCodeAt(0))) {
break;
}
}
yield pushToken('', 'Whitespace');
buffer = '';
if (ind >= iterator.length) {
break;
}
}
switch (value) {
case '/':
Expand All @@ -192,34 +179,12 @@ function* tokenize(iterator) {
}
buffer += value;
if (peek() == '*') {
buffer += '*';
// i++;
next();
buffer += next();
while (value = next()) {
if (ind >= iterator.length) {
yield pushToken(buffer, 'Bad-comment');
break;
}
if (value == '\\') {
buffer += value;
value = next();
if (ind >= iterator.length) {
yield pushToken(buffer, 'Bad-comment');
break;
}
buffer += value;
continue;
}
if (value == '*') {
buffer += value;
value = next();
if (ind >= iterator.length) {
yield pushToken(buffer, 'Bad-comment');
break;
}
buffer += value;
if (value == '/') {
yield pushToken(buffer, 'Comment');
if (peek() == '/') {
yield pushToken(buffer + next(), 'Comment');
buffer = '';
break;
}
Expand All @@ -228,6 +193,8 @@ function* tokenize(iterator) {
buffer += value;
}
}
yield pushToken(buffer, 'Bad-comment');
buffer = '';
}
break;
case '<':
Expand All @@ -241,15 +208,9 @@ function* tokenize(iterator) {
break;
}
buffer += value;
value = next();
if (ind >= iterator.length) {
break;
}
if (peek(3) == '!--') {
buffer += next(3);
while (value = next()) {
if (ind >= iterator.length) {
break;
}
buffer += value;
if (value == '>' && prev(2) == '--') {
yield pushToken(buffer, 'CDOCOMM');
Expand All @@ -258,15 +219,14 @@ function* tokenize(iterator) {
}
}
}
if (ind >= iterator.length) {
yield pushToken(buffer, 'BADCDO');
buffer = '';
}
// if (!peek()) {
yield pushToken(buffer, 'Bad-cdo');
buffer = '';
// }
break;
case '\\':
value = next();
// EOF
if (ind + 1 >= iterator.length) {
if (!(value = next())) {
// end of stream ignore \\
yield pushToken(buffer);
buffer = '';
Expand All @@ -285,8 +245,7 @@ function* tokenize(iterator) {
buffer = '';
}
buffer += value;
value = next();
if (ind >= iterator.length) {
if (!(value = next())) {
yield pushToken(buffer);
buffer = '';
break;
Expand Down Expand Up @@ -453,8 +412,7 @@ function* tokenize(iterator) {
yield pushToken(buffer);
buffer = '';
}
const important = peek(9);
if (important == 'important') {
if (peek(9) == 'important') {
yield pushToken('', 'Important');
next(9);
buffer = '';
Expand Down
39 changes: 27 additions & 12 deletions dist/lib/parser/utils/syntax.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { colorsFunc } from '../../renderer/render.js';
import { COLORS_NAMES } from '../../renderer/utils/color.js';

// https://www.w3.org/TR/CSS21/syndata.html#syntax
// https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-ident-token
// '\\'
Expand All @@ -23,6 +26,25 @@ function isTime(dimension) {
function isFrequency(dimension) {
return 'unit' in dimension && ['hz', 'khz'].includes(dimension.unit.toLowerCase());
}
function isColor(token) {
if (token.typ == 'Color') {
return true;
}
if (token.typ == 'Iden') {
// named color
return token.val.toLowerCase() in COLORS_NAMES;
}
if (token.typ == 'Func' && token.chi.length > 0 && colorsFunc.includes(token.val)) {
// @ts-ignore
for (const v of token.chi) {
if (!['Number', 'Perc', 'Comma', 'Whitespace'].includes(v.typ)) {
return false;
}
}
return true;
}
return false;
}
function isLetter(codepoint) {
// lowercase
return (codepoint >= 0x61 && codepoint <= 0x7a) ||
Expand Down Expand Up @@ -75,19 +97,12 @@ function isIdent(name) {
return true;
}
function isPseudo(name) {
if (name.charAt(0) != ':') {
return false;
}
if (name.endsWith('(')) {
return isIdent(name.charAt(1) == ':' ? name.slice(2, -1) : name.slice(1, -1));
}
return isIdent(name.charAt(1) == ':' ? name.slice(2) : name.slice(1));
return name.charAt(0) == ':' &&
((name.endsWith('(') && isIdent(name.charAt(1) == ':' ? name.slice(2, -1) : name.slice(1, -1))) ||
isIdent(name.charAt(1) == ':' ? name.slice(2) : name.slice(1)));
}
function isHash(name) {
if (name.charAt(0) != '#') {
return false;
}
return isIdent(name.charAt(1));
return name.charAt(0) == '#' && isIdent(name.charAt(1));
}
function isNumber(name) {
if (name.length == 0) {
Expand Down Expand Up @@ -256,4 +271,4 @@ function isWhiteSpace(codepoint) {
codepoint == 0xa || codepoint == 0xc || codepoint == 0xd;
}

export { isAngle, isAtKeyword, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension };
export { isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension };
2 changes: 1 addition & 1 deletion dist/lib/parser/utils/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ function matchType(val, properties) {
return false;
}

export { matchType };
export { funcList, matchType };
3 changes: 2 additions & 1 deletion dist/lib/renderer/render.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getAngle, COLORS_NAMES, rgb2Hex, hsl2Hex, hwb2hex, cmyk2hex, NAMES_COLORS } from './utils/color.js';

const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk'];
function reduceNumber(val) {
val = (+val).toString();
if (val === '0') {
Expand Down Expand Up @@ -304,4 +305,4 @@ function renderToken(token, options = {}, reducer) {
return '';
}

export { render, renderToken };
export { colorsFunc, render, renderToken };
1 change: 1 addition & 0 deletions dist/lib/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parse } from './parser/parse.js';
import { render } from './renderer/render.js';
import './renderer/utils/color.js';

async function transform(css, options = {}) {
options = { minify: true, removeEmpty: true, ...options };
Expand Down
6 changes: 3 additions & 3 deletions dist/node/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { parse as parse$1 } from '../lib/parser/parse.js';
export { parseString, urlTokenMatcher } from '../lib/parser/parse.js';
export { tokenize } from '../lib/parser/tokenize.js';
export { isAngle, isAtKeyword, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from '../lib/parser/utils/syntax.js';
export { isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from '../lib/parser/utils/syntax.js';
export { getConfig } from '../lib/parser/utils/config.js';
export { matchType } from '../lib/parser/utils/type.js';
export { render, renderToken } from '../lib/renderer/render.js';
export { funcList, matchType } from '../lib/parser/utils/type.js';
export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
import { transform as transform$1 } from '../lib/transform.js';
export { combinators, hasDeclaration, minify, minifyRule, reduceSelector } from '../lib/ast/minify.js';
export { walk } from '../lib/ast/walk.js';
Expand Down
6 changes: 3 additions & 3 deletions dist/web/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { parse as parse$1 } from '../lib/parser/parse.js';
export { parseString, urlTokenMatcher } from '../lib/parser/parse.js';
export { tokenize } from '../lib/parser/tokenize.js';
export { isAngle, isAtKeyword, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from '../lib/parser/utils/syntax.js';
export { isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from '../lib/parser/utils/syntax.js';
export { getConfig } from '../lib/parser/utils/config.js';
export { matchType } from '../lib/parser/utils/type.js';
export { render, renderToken } from '../lib/renderer/render.js';
export { funcList, matchType } from '../lib/parser/utils/type.js';
export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
import { transform as transform$1 } from '../lib/transform.js';
export { combinators, hasDeclaration, minify, minifyRule, reduceSelector } from '../lib/ast/minify.js';
export { walk } from '../lib/ast/walk.js';
Expand Down
Loading

0 comments on commit ce756cc

Please sign in to comment.