Skip to content

Replace CSS parser with postcss #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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++;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't fully understand why this was done previously so I made this equivalent.

}

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