Stringify

This commit is contained in:
Rafael Caricio 2014-09-19 10:07:31 +02:00
parent 75e01de8e4
commit 129101533e
9 changed files with 227 additions and 8 deletions

View file

@ -2,7 +2,70 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var GradientParser = {};
var GradientParser = (GradientParser || {});
GradientParser.stringify = (function() {
var visitor = {
'visit_linear-gradient': function(node) {
var orientation = visitor.visit(node.orientation);
if (orientation) {
orientation += ', ';
}
return 'linear-gradient('+ orientation + visitor.visit(node.colorStops) + ')';
},
visit_literal: function(node) {
return node.value;
},
visit_array: function(elements) {
var result = '',
size = elements.length;
elements.forEach(function(element, i) {
result += visitor.visit(element);
if (i < size - 1) {
result += ', ';
}
});
return result;
},
visit: function(element) {
if (!element) {
return '';
}
if (element instanceof Array) {
return visitor.visit_array(element, result);
} else if (element.type) {
var nodeVisitor = visitor['visit_' + element.type];
if (nodeVisitor) {
return nodeVisitor(element);
} else {
throw Error('Missing visitor visit_' + element.type);
}
} else {
throw Error('Invalid node.');
}
}
};
return function(root) {
return visitor.visit(root);
};
})();
// Copyright (c) 2014 Rafael Caricio. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var GradientParser = (GradientParser || {});
GradientParser.parse = (function() {
@ -336,4 +399,5 @@ GradientParser.parse = (function() {
};
})();
exports.parse = (GradientParser || {}).parse;
exports.parse = GradientParser.parse;
exports.stringify = GradientParser.stringify;

View file

@ -1,8 +1,10 @@
var GradientParser = (window.GradientParser || {});
// Copyright (c) 2014 Rafael Caricio. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var GradientParser = {};
var GradientParser = (GradientParser || {});
GradientParser.parse = (function() {
@ -335,3 +337,66 @@ GradientParser.parse = (function() {
return getAST();
};
})();
// Copyright (c) 2014 Rafael Caricio. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var GradientParser = (GradientParser || {});
GradientParser.stringify = (function() {
var visitor = {
'visit_linear-gradient': function(node) {
var orientation = visitor.visit(node.orientation);
if (orientation) {
orientation += ', ';
}
return 'linear-gradient('+ orientation + visitor.visit(node.colorStops) + ')';
},
visit_literal: function(node) {
return node.value;
},
visit_array: function(elements) {
var result = '',
size = elements.length;
elements.forEach(function(element, i) {
result += visitor.visit(element);
if (i < size - 1) {
result += ', ';
}
});
return result;
},
visit: function(element) {
if (!element) {
return '';
}
if (element instanceof Array) {
return visitor.visit_array(element, result);
} else if (element.type) {
var nodeVisitor = visitor['visit_' + element.type];
if (nodeVisitor) {
return nodeVisitor(element);
} else {
throw Error('Missing visitor visit_' + element.type);
}
} else {
throw Error('Invalid node.');
}
}
};
return function(root) {
return visitor.visit(root);
};
})();

View file

@ -19,8 +19,8 @@ module.exports = function (grunt) {
concat: {
release: {
files: {
'build/node.js': ['lib/parser.js', 'index.js'],
'build/web.js': ['lib/parser.js']
'build/node.js': ['lib/stringify.js', 'lib/parser.js', 'index.js'],
'build/web.js': ['webify.js', 'lib/parser.js', 'lib/stringify.js']
}
}
}

View file

@ -1 +1,2 @@
exports.parse = (GradientParser || {}).parse;
exports.parse = GradientParser.parse;
exports.stringify = GradientParser.stringify;

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var GradientParser = {};
var GradientParser = (GradientParser || {});
GradientParser.parse = (function() {

62
lib/stringify.js Normal file
View file

@ -0,0 +1,62 @@
// Copyright (c) 2014 Rafael Caricio. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var GradientParser = (GradientParser || {});
GradientParser.stringify = (function() {
var visitor = {
'visit_linear-gradient': function(node) {
var orientation = visitor.visit(node.orientation);
if (orientation) {
orientation += ', ';
}
return 'linear-gradient('+ orientation + visitor.visit(node.colorStops) + ')';
},
visit_literal: function(node) {
return node.value;
},
visit_array: function(elements) {
var result = '',
size = elements.length;
elements.forEach(function(element, i) {
result += visitor.visit(element);
if (i < size - 1) {
result += ', ';
}
});
return result;
},
visit: function(element) {
if (!element) {
return '';
}
if (element instanceof Array) {
return visitor.visit_array(element, result);
} else if (element.type) {
var nodeVisitor = visitor['visit_' + element.type];
if (nodeVisitor) {
return nodeVisitor(element);
} else {
throw Error('Missing visitor visit_' + element.type);
}
} else {
throw Error('Invalid node.');
}
}
};
return function(root) {
return visitor.visit(root);
};
})();

View file

@ -4,7 +4,7 @@ var expect = require('expect.js');
var gradients = require('build/node');
describe('gradient-parser.js', function () {
describe('lib/parser.js', function () {
var ast,
subject;

26
spec/stringify.spec.js Normal file
View file

@ -0,0 +1,26 @@
'use strict';
var expect = require('expect.js');
var gradients = require('build/node');
describe('lib/stringify.js', function () {
var subject;
it('should exist', function () {
expect(typeof gradients.stringify).to.equal('function');
});
describe('serialization', function() {
it('if tree is null', function() {
expect(gradients.stringify(null)).to.equal('');
});
it('should serialize a simple gradient', function() {
var gradientDef = 'linear-gradient(black, white)';
expect(gradients.stringify(gradients.parse(gradientDef))).to.equal(gradientDef);
});
});
});

1
webify.js Normal file
View file

@ -0,0 +1 @@
var GradientParser = (window.GradientParser || {});