Skip to content

Commit 93a7e0a

Browse files
committed
Replace CSS parser with postcss
https://www.npmjs.com/package/css doesn't support newer CSS features
1 parent e47352a commit 93a7e0a

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

server/cssfile.js

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var csslint = require('csslint').CSSLint;
2-
var cssparser = require('css');
2+
var cssparser = require('postcss');
33

44
function CssFile(source, path, callback){
55
callback = callback || function(){}
@@ -12,28 +12,25 @@ CssFile.prototype.webSrc = function(){
1212
};
1313

1414
CssFile.prototype.selectorFromPosition = function(line, column){
15-
var rules = this.parsed.stylesheet.rules;
16-
for(var i = 0; i < rules.length; i++){
17-
var position = rules[i].position;
18-
if((position.start.line < line && position.end.line > line)
19-
|| (position.start.line == line
20-
&& position.end.line != line
21-
&& position.start.column <= line)
22-
|| (position.start.line != line
23-
&& position.end.line == line
24-
&& position.start.column >= line)
25-
|| (position.start.line == line
26-
&& position.end.line == line
27-
&& position.start.column <= line
28-
&& position.end.column >= line)){
29-
if(rules[i].selectors){
30-
return rules[i].selectors.join(' ');
31-
}else{
32-
return null;
33-
}
15+
for (const rule of this.parsed.nodes) {
16+
const {
17+
start: { line: startLine, column: startColumn },
18+
end: { line: endLine, column: endColumn },
19+
} = rule.source
20+
if((startLine < line && endLine > line)
21+
|| (startLine == line
22+
&& endLine != line
23+
&& startColumn <= line)
24+
|| (startLine != line
25+
&& endLine == line
26+
&& startColumn >= line)
27+
|| (startLine == line
28+
&& endLine == line
29+
&& startColumn <= line
30+
&& endColumn >= line)){
31+
return rule.selector || null;
3432
}
3533
}
36-
3734
return null;
3835
};
3936

@@ -62,11 +59,12 @@ CssFile.prototype.setContent = function(source, callback){
6259
return;
6360
}
6461

65-
this.parsed.stylesheet.rules.forEach(function(rule){
66-
var position = rule.position;
67-
position.start.line--;
68-
position.start.column--;
69-
});
62+
for (const rule of this.parsed.nodes) {
63+
let source = rule.source;
64+
source.start.line--;
65+
source.start.column--;
66+
source.end.column++;
67+
}
7068

7169
if(changed){
7270
callback(null);

server/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
"description": "",
55
"main": "bracey.js",
66
"dependencies": {
7-
"css": "^2.2.1",
8-
"csslint": "^0.10.0",
7+
"csslint": "^1.0.5",
98
"domhandler": "^2.3.0",
109
"domutils": "^1.5.1",
1110
"htmlhint": "^0.11.0",
1211
"htmlparser2": "^3.9.0",
1312
"mime": "^1.3.4",
14-
"websocket": "^1.0.22"
13+
"postcss": "^7.0.32",
14+
"websocket": "^1.0.32"
1515
},
1616
"devDependencies": {
1717
"chai": "*",
18-
"mocha": "*"
18+
"mocha": "^8.1.3"
1919
},
2020
"scripts": {
2121
"test": "mocha"

server/test/cssfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('cssfile', function(){
2222
it('calls callback with errors', function(done){
2323
var invalidCss = 'body{ background: red color: white}';
2424
file = new cssfile(invalidCss, 'can be whatever', function(err){
25-
err.should.not.be.null;
25+
expect(err).to.not.be.null;
2626
done();
2727
});
2828
});

0 commit comments

Comments
 (0)