-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- new function `clone` to deep clone of AST node - new option `clone` for `compress()` to transform copy of input AST (useful in case of AST reuse)
- Loading branch information
Showing
5 changed files
with
110 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var List = require('./list'); | ||
|
||
module.exports = function clone(node) { | ||
var result = {}; | ||
|
||
for (var key in node) { | ||
var value = node[key]; | ||
|
||
if (value) { | ||
if (Array.isArray(value)) { | ||
value = value.slice(0); | ||
} else if (value instanceof List) { | ||
value = new List(value.map(clone)); | ||
} else if (value.constructor === Object) { | ||
value = clone(value); | ||
} | ||
} | ||
|
||
result[key] = value; | ||
} | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
var assert = require('assert'); | ||
var csso = require('../lib/index.js'); | ||
|
||
function sumMarker(ast) { | ||
var result = 0; | ||
|
||
csso.walk(ast, function(node) { | ||
result += node.marker; | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
describe('AST clone', function() { | ||
it('clone()', function() { | ||
var ast = csso.parse('.test{color:red;}@media foo{div{color:green}}'); | ||
var astCopy = csso.clone(ast); | ||
|
||
csso.walk(ast, function(node) { | ||
node.marker = 1; | ||
}); | ||
|
||
csso.walk(astCopy, function(node) { | ||
node.marker = 2; | ||
}); | ||
|
||
assert(sumMarker(ast) > 1); | ||
assert.equal(sumMarker(ast) * 2, sumMarker(astCopy)); | ||
}); | ||
|
||
it('compress(ast, { clone: false })', function() { | ||
var ast = csso.parse('.foo{color:red}.bar{color:#ff0000}'); | ||
var compressedAst = csso.compress(ast, { clone: false }).ast; | ||
|
||
assert.equal(csso.translate(compressedAst), '.bar,.foo{color:red}'); | ||
assert.equal(csso.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; | ||
|
||
assert.equal(csso.translate(compressedAst), '.bar,.foo{color:red}'); | ||
assert.equal(csso.translate(ast), '.foo{color:red}.bar{color:#ff0000}'); | ||
}); | ||
}); |