From 129101533e75e4bc151e9f8fb69b4a98103ad7e7 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Fri, 19 Sep 2014 10:07:31 +0200 Subject: [PATCH] Stringify --- build/node.js | 68 ++++++++++++++++++++++++++++++++++++++++-- build/web.js | 67 ++++++++++++++++++++++++++++++++++++++++- gruntfile.js | 4 +-- index.js | 3 +- lib/parser.js | 2 +- lib/stringify.js | 62 ++++++++++++++++++++++++++++++++++++++ spec/parser.spec.js | 2 +- spec/stringify.spec.js | 26 ++++++++++++++++ webify.js | 1 + 9 files changed, 227 insertions(+), 8 deletions(-) create mode 100644 lib/stringify.js create mode 100644 spec/stringify.spec.js create mode 100644 webify.js diff --git a/build/node.js b/build/node.js index fcd0bcb..0ee01c2 100644 --- a/build/node.js +++ b/build/node.js @@ -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; diff --git a/build/web.js b/build/web.js index de8fd61..5371d8f 100644 --- a/build/web.js +++ b/build/web.js @@ -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); + }; +})(); diff --git a/gruntfile.js b/gruntfile.js index c9e35cf..c75e223 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -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'] } } } diff --git a/index.js b/index.js index e007297..d8e7f56 100644 --- a/index.js +++ b/index.js @@ -1 +1,2 @@ -exports.parse = (GradientParser || {}).parse; +exports.parse = GradientParser.parse; +exports.stringify = GradientParser.stringify; diff --git a/lib/parser.js b/lib/parser.js index de8fd61..e8df197 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -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() { diff --git a/lib/stringify.js b/lib/stringify.js new file mode 100644 index 0000000..a5eb01f --- /dev/null +++ b/lib/stringify.js @@ -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); + }; +})(); diff --git a/spec/parser.spec.js b/spec/parser.spec.js index 2e842d4..4eda773 100644 --- a/spec/parser.spec.js +++ b/spec/parser.spec.js @@ -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; diff --git a/spec/stringify.spec.js b/spec/stringify.spec.js new file mode 100644 index 0000000..59affd7 --- /dev/null +++ b/spec/stringify.spec.js @@ -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); + }); + + }); + +}); diff --git a/webify.js b/webify.js new file mode 100644 index 0000000..07b9608 --- /dev/null +++ b/webify.js @@ -0,0 +1 @@ +var GradientParser = (window.GradientParser || {});