@adguard/css-tokenizer
Advanced tools
Comparing version 1.1.1 to 1.2.0
/* | ||
* CSSTokenizer v1.1.1 (build date: Thu, 19 Sep 2024 13:23:31 GMT) | ||
* (c) 2024 Adguard Software Ltd. | ||
* CSSTokenizer v1.2.0 (build date: Mon, 03 Feb 2025 15:26:11 GMT) | ||
* (c) 2025 Adguard Software Ltd. | ||
* Released under the MIT license | ||
@@ -151,2 +151,12 @@ * https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/css-tokenizer#readme | ||
/** | ||
* Stops the tokenizer by moving the cursor to the end of the input. | ||
* | ||
* @note This method is defined as an arrow function to ensure it retains the correct `this` context. | ||
* Since `stop` is always passed to the `onToken` callback, which is invoked frequently during tokenization, | ||
* avoiding unnecessary overhead is crucial. | ||
* Using an arrow function provides better performance compared to binding the method in the constructor | ||
* or at the call site. | ||
*/ | ||
stop: () => void; | ||
/** | ||
* Check if the next code point is EOF | ||
@@ -216,6 +226,7 @@ * | ||
* @param props Other token properties (if any) | ||
* @param stop Function to halt the tokenization process | ||
* @note Hash tokens have a type flag set to either "id" or "unrestricted". The type flag defaults to "unrestricted" if | ||
* not otherwise set | ||
*/ | ||
type OnTokenCallback = (type: TokenType, start: number, end: number, props?: Record<string, unknown>) => void; | ||
type OnTokenCallback = (type: TokenType, start: number, end: number, props: Record<string, unknown> | undefined, stop: () => void) => void; | ||
/** | ||
@@ -238,2 +249,22 @@ * Callback which is called when a parsing error is found. According to the spec, parsing errors are not fatal and | ||
type TokenizerContextFunction = (context: TokenizerContext, ...args: any[]) => void; | ||
/** | ||
* Tokenizer function | ||
*/ | ||
type TokenizerFunction = ( | ||
/** | ||
* The raw string (source code) to tokenize. | ||
*/ | ||
source: string, | ||
/** | ||
* The callback invoked for each token encountered during tokenization. | ||
*/ | ||
onToken: OnTokenCallback, | ||
/** | ||
* The callback invoked on parsing errors (optional). | ||
*/ | ||
onError?: OnErrorCallback, | ||
/** | ||
* A map of function handlers (optional), typically used for handling custom parsing logic. | ||
*/ | ||
functionHandlers?: Map<number, TokenizerContextFunction>) => void; | ||
@@ -287,3 +318,3 @@ /** | ||
*/ | ||
declare const tokenize: (source: string, onToken: OnTokenCallback, onError?: OnErrorCallback, functionHandlers?: Map<number, TokenizerContextFunction>) => void; | ||
declare const tokenize: TokenizerFunction; | ||
@@ -314,3 +345,3 @@ /** | ||
*/ | ||
declare function tokenizeExtended(source: string, onToken: OnTokenCallback, onError?: OnErrorCallback, functionHandlers?: Map<number, TokenizerContextFunction>): void; | ||
declare const tokenizeExtended: TokenizerFunction; | ||
@@ -340,2 +371,16 @@ /** | ||
export { CSS_TOKENIZER_VERSION, type OnErrorCallback, type OnTokenCallback, TokenType, TokenizerContext, type TokenizerContextFunction, decodeIdent, getBaseTokenName, getFormattedTokenName, tokenize, tokenizeExtended }; | ||
/** | ||
* @file hasToken function | ||
*/ | ||
/** | ||
* Checks if the given raw string contains any of the specified tokens. | ||
* | ||
* @param raw - The raw string to be tokenized and checked. | ||
* @param tokens - A set of token types to check for in the raw string. | ||
* @param tokenizer - The tokenizer function to use. Defaults to `tokenizeExtended`. | ||
* @returns `true` if any of the specified tokens are found in the raw string, otherwise `false`. | ||
*/ | ||
declare const hasToken: (raw: string, tokens: Set<TokenType>, tokenizer?: TokenizerFunction) => boolean; | ||
export { CSS_TOKENIZER_VERSION, type OnErrorCallback, type OnTokenCallback, TokenType, TokenizerContext, type TokenizerContextFunction, decodeIdent, getBaseTokenName, getFormattedTokenName, hasToken, tokenize, tokenizeExtended }; |
{ | ||
"name": "@adguard/css-tokenizer", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "CSS / Extended CSS tokenizer", | ||
@@ -71,3 +71,2 @@ "keywords": [ | ||
"node-fetch": "^2.7.0", | ||
"os-name": "^5.1.0", | ||
"parse-css": "^0.1.0", | ||
@@ -78,2 +77,3 @@ "rimraf": "^5.0.5", | ||
"rollup-plugin-node-externals": "^6.1.1", | ||
"systeminformation": "^5.22.6", | ||
"terser": "^5.21.0", | ||
@@ -80,0 +80,0 @@ "ts-node": "^10.9.1", |
@@ -27,2 +27,3 @@ <!-- omit in toc --> | ||
- [Utilities](#utilities) | ||
- [`hasToken`](#hastoken) | ||
- [`TokenizerContext`](#tokenizercontext) | ||
@@ -180,6 +181,13 @@ - [`decodeIdent`](#decodeident) | ||
* @param props Other token properties (if any) | ||
* @param stop Function to halt the tokenization process | ||
* @note Hash tokens have a type flag set to either "id" or "unrestricted". The type flag defaults to "unrestricted" if | ||
* not otherwise set | ||
*/ | ||
type OnTokenCallback = (type: TokenType, start: number, end: number, props?: Record<string, unknown>) => void; | ||
type OnTokenCallback = ( | ||
type: TokenType, | ||
start: number, | ||
end: number, | ||
props: Record<string, unknown> | undefined, | ||
stop: () => void | ||
); | ||
``` | ||
@@ -237,2 +245,23 @@ | ||
#### `hasToken` | ||
```ts | ||
/** | ||
* Checks if the given raw string contains any of the specified tokens. | ||
* | ||
* @param raw - The raw string to be tokenized and checked. | ||
* @param tokens - A set of token types to check for in the raw string. | ||
* @param tokenizer - The tokenizer function to use. Defaults to `tokenizeExtended`. | ||
* | ||
* @example hasToken('div:contains("foo")', new Set([TokenType.Function]), tokenizeExtended); // true | ||
* | ||
* @returns `true` if any of the specified tokens are found in the raw string, otherwise `false`. | ||
*/ | ||
function hasToken = ( | ||
raw: string, | ||
tokens: Set<TokenType>, | ||
tokenizer: TokenizerFunction = tokenizeExtended, | ||
): boolean | ||
``` | ||
#### `TokenizerContext` | ||
@@ -239,0 +268,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
189990
3885
382