Skip to content

Commit

Permalink
fix: ident parse (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
barak007 committed Jun 8, 2023
1 parent 0f6aaea commit c2867d4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ export interface TokyOptions<T extends Token<unknown>> {
): boolean;
getUnclosedComment(inComment: string): string;
createToken(value: string, type: T['type'], start: number, end: number): T;
shouldClose?(ch: string, previousChar: string): boolean;
offset?: number;
}

export function tokenize<T extends Token<unknown>>(
source: string,
{
shouldClose,
isDelimiter,
isStringDelimiter,
isWhitespace,
Expand Down Expand Up @@ -70,6 +72,10 @@ export function tokenize<T extends Token<unknown>>(
} else if (!isWhitespace(ch) && isWhitespace(previousChar)) {
pushBuffer();
buffer += ch;
} else if(shouldClose?.(ch, previousChar)) {
pushBuffer();
buffer += ch;
pushBuffer(ch);
} else {
buffer += ch;
}
Expand Down
17 changes: 16 additions & 1 deletion packages/css-value-parser/src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@tokey/core';
import type { Token, Descriptors } from '@tokey/core';

type Delimiters = '(' | ')' | ',' | '/' | '+' | '-' | '*' | '#' | '.';
type Delimiters = '(' | ')' | ',' | '/' | '+' | '-' | '*' | '#' | '.' | '%';

export type CSSValueToken = Token<Descriptors | Delimiters>;

Expand All @@ -29,6 +29,20 @@ export function tokenizeValue(source: string, options: { offset?: number } = {})
: getMultilineCommentStartType,
isCommentEnd,
getUnclosedComment,
shouldClose(ch, previousChar) {
if (isWhitespace(ch) && isWhitespace(previousChar)) {
return false;
}
if (previousChar === '\\') {
return false;
}
const isAllowedChars = /[-_a-zA-Z0-9]/.test(ch);
if (isAllowedChars) {
return false;
}
// match css identifier char don't allow non-ascii chars
return ch.charCodeAt(0) <= 127;
},
offset: options.offset,
});
}
Expand All @@ -55,4 +69,5 @@ const isDelimiter = (char: string, previousChar: string) =>
char === '-' ||
char === '*' ||
char === '#' ||
char === '%' ||
char === '.');
1 change: 1 addition & 0 deletions packages/css-value-parser/src/value-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ function parseNumber(
nextToken.type === `-` ||
nextToken.type === `+` ||
nextToken.type === `.` ||
nextToken.type === `%` ||
nextToken.type === `text`
) {
const nextValue = numberValue + nextToken.value;
Expand Down
19 changes: 19 additions & 0 deletions packages/css-value-parser/test/value-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,25 @@ describe(`value-parser`, () => {
literal({ value: `]`, start: 4, end: 5 }),
],
},
{
type: `<custom-ident>`,
desc: `split custom ident`,
source: `abc==efg`,
expected: [
customIdent({
value: `abc`,
start: 0,
end: 3,
}),
literal({ value: `=`, start: 3, end: 4 }),
literal({ value: `=`, start: 4, end: 5 }),
customIdent({
value: `efg`,
start: 5,
end: 8,
}),
],
},
].forEach(createTest);
});
describe(`dashed-ident`, () => {
Expand Down

0 comments on commit c2867d4

Please sign in to comment.