Skip to content

Commit

Permalink
Replace CSS parser with postcss
Browse files Browse the repository at this point in the history
https://www.npmjs.com/package/css doesn't support newer CSS features
  • Loading branch information
jdsutherland committed Sep 8, 2020
1 parent e47352a commit 93a7e0a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
50 changes: 24 additions & 26 deletions server/cssfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var csslint = require('csslint').CSSLint;
var cssparser = require('css');
var cssparser = require('postcss');

function CssFile(source, path, callback){
callback = callback || function(){}
Expand All @@ -12,28 +12,25 @@ CssFile.prototype.webSrc = function(){
};

CssFile.prototype.selectorFromPosition = function(line, column){
var rules = this.parsed.stylesheet.rules;
for(var i = 0; i < rules.length; i++){
var position = rules[i].position;
if((position.start.line < line && position.end.line > line)
|| (position.start.line == line
&& position.end.line != line
&& position.start.column <= line)
|| (position.start.line != line
&& position.end.line == line
&& position.start.column >= line)
|| (position.start.line == line
&& position.end.line == line
&& position.start.column <= line
&& position.end.column >= line)){
if(rules[i].selectors){
return rules[i].selectors.join(' ');
}else{
return null;
}
for (const rule of this.parsed.nodes) {
const {
start: { line: startLine, column: startColumn },
end: { line: endLine, column: endColumn },
} = rule.source
if((startLine < line && endLine > line)
|| (startLine == line
&& endLine != line
&& startColumn <= line)
|| (startLine != line
&& endLine == line
&& startColumn >= line)
|| (startLine == line
&& endLine == line
&& startColumn <= line
&& endColumn >= line)){
return rule.selector || null;
}
}

return null;
};

Expand Down Expand Up @@ -62,11 +59,12 @@ CssFile.prototype.setContent = function(source, callback){
return;
}

this.parsed.stylesheet.rules.forEach(function(rule){
var position = rule.position;
position.start.line--;
position.start.column--;
});
for (const rule of this.parsed.nodes) {
let source = rule.source;
source.start.line--;
source.start.column--;
source.end.column++;
}

if(changed){
callback(null);
Expand Down
8 changes: 4 additions & 4 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
"description": "",
"main": "bracey.js",
"dependencies": {
"css": "^2.2.1",
"csslint": "^0.10.0",
"csslint": "^1.0.5",
"domhandler": "^2.3.0",
"domutils": "^1.5.1",
"htmlhint": "^0.11.0",
"htmlparser2": "^3.9.0",
"mime": "^1.3.4",
"websocket": "^1.0.22"
"postcss": "^7.0.32",
"websocket": "^1.0.32"
},
"devDependencies": {
"chai": "*",
"mocha": "*"
"mocha": "^8.1.3"
},
"scripts": {
"test": "mocha"
Expand Down
2 changes: 1 addition & 1 deletion server/test/cssfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('cssfile', function(){
it('calls callback with errors', function(done){
var invalidCss = 'body{ background: red color: white}';
file = new cssfile(invalidCss, 'can be whatever', function(err){
err.should.not.be.null;
expect(err).to.not.be.null;
done();
});
});
Expand Down

0 comments on commit 93a7e0a

Please sign in to comment.