Skip to content

Commit

Permalink
Merge a96b3b6 into acd76f0
Browse files Browse the repository at this point in the history
  • Loading branch information
macbre committed Jun 14, 2021
2 parents acd76f0 + a96b3b6 commit 7c871da
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ analyzer.prototype = {
}, this);
},

fixCss: function (css) {
// properly handle ; in @import URLs
// see https://github.com/macbre/analyze-css/pull/322
// see https://github.com/reworkcss/css/issues/137
return css.replace(/@import url([^)]+["'])/, (match) => {
return match.replace(/;/g, "%3B");
});
},

parseCss: function (css) {
var debug = require("debug")("analyze-css:parser");
debug("Going to parse %s kB of CSS", (css.length / 1024).toFixed(2));
Expand All @@ -180,6 +189,8 @@ analyzer.prototype = {
return this.error("Empty CSS was provided", analyzer.EXIT_EMPTY_CSS);
}

css = this.fixCss(css);

this.tree = cssParser(css, {
// errors are listed in the parsingErrors property instead of being thrown (#84)
silent: true,
Expand All @@ -190,6 +201,8 @@ analyzer.prototype = {
},

parseRules: function (rules) {
const debug = require("debug")("analyze-css:parseRules");

rules.forEach(function (rule) {
debug("%j", rule);

Expand Down Expand Up @@ -301,7 +314,9 @@ analyzer.prototype = {

// {"type":"import","import":"url('/css/styles.css')"}
case "import":
this.emit("import", rule.import);
// replace encoded semicolon back into ;
// https://github.com/macbre/analyze-css/pull/322
this.emit("import", rule.import.replace(/%3B/g, ";"));
break;
}
}, this);
Expand Down
22 changes: 22 additions & 0 deletions test/fixes/280-url-with-semicolons.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { describe, it } = require("@jest/globals");
const assert = require('assert');

describe('@import rule with url containing semicolon', () => {
it('is properly parsed', () => {
const analyzer = require('../..'),
css = `
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;600;800&display=swap');
body {
}
`.trim();

new analyzer(css, (err, res) => {
assert.ok(err === null, err);
assert.strictEqual(res.metrics.imports, 1, '@import is detected');
assert.strictEqual(res.metrics.selectors, 1, 'body {} is detected');
assert.deepStrictEqual(res.offenders.imports[0].message, "url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;600;800&display=swap')");
});
});
});

0 comments on commit 7c871da

Please sign in to comment.