Skip to content

Commit 30cea85

Browse files
committed
fix a bug when remove prefixes is enabled #120
1 parent d5ac438 commit 30cea85

File tree

11 files changed

+81
-40
lines changed

11 files changed

+81
-40
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v1.4.2
4+
5+
- [x] fix bug when using remove prefix
6+
37
## v1.4.0
48

59
### CSS Module support

dist/index-umd-web.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23205,6 +23205,7 @@
2320523205
const combinators = ['+', '>', '~', '||', '|'];
2320623206
const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
2320723207
const notEndingWith = ['(', '['].concat(combinators);
23208+
const rules = [exports.EnumToken.AtRuleNodeType, exports.EnumToken.RuleNodeType, exports.EnumToken.AtRuleTokenType, exports.EnumToken.KeyFramesRuleNodeType];
2320823209
// @ts-ignore
2320923210
const features = Object.values(allFeatures).sort((a, b) => a.ordering - b.ordering);
2321023211
/**
@@ -23256,6 +23257,9 @@
2325623257
if ((feature.processMode & exports.FeatureWalkMode.Pre) === 0 || (feature.accept != null && !feature.accept.has(parent.typ))) {
2325723258
continue;
2325823259
}
23260+
if (rules.includes(replacement.typ) && !Array.isArray(replacement.tokens)) {
23261+
Object.defineProperty(replacement, 'tokens', { ...definedPropertySettings, value: parseString(replacement.typ == exports.EnumToken.RuleNodeType || replacement.typ == exports.EnumToken.KeyFramesRuleNodeType ? replacement.sel : replacement.val) });
23262+
}
2325923263
const result = feature.run(replacement, options, parent.parent ?? ast, context, exports.FeatureWalkMode.Pre);
2326023264
if (result != null) {
2326123265
replacement = result;
@@ -24636,26 +24640,30 @@
2463624640
})(exports.ResponseType || (exports.ResponseType = {}));
2463724641

2463824642
/**
24639-
* default file or url loader
24643+
* load file or url
2464024644
* @param url
24641-
* @param currentFile
24642-
*
24645+
* @param currentDirectory
2464324646
* @param responseType
24644-
* @private
24647+
* @throws Error file not found
24648+
*
24649+
* ```ts
24650+
* import {load, ResponseType} from '@tbela99/css-parser';
24651+
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
24652+
* ```
2464524653
*/
24646-
async function load(url, currentFile = '.', responseType = false) {
24654+
async function load(url, currentDirectory = '.', responseType = false) {
2464724655
if (typeof responseType == 'boolean') {
2464824656
responseType = responseType ? exports.ResponseType.ReadableStream : exports.ResponseType.Text;
2464924657
}
2465024658
let t;
2465124659
if (matchUrl.test(url)) {
2465224660
t = new URL(url);
2465324661
}
24654-
else if (currentFile != null && matchUrl.test(currentFile)) {
24655-
t = new URL(url, currentFile);
24662+
else if (currentDirectory != null && matchUrl.test(currentDirectory)) {
24663+
t = new URL(url, currentDirectory);
2465624664
}
2465724665
else {
24658-
const path = resolve(url, currentFile).absolute;
24666+
const path = resolve(url, currentDirectory).absolute;
2465924667
t = new URL(path, self.origin);
2466024668
}
2466124669
return fetch(t, t.origin != self.origin ? { mode: 'cors' } : {}).then(async (response) => {

dist/index.cjs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23391,6 +23391,7 @@ var allFeatures = /*#__PURE__*/Object.freeze({
2339123391
const combinators = ['+', '>', '~', '||', '|'];
2339223392
const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
2339323393
const notEndingWith = ['(', '['].concat(combinators);
23394+
const rules = [exports.EnumToken.AtRuleNodeType, exports.EnumToken.RuleNodeType, exports.EnumToken.AtRuleTokenType, exports.EnumToken.KeyFramesRuleNodeType];
2339423395
// @ts-ignore
2339523396
const features = Object.values(allFeatures).sort((a, b) => a.ordering - b.ordering);
2339623397
/**
@@ -23442,6 +23443,9 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
2344223443
if ((feature.processMode & exports.FeatureWalkMode.Pre) === 0 || (feature.accept != null && !feature.accept.has(parent.typ))) {
2344323444
continue;
2344423445
}
23446+
if (rules.includes(replacement.typ) && !Array.isArray(replacement.tokens)) {
23447+
Object.defineProperty(replacement, 'tokens', { ...definedPropertySettings, value: parseString(replacement.typ == exports.EnumToken.RuleNodeType || replacement.typ == exports.EnumToken.KeyFramesRuleNodeType ? replacement.sel : replacement.val) });
23448+
}
2344523449
const result = feature.run(replacement, options, parent.parent ?? ast, context, exports.FeatureWalkMode.Pre);
2344623450
if (result != null) {
2344723451
replacement = result;
@@ -24637,16 +24641,19 @@ function replaceCompoundLiteral(selector, replace) {
2463724641
}
2463824642

2463924643
/**
24640-
* load file or url as stream
24644+
* load file or url
2464124645
* @param url
24642-
* @param currentFile
24646+
* @param currentDirectory
2464324647
* @param responseType
2464424648
* @throws Error file not found
2464524649
*
24646-
* @private
24650+
* ```ts
24651+
* import {load, ResponseType} from '@tbela99/css-parser';
24652+
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
24653+
* ```
2464724654
*/
24648-
async function load(url, currentFile = '.', responseType = false) {
24649-
const resolved = resolve(url, currentFile);
24655+
async function load(url, currentDirectory = '.', responseType = false) {
24656+
const resolved = resolve(url, currentDirectory);
2465024657
if (typeof responseType == 'boolean') {
2465124658
responseType = responseType ? exports.ResponseType.ReadableStream : exports.ResponseType.Text;
2465224659
}

dist/index.d.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3521,11 +3521,11 @@ export declare interface ParserOptions extends MinifyOptions, MinifyFeatureOptio
35213521
/**
35223522
* url and file loader
35233523
* @param url
3524-
* @param currentUrl
3525-
* @param asStream
3524+
* @param currentDirectory
3525+
* @param responseType
35263526
*
35273527
*/
3528-
load?: (url: string, currentUrl?: string, asStream?: boolean) => LoadResult;
3528+
load?: (url: string, currentDirectory: string = '.', responseType?: boolean | ResponseType) => Promise<string | ArrayBuffer | ReadableStream<Uint8Array<ArrayBufferLike>>>;
35293529
/**
35303530
* get directory name
35313531
* @param path
@@ -4027,7 +4027,7 @@ declare function resolve(url: string, currentDirectory: string, cwd?: string): {
40274027
/**
40284028
* response type
40294029
*/
4030-
declare enum ResponseType {
4030+
declare enum ResponseType$1 {
40314031
/**
40324032
* return text
40334033
*/
@@ -4043,15 +4043,18 @@ declare enum ResponseType {
40434043
}
40444044

40454045
/**
4046-
* load file or url as stream
4046+
* load file or url
40474047
* @param url
4048-
* @param currentFile
4048+
* @param currentDirectory
40494049
* @param responseType
40504050
* @throws Error file not found
40514051
*
4052-
* @private
4052+
* ```ts
4053+
* import {load, ResponseType} from '@tbela99/css-parser';
4054+
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
4055+
* ```
40534056
*/
4054-
declare function load(url: string, currentFile?: string, responseType?: boolean | ResponseType): Promise<string | ArrayBuffer | ReadableStream<Uint8Array<ArrayBufferLike>>>;
4057+
declare function load(url: string, currentDirectory?: string, responseType?: boolean | ResponseType$1): Promise<string | ArrayBuffer | ReadableStream<Uint8Array<ArrayBufferLike>>>;
40554058
/**
40564059
* render the ast tree
40574060
* @param data
@@ -4222,5 +4225,5 @@ declare function transformFile(file: string, options?: TransformOptions, asStrea
42224225
*/
42234226
declare function transform(css: string | ReadableStream<Uint8Array>, options?: TransformOptions): Promise<TransformResult>;
42244227

4225-
export { ColorType, EnumToken, FeatureWalkMode, ModuleCaseTransformEnum, ModuleScopeEnumOptions, ResponseType, SourceMap, ValidationLevel, WalkerEvent, WalkerOptionEnum, convertColor, dirname, expand, isOkLabClose, load, mathFuncs, minify, okLabDistance, parse, parseDeclarations, parseFile, parseString, parseTokens, render, renderToken, resolve, transform, transformFile, transformFunctions, walk, walkValues };
4228+
export { ColorType, EnumToken, FeatureWalkMode, ModuleCaseTransformEnum, ModuleScopeEnumOptions, ResponseType$1 as ResponseType, SourceMap, ValidationLevel, WalkerEvent, WalkerOptionEnum, convertColor, dirname, expand, isOkLabClose, load, mathFuncs, minify, okLabDistance, parse, parseDeclarations, parseFile, parseString, parseTokens, render, renderToken, resolve, transform, transformFile, transformFunctions, walk, walkValues };
42264229
export type { AddToken, AngleToken, AstAtRule, AstComment, AstDeclaration, AstInvalidAtRule, AstInvalidDeclaration, AstInvalidRule, AstKeyFrameRule, AstKeyframesAtRule, AstKeyframesRule, AstNode$1 as AstNode, AstRule, AstRuleList, AstStyleSheet, AtRuleToken, AtRuleVisitorHandler, AttrEndToken, AttrStartToken, AttrToken, Background, BackgroundAttachmentMapping, BackgroundPosition, BackgroundPositionClass, BackgroundPositionConstraints, BackgroundPositionMapping, BackgroundProperties, BackgroundRepeat, BackgroundRepeatMapping, BackgroundSize, BackgroundSizeMapping, BadCDOCommentToken, BadCommentToken, BadStringToken, BadUrlToken, BaseToken, BinaryExpressionNode, BinaryExpressionToken, BlockEndToken, BlockStartToken, Border, BorderColor, BorderColorClass, BorderProperties, BorderRadius, CDOCommentToken, ChildCombinatorToken, ClassSelectorToken, ColonToken, ColorToken, ColumnCombinatorToken, CommaToken, CommentToken, ComposesSelectorToken, ConstraintsMapping, ContainMatchToken, Context, CssVariableImportTokenType$1 as CssVariableImportTokenType, CssVariableMapTokenType, CssVariableToken$1 as CssVariableToken, DashMatchToken, DashedIdentToken, DeclarationVisitorHandler, DelimToken, DescendantCombinatorToken, DimensionToken, DivToken, EOFToken, EndMatchToken, EqualMatchToken, ErrorDescription, FlexToken, Font, FontFamily, FontProperties, FontWeight, FontWeightConstraints, FontWeightMapping, FractionToken, FrequencyToken, FunctionImageToken, FunctionToken, FunctionURLToken, GenericVisitorAstNodeHandlerMap, GenericVisitorHandler, GenericVisitorResult, GreaterThanOrEqualToken, GreaterThanToken, GridTemplateFuncToken, HashToken, IdentListToken, IdentToken, ImportantToken, IncludeMatchToken, InvalidAttrToken, InvalidClassSelectorToken, LengthToken, LessThanOrEqualToken, LessThanToken, LineHeight, ListToken, LiteralToken, LoadResult, Location, Map$1 as Map, MatchExpressionToken, MatchedSelector, MediaFeatureAndToken, MediaFeatureNotToken, MediaFeatureOnlyToken, MediaFeatureOrToken, MediaFeatureToken, MediaQueryConditionToken, MinifyFeature, MinifyFeatureOptions, MinifyOptions, ModuleOptions, MulToken, NameSpaceAttributeToken, NestingSelectorToken, NextSiblingCombinatorToken, NumberToken, OptimizedSelector, OptimizedSelectorToken, Outline, OutlineProperties, ParensEndToken, ParensStartToken, ParensToken, ParseInfo, ParseResult, ParseResultStats, ParseTokenOptions, ParserOptions, PercentageToken, Position, Prefix, PropertiesConfig, PropertiesConfigProperties, PropertyListOptions, PropertyMapType, PropertySetType, PropertyType, PseudoClassFunctionToken, PseudoClassToken, PseudoElementToken, PseudoPageToken, PurpleBackgroundAttachment, RawSelectorTokens, RenderOptions, RenderResult, ResolutionToken, ResolvedPath, RuleVisitorHandler, SemiColonToken, Separator, ShorthandDef, ShorthandMapType, ShorthandProperties, ShorthandPropertyType, ShorthandType, SourceMapObject, StartMatchToken, StringToken, SubToken, SubsequentCombinatorToken, TimeToken, TimelineFunctionToken, TimingFunctionToken, Token$1 as Token, TokenizeResult, TransformOptions, TransformResult, UnaryExpression, UnaryExpressionNode, UnclosedStringToken, UniversalSelectorToken, UrlToken, ValidationConfiguration, ValidationOptions, ValidationResult, ValidationSelectorOptions, ValidationSyntaxNode, ValidationSyntaxResult, Value, ValueVisitorHandler, VariableScopeInfo, VisitorNodeMap, WalkAttributesResult, WalkResult, WalkerFilter, WalkerOption, WalkerValueFilter, WhitespaceToken };

dist/lib/ast/minify.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { replaceToken, parseString } from '../parser/parse.js';
1+
import { parseString, replaceToken } from '../parser/parse.js';
22
import '../parser/tokenize.js';
33
import '../parser/utils/config.js';
44
import { EnumToken } from './types.js';
@@ -13,6 +13,7 @@ import { FeatureWalkMode } from './features/type.js';
1313
const combinators = ['+', '>', '~', '||', '|'];
1414
const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
1515
const notEndingWith = ['(', '['].concat(combinators);
16+
const rules = [EnumToken.AtRuleNodeType, EnumToken.RuleNodeType, EnumToken.AtRuleTokenType, EnumToken.KeyFramesRuleNodeType];
1617
// @ts-ignore
1718
const features = Object.values(index).sort((a, b) => a.ordering - b.ordering);
1819
/**
@@ -64,6 +65,9 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
6465
if ((feature.processMode & FeatureWalkMode.Pre) === 0 || (feature.accept != null && !feature.accept.has(parent.typ))) {
6566
continue;
6667
}
68+
if (rules.includes(replacement.typ) && !Array.isArray(replacement.tokens)) {
69+
Object.defineProperty(replacement, 'tokens', { ...definedPropertySettings, value: parseString(replacement.typ == EnumToken.RuleNodeType || replacement.typ == EnumToken.KeyFramesRuleNodeType ? replacement.sel : replacement.val) });
70+
}
6771
const result = feature.run(replacement, options, parent.parent ?? ast, context, FeatureWalkMode.Pre);
6872
if (result != null) {
6973
replacement = result;

dist/node.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@ import { ResponseType } from './types.js';
2626
export { FeatureWalkMode } from './lib/ast/features/type.js';
2727

2828
/**
29-
* load file or url as stream
29+
* load file or url
3030
* @param url
31-
* @param currentFile
31+
* @param currentDirectory
3232
* @param responseType
3333
* @throws Error file not found
3434
*
35-
* @private
35+
* ```ts
36+
* import {load, ResponseType} from '@tbela99/css-parser';
37+
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
38+
* ```
3639
*/
37-
async function load(url, currentFile = '.', responseType = false) {
38-
const resolved = resolve(url, currentFile);
40+
async function load(url, currentDirectory = '.', responseType = false) {
41+
const resolved = resolve(url, currentDirectory);
3942
if (typeof responseType == 'boolean') {
4043
responseType = responseType ? ResponseType.ReadableStream : ResponseType.Text;
4144
}

dist/web.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,30 @@ import { ResponseType } from './types.js';
2222
export { FeatureWalkMode } from './lib/ast/features/type.js';
2323

2424
/**
25-
* default file or url loader
25+
* load file or url
2626
* @param url
27-
* @param currentFile
28-
*
27+
* @param currentDirectory
2928
* @param responseType
30-
* @private
29+
* @throws Error file not found
30+
*
31+
* ```ts
32+
* import {load, ResponseType} from '@tbela99/css-parser';
33+
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
34+
* ```
3135
*/
32-
async function load(url, currentFile = '.', responseType = false) {
36+
async function load(url, currentDirectory = '.', responseType = false) {
3337
if (typeof responseType == 'boolean') {
3438
responseType = responseType ? ResponseType.ReadableStream : ResponseType.Text;
3539
}
3640
let t;
3741
if (matchUrl.test(url)) {
3842
t = new URL(url);
3943
}
40-
else if (currentFile != null && matchUrl.test(currentFile)) {
41-
t = new URL(url, currentFile);
44+
else if (currentDirectory != null && matchUrl.test(currentDirectory)) {
45+
t = new URL(url, currentDirectory);
4246
}
4347
else {
44-
const path = resolve(url, currentFile).absolute;
48+
const path = resolve(url, currentDirectory).absolute;
4549
t = new URL(path, self.origin);
4650
}
4751
return fetch(t, t.origin != self.origin ? { mode: 'cors' } : {}).then(async (response) => {

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tbela99/css-parser",
3-
"version": "1.4.1",
3+
"version": "1.4.2",
44
"publish": {
55
"include": [
66
"src",

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@tbela99/css-parser",
33
"description": "CSS parser, minifier and validator for node and the browser",
4-
"version": "v1.4.1",
4+
"version": "v1.4.2",
55
"exports": {
66
".": "./dist/node.js",
77
"./node": "./dist/node.js",
@@ -32,6 +32,8 @@
3232
"keywords": [
3333
"parser",
3434
"css",
35+
"modules",
36+
"css-modules",
3537
"css-parser",
3638
"node",
3739
"ast",
@@ -48,7 +50,7 @@
4850
"streaming-parser"
4951
],
5052
"author": "Thierry Bela",
51-
"license": "MIT OR LGPL-3.0",
53+
"license": "MIT",
5254
"bugs": {
5355
"url": "https://github.com/tbela99/css-parser/issues"
5456
},

src/@types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ export declare interface ParserOptions extends MinifyOptions, MinifyFeatureOptio
352352
* @param responseType
353353
*
354354
*/
355-
load?: (url: string, currentDirectory: string = '.', responseType?: boolean | ResponseType) => Promise<string | ArrayBuffer | ReadableStream<Uint8Array<ArrayBufferLike>>>;
355+
load?: (url: string, currentDirectory: string, responseType?: boolean | ResponseType) => Promise<string | ArrayBuffer | ReadableStream<Uint8Array<ArrayBufferLike>>>;
356356
/**
357357
* get directory name
358358
* @param path

0 commit comments

Comments
 (0)