Skip to content

Commit 89db057

Browse files
committed
failed to flatten @import when using url() syntax #33
1 parent 46b8379 commit 89db057

File tree

8 files changed

+65
-28
lines changed

8 files changed

+65
-28
lines changed

CHANGELOG.md

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

3+
## V0.5.1
4+
- [x] failed to flatten @import when using url() syntax
5+
-
36
## V0.5.0
47
- [x] render node with parents
58
- [x] fix relative color from xyz

dist/index-umd-web.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6371,12 +6371,16 @@
63716371
}
63726372
if (atRule.val == 'import') {
63736373
// @ts-ignore
6374-
if (tokens[0].typ == exports.EnumToken.UrlFunctionTokenType && tokens[1].typ == exports.EnumToken.UrlTokenTokenType) {
6375-
tokens.shift();
6376-
// @ts-ignore
6377-
tokens[0].typ = exports.EnumToken.StringTokenType;
6378-
// @ts-ignore
6379-
tokens[0].val = `"${tokens[0].val}"`;
6374+
if (tokens[0].typ == exports.EnumToken.UrlFunctionTokenType) {
6375+
if (tokens[1].typ == exports.EnumToken.UrlTokenTokenType || tokens[1].typ == exports.EnumToken.StringTokenType) {
6376+
tokens.shift();
6377+
if (tokens[1].typ == exports.EnumToken.UrlTokenTokenType) {
6378+
// @ts-ignore
6379+
tokens[0].typ = exports.EnumToken.StringTokenType;
6380+
// @ts-ignore
6381+
tokens[0].val = `"${tokens[0].val}"`;
6382+
}
6383+
}
63806384
}
63816385
// @ts-ignore
63826386
if (tokens[0].typ == exports.EnumToken.StringTokenType) {

dist/index.cjs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6369,12 +6369,16 @@ async function parseNode(results, context, stats, options, errors, src, map) {
63696369
}
63706370
if (atRule.val == 'import') {
63716371
// @ts-ignore
6372-
if (tokens[0].typ == exports.EnumToken.UrlFunctionTokenType && tokens[1].typ == exports.EnumToken.UrlTokenTokenType) {
6373-
tokens.shift();
6374-
// @ts-ignore
6375-
tokens[0].typ = exports.EnumToken.StringTokenType;
6376-
// @ts-ignore
6377-
tokens[0].val = `"${tokens[0].val}"`;
6372+
if (tokens[0].typ == exports.EnumToken.UrlFunctionTokenType) {
6373+
if (tokens[1].typ == exports.EnumToken.UrlTokenTokenType || tokens[1].typ == exports.EnumToken.StringTokenType) {
6374+
tokens.shift();
6375+
if (tokens[1].typ == exports.EnumToken.UrlTokenTokenType) {
6376+
// @ts-ignore
6377+
tokens[0].typ = exports.EnumToken.StringTokenType;
6378+
// @ts-ignore
6379+
tokens[0].val = `"${tokens[0].val}"`;
6380+
}
6381+
}
63786382
}
63796383
// @ts-ignore
63806384
if (tokens[0].typ == exports.EnumToken.StringTokenType) {

dist/lib/parser/parse.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,16 @@ async function parseNode(results, context, stats, options, errors, src, map) {
346346
}
347347
if (atRule.val == 'import') {
348348
// @ts-ignore
349-
if (tokens[0].typ == EnumToken.UrlFunctionTokenType && tokens[1].typ == EnumToken.UrlTokenTokenType) {
350-
tokens.shift();
351-
// @ts-ignore
352-
tokens[0].typ = EnumToken.StringTokenType;
353-
// @ts-ignore
354-
tokens[0].val = `"${tokens[0].val}"`;
349+
if (tokens[0].typ == EnumToken.UrlFunctionTokenType) {
350+
if (tokens[1].typ == EnumToken.UrlTokenTokenType || tokens[1].typ == EnumToken.StringTokenType) {
351+
tokens.shift();
352+
if (tokens[1].typ == EnumToken.UrlTokenTokenType) {
353+
// @ts-ignore
354+
tokens[0].typ = EnumToken.StringTokenType;
355+
// @ts-ignore
356+
tokens[0].val = `"${tokens[0].val}"`;
357+
}
358+
}
355359
}
356360
// @ts-ignore
357361
if (tokens[0].typ == EnumToken.StringTokenType) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@tbela99/css-parser",
33
"description": "CSS parser for node and the browser",
4-
"version": "0.5.0",
4+
"version": "0.5.1",
55
"exports": {
66
".": "./dist/node/index.js",
77
"./umd": "./dist/index-umd-web.js",

src/lib/parser/parse.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,20 @@ async function parseNode(results: TokenizeResult[], context: AstRuleList, stats:
532532
if (atRule.val == 'import') {
533533

534534
// @ts-ignore
535-
if (tokens[0].typ == EnumToken.UrlFunctionTokenType && tokens[1].typ == EnumToken.UrlTokenTokenType) {
536-
tokens.shift();
537-
// @ts-ignore
538-
tokens[0].typ = EnumToken.StringTokenType;
539-
// @ts-ignore
540-
tokens[0].val = `"${tokens[0].val}"`;
535+
if (tokens[0].typ == EnumToken.UrlFunctionTokenType) {
536+
537+
if (tokens[1].typ == EnumToken.UrlTokenTokenType || tokens[1].typ == EnumToken.StringTokenType) {
538+
539+
tokens.shift();
540+
541+
if (tokens[1].typ == EnumToken.UrlTokenTokenType) {
542+
543+
// @ts-ignore
544+
tokens[0].typ = EnumToken.StringTokenType;
545+
// @ts-ignore
546+
tokens[0].val = `"${tokens[0].val}"`;
547+
}
548+
}
541549
}
542550
// @ts-ignore
543551
if (tokens[0].typ == EnumToken.StringTokenType) {

test/inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ const css = readFileSync(dirname(new URL(import.meta.url).pathname) + '/files/cs
77
const {code, stats} = await transform(css);
88

99
console.log(code);
10-
console.error({stats});
10+
console.log({stats});

test/specs/code/import2.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ export function run(describe, expect, transform, parse, render, dirname, readFil
33

44
const import1 = `@import 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.css';
55
`;
6-
describe('process import', function () {
7-
it('process import #2', function () {
6+
const import2 = `@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.css');
7+
`;
8+
9+
describe('process import #2', function () {
10+
it('process import #1', function () {
811
return readFile(dirname(new URL(import.meta.url).pathname) + '/../../files/result/font-awesome-all.css').
912
then(file => transform(import1, {
1013
minify: false,
@@ -14,4 +17,15 @@ export function run(describe, expect, transform, parse, render, dirname, readFil
1417
});
1518
});
1619

20+
describe('process import #2', function () {
21+
it('process import #2', function () {
22+
return readFile(dirname(new URL(import.meta.url).pathname) + '/../../files/result/font-awesome-all.css').
23+
then(file => transform(import2, {
24+
minify: false,
25+
resolveImport: true
26+
}).
27+
then((result) => expect(result.code).equals(file.trimEnd())));
28+
});
29+
});
30+
1731
}

0 commit comments

Comments
 (0)