Skip to content

Commit

Permalink
hide all csstree methods behind syntax property and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Feb 28, 2017
1 parent 9dbdfc8 commit a67d1e9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 49 deletions.
31 changes: 7 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
var parse = require('css-tree').parse;
var csstree = require('css-tree');
var parse = csstree.parse;
var compress = require('./compressor');
var translate = require('css-tree').translate;
var translateWithSourceMap = require('css-tree').translateWithSourceMap;
var walk = require('css-tree').walk;
var walkUp = require('css-tree').walkUp;
var walkRules = require('css-tree').walkRules;
var walkRulesRight = require('css-tree').walkRulesRight;
var clone = require('css-tree').clone;
var List = require('css-tree').List;
var translate = csstree.translate;
var translateWithSourceMap = csstree.translateWithSourceMap;

function debugOutput(name, options, startTime, data) {
if (options.debug) {
Expand Down Expand Up @@ -136,25 +131,13 @@ function minifyBlock(source, options) {
module.exports = {
version: require('../package.json').version,

// classes
List: List,

// main methods
minify: minifyStylesheet,
minifyBlock: minifyBlock,

// step by step
parse: parse,
// compress an AST
compress: compress,
translate: translate,
translateWithSourceMap: translateWithSourceMap,

// walkers
walk: walk,
walkUp: walkUp,
walkRules: walkRules,
walkRulesRight: walkRulesRight,

// utils
clone: clone
// css syntax parser/walkers/generator/etc
syntax: csstree
};
21 changes: 12 additions & 9 deletions test/clone.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
var assert = require('assert');
var csso = require('../lib/index.js');
var csso = require('../lib');
var parse = csso.syntax.parse;
var compress = csso.compress;
var translate = csso.syntax.translate;

describe('AST clone', function() {
it('compress(ast, { clone: false })', function() {
var ast = csso.parse('.foo{color:red}.bar{color:#ff0000}');
var compressedAst = csso.compress(ast, { clone: false }).ast;
var ast = parse('.foo{color:red}.bar{color:#ff0000}');
var compressedAst = compress(ast, { clone: false }).ast;

assert.equal(csso.translate(compressedAst), '.bar,.foo{color:red}');
assert.equal(csso.translate(ast), '.bar,.foo{color:red}');
assert.equal(translate(compressedAst), '.bar,.foo{color:red}');
assert.equal(translate(ast), '.bar,.foo{color:red}');
});

it('compress(ast, { clone: true })', function() {
var ast = csso.parse('.foo{color:red}.bar{color:#ff0000}');
var compressedAst = csso.compress(ast, { clone: true }).ast;
var ast = parse('.foo{color:red}.bar{color:#ff0000}');
var compressedAst = compress(ast, { clone: true }).ast;

assert.equal(csso.translate(compressedAst), '.bar,.foo{color:red}');
assert.equal(csso.translate(ast), '.foo{color:red}.bar{color:#ff0000}');
assert.equal(translate(compressedAst), '.bar,.foo{color:red}');
assert.equal(translate(ast), '.foo{color:red}.bar{color:#ff0000}');
});
});
20 changes: 11 additions & 9 deletions test/compress.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var path = require('path');
var assert = require('assert');
var csso = require('../lib/index.js');
var translate = require('css-tree').translate;
var csso = require('../lib');
var parse = csso.syntax.parse;
var compress = csso.compress;
var translate = csso.syntax.translate;
var tests = require('./fixture/compress');

function normalize(str) {
Expand All @@ -14,8 +16,8 @@ function createCompressTest(name, test) {

assert.equal(normalize(compressed.css), normalize(test.compressed), 'compress by minify()');

var ast = csso.parse(test.source);
var compressedAst = csso.compress(ast).ast;
var ast = parse(test.source);
var compressedAst = compress(ast).ast;
var css = translate(compressedAst);

assert.equal(normalize(css), normalize(test.compressed), 'compress step by step');
Expand All @@ -35,15 +37,15 @@ describe('compress', function() {

describe('should return the same ast as input by default', function() {
it('compress stylesheet', function() {
var ast = csso.parse('.test{color:red}');
var resultAst = csso.compress(ast).ast;
var ast = parse('.test{color:red}');
var resultAst = compress(ast).ast;

assert(ast === resultAst);
});

it('compress block', function() {
var ast = csso.parse('color:#ff0000;width:1px', { context: 'declarationList' });
var resultAst = csso.compress(ast).ast;
var ast = parse('color:#ff0000;width:1px', { context: 'declarationList' });
var resultAst = compress(ast).ast;

assert(ast === resultAst);
assert.equal(translate(ast), 'color:red;width:1px');
Expand Down Expand Up @@ -171,6 +173,6 @@ describe('compress', function() {
});

it('should not fail if no ast passed', function() {
assert.equal(translate(csso.compress().ast, true), '');
assert.equal(translate(compress().ast, true), '');
});
});
2 changes: 1 addition & 1 deletion test/plugins.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var assert = require('assert');
var csso = require('../lib/index.js');
var csso = require('../lib');
var css = '.test{color:red;}@media foo{div{color:green}}';

describe('plugins', function() {
Expand Down
7 changes: 4 additions & 3 deletions test/sourceMaps.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fs = require('fs');
var assert = require('assert');
var csso = require('../lib/index.js');
var csso = require('../lib');
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var css = '.a { color: #ff0000; }\n.b { display: block; float: left; }';
var minifiedCss = '.a{color:red}.b{display:block;float:left}';
Expand Down Expand Up @@ -46,7 +46,7 @@ function getGeneratedPosition(str, source) {
}

function defineSourceMap(filename) {
var string = '{"version":3,"sources":["' + filename + '"],"names":[],"mappings":"AAAA,E,CAAK,S,CACL,E,CAAK,a,CAAgB,U","file":"' + filename + '","sourcesContent":[' + JSON.stringify(css) + ']}';
var string = '{"version":3,"sources":["' + filename + '"],"names":[],"mappings":"AAAA,E,CAAK,S,CACL,E,CAAK,cAAgB,U","file":"' + filename + '","sourcesContent":[' + JSON.stringify(css) + ']}';
var base64 = new Buffer(string, 'utf8').toString('base64');
var inline = '/*# sourceMappingURL=data:application/json;base64,' + base64 + ' */';

Expand Down Expand Up @@ -76,6 +76,7 @@ describe('sourceMaps', function() {
assert('map' in result, 'should has `map` property');
assert.equal(result.css, minifiedCss);
assert.equal(result.map.toString(), anonymousMap.string);

});

it('should use passed filename in map', function() {
Expand All @@ -99,7 +100,7 @@ describe('sourceMaps', function() {
assert.equal(result.css,
'/*! check location merge */\n.a{a:2;b:2}\n' +
'/*! several exlamation comments */\n.foo{color:red}');
assert.equal(result.map.toString(), '{"version":3,"sources":["<unknown>"],"names":[],"mappings":";AAA2B,E,CAAQ,G,CAAS,G;;AAAsC,I,CAAO,S","file":"<unknown>","sourcesContent":[' + JSON.stringify(css) + ']}');
assert.equal(result.map.toString(), '{"version":3,"sources":["<unknown>"],"names":[],"mappings":";AAA2B,E,CAAQ,IAAS,G;;AAAsC,I,CAAO,S","file":"<unknown>","sourcesContent":[' + JSON.stringify(css) + ']}');
});

describe('check positions', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/specificity.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var fs = require('fs');
var assert = require('assert');
var csso = require('../lib/index.js');
var csso = require('../lib');
var specificity = require('../lib/compressor/restructure/prepare/specificity.js');

function createSpecificityTest(test) {
it(test.selector, function() {
var ast = csso.parse(test.selector, {
var ast = csso.syntax.parse(test.selector, {
context: 'selector'
});

Expand Down
2 changes: 1 addition & 1 deletion test/usage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var assert = require('assert');
var csso = require('../lib/index.js');
var csso = require('../lib');
var tests = require('./fixture/usage');

function normalize(str) {
Expand Down

0 comments on commit a67d1e9

Please sign in to comment.