Skip to content

Commit

Permalink
failed to flatten @import when using url() syntax #33
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed May 4, 2024
1 parent 46b8379 commit 89db057
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## V0.5.1
- [x] failed to flatten @import when using url() syntax
-
## V0.5.0
- [x] render node with parents
- [x] fix relative color from xyz
Expand Down
16 changes: 10 additions & 6 deletions dist/index-umd-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -6371,12 +6371,16 @@
}
if (atRule.val == 'import') {
// @ts-ignore
if (tokens[0].typ == exports.EnumToken.UrlFunctionTokenType && tokens[1].typ == exports.EnumToken.UrlTokenTokenType) {
tokens.shift();
// @ts-ignore
tokens[0].typ = exports.EnumToken.StringTokenType;
// @ts-ignore
tokens[0].val = `"${tokens[0].val}"`;
if (tokens[0].typ == exports.EnumToken.UrlFunctionTokenType) {
if (tokens[1].typ == exports.EnumToken.UrlTokenTokenType || tokens[1].typ == exports.EnumToken.StringTokenType) {
tokens.shift();
if (tokens[1].typ == exports.EnumToken.UrlTokenTokenType) {
// @ts-ignore
tokens[0].typ = exports.EnumToken.StringTokenType;
// @ts-ignore
tokens[0].val = `"${tokens[0].val}"`;
}
}
}
// @ts-ignore
if (tokens[0].typ == exports.EnumToken.StringTokenType) {
Expand Down
16 changes: 10 additions & 6 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6369,12 +6369,16 @@ async function parseNode(results, context, stats, options, errors, src, map) {
}
if (atRule.val == 'import') {
// @ts-ignore
if (tokens[0].typ == exports.EnumToken.UrlFunctionTokenType && tokens[1].typ == exports.EnumToken.UrlTokenTokenType) {
tokens.shift();
// @ts-ignore
tokens[0].typ = exports.EnumToken.StringTokenType;
// @ts-ignore
tokens[0].val = `"${tokens[0].val}"`;
if (tokens[0].typ == exports.EnumToken.UrlFunctionTokenType) {
if (tokens[1].typ == exports.EnumToken.UrlTokenTokenType || tokens[1].typ == exports.EnumToken.StringTokenType) {
tokens.shift();
if (tokens[1].typ == exports.EnumToken.UrlTokenTokenType) {
// @ts-ignore
tokens[0].typ = exports.EnumToken.StringTokenType;
// @ts-ignore
tokens[0].val = `"${tokens[0].val}"`;
}
}
}
// @ts-ignore
if (tokens[0].typ == exports.EnumToken.StringTokenType) {
Expand Down
16 changes: 10 additions & 6 deletions dist/lib/parser/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,16 @@ async function parseNode(results, context, stats, options, errors, src, map) {
}
if (atRule.val == 'import') {
// @ts-ignore
if (tokens[0].typ == EnumToken.UrlFunctionTokenType && tokens[1].typ == EnumToken.UrlTokenTokenType) {
tokens.shift();
// @ts-ignore
tokens[0].typ = EnumToken.StringTokenType;
// @ts-ignore
tokens[0].val = `"${tokens[0].val}"`;
if (tokens[0].typ == EnumToken.UrlFunctionTokenType) {
if (tokens[1].typ == EnumToken.UrlTokenTokenType || tokens[1].typ == EnumToken.StringTokenType) {
tokens.shift();
if (tokens[1].typ == EnumToken.UrlTokenTokenType) {
// @ts-ignore
tokens[0].typ = EnumToken.StringTokenType;
// @ts-ignore
tokens[0].val = `"${tokens[0].val}"`;
}
}
}
// @ts-ignore
if (tokens[0].typ == EnumToken.StringTokenType) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@tbela99/css-parser",
"description": "CSS parser for node and the browser",
"version": "0.5.0",
"version": "0.5.1",
"exports": {
".": "./dist/node/index.js",
"./umd": "./dist/index-umd-web.js",
Expand Down
20 changes: 14 additions & 6 deletions src/lib/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,20 @@ async function parseNode(results: TokenizeResult[], context: AstRuleList, stats:
if (atRule.val == 'import') {

// @ts-ignore
if (tokens[0].typ == EnumToken.UrlFunctionTokenType && tokens[1].typ == EnumToken.UrlTokenTokenType) {
tokens.shift();
// @ts-ignore
tokens[0].typ = EnumToken.StringTokenType;
// @ts-ignore
tokens[0].val = `"${tokens[0].val}"`;
if (tokens[0].typ == EnumToken.UrlFunctionTokenType) {

if (tokens[1].typ == EnumToken.UrlTokenTokenType || tokens[1].typ == EnumToken.StringTokenType) {

tokens.shift();

if (tokens[1].typ == EnumToken.UrlTokenTokenType) {

// @ts-ignore
tokens[0].typ = EnumToken.StringTokenType;
// @ts-ignore
tokens[0].val = `"${tokens[0].val}"`;
}
}
}
// @ts-ignore
if (tokens[0].typ == EnumToken.StringTokenType) {
Expand Down
2 changes: 1 addition & 1 deletion test/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ const css = readFileSync(dirname(new URL(import.meta.url).pathname) + '/files/cs
const {code, stats} = await transform(css);

console.log(code);
console.error({stats});
console.log({stats});
18 changes: 16 additions & 2 deletions test/specs/code/import2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ export function run(describe, expect, transform, parse, render, dirname, readFil

const import1 = `@import 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.css';
`;
describe('process import', function () {
it('process import #2', function () {
const import2 = `@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.css');
`;

describe('process import #2', function () {
it('process import #1', function () {
return readFile(dirname(new URL(import.meta.url).pathname) + '/../../files/result/font-awesome-all.css').
then(file => transform(import1, {
minify: false,
Expand All @@ -14,4 +17,15 @@ export function run(describe, expect, transform, parse, render, dirname, readFil
});
});

describe('process import #2', function () {
it('process import #2', function () {
return readFile(dirname(new URL(import.meta.url).pathname) + '/../../files/result/font-awesome-all.css').
then(file => transform(import2, {
minify: false,
resolveImport: true
}).
then((result) => expect(result.code).equals(file.trimEnd())));
});
});

}

0 comments on commit 89db057

Please sign in to comment.