Comparing version 0.0.2 to 0.0.3
@@ -1,2 +0,9 @@ | ||
### 0.0.2 / 28.09.2012 | ||
### [[>](//github.com/deepsweet/svgo/tree/v0.0.1)] 0.0.3 / 29.09.2012 | ||
* plugins/collapseGroups bugfix | ||
* plugins/moveElemsAttrsToGroup bugfix | ||
* svgo now display --help if running w/o arguments | ||
* massive jsdoc updates | ||
* plugins engine main filter function optimization | ||
### [[>](//github.com/deepsweet/svgo/tree/v0.0.2)] 0.0.2 / 28.09.2012 | ||
* add --disable and --enable command line options | ||
@@ -6,3 +13,3 @@ * add an empty values rejecting to coa.js | ||
### 0.0.1 / 27.09.2012 | ||
* initial public version | ||
### [[>](//github.com/deepsweet/svgo/tree/v0.0.1)] 0.0.1 / 27.09.2012 | ||
* initial public version |
var Q = require('q'), | ||
info = JSON.parse(require('fs').readFileSync(__dirname + '/../package.json')); | ||
var coa = module.exports = require('coa').Cmd() | ||
/** | ||
* Command-Option-Argument. | ||
* | ||
* @see https://github.com/veged/coa | ||
* | ||
* @module coa | ||
*/ | ||
module.exports = require('coa').Cmd() | ||
.helpful() | ||
@@ -21,3 +28,3 @@ .name(info.name) | ||
.val(function(val) { | ||
return val || coa.reject('Option --config must have a value.'); | ||
return val || this.reject('Option --config must have a value.'); | ||
}) | ||
@@ -30,3 +37,3 @@ .end() | ||
.val(function(val) { | ||
return val || coa.reject('Option --disable must have a value.'); | ||
return val || this.reject('Option --disable must have a value.'); | ||
}) | ||
@@ -39,3 +46,3 @@ .end() | ||
.val(function(val) { | ||
return val || coa.reject('Option --enable must have a value.'); | ||
return val || this.reject('Option --enable must have a value.'); | ||
}) | ||
@@ -59,2 +66,5 @@ .end() | ||
// if run as just 'svgo' display help and exit | ||
if (options.input.isTTY) return this.usage(); | ||
options.input | ||
@@ -61,0 +71,0 @@ .on('data', function(chunk) { |
@@ -5,2 +5,10 @@ var QFS = require('q-fs'), | ||
/** | ||
* Read and/or extend default config file. | ||
* | ||
* @module config | ||
* | ||
* @param {Object} [options] options | ||
* @return {Object} config deferred promise | ||
*/ | ||
module.exports = function(options) { | ||
@@ -11,3 +19,3 @@ | ||
// if there are no any options then return default config | ||
if (!options) return readConfig(defaultConfigPath); | ||
if (!options) return _readConfig(defaultConfigPath); | ||
@@ -19,10 +27,10 @@ // COA options | ||
return readConfig(defaultConfigPath) | ||
return _readConfig(defaultConfigPath) | ||
.then(function(defaultConfig) { | ||
// --disable | ||
if (options.disable) return pluginsFromCOA(defaultConfig, options.disable, false); | ||
if (options.disable) return _extendPluginsFromCOA(defaultConfig, options.disable, false); | ||
// --enable | ||
if (options.enable) return pluginsFromCOA(defaultConfig, options.enable, true); | ||
if (options.enable) return _extendPluginsFromCOA(defaultConfig, options.enable, true); | ||
@@ -44,3 +52,3 @@ // --config | ||
// if it exists then return extended default | ||
return readConfig(localConfigPath) | ||
return _readConfig(localConfigPath) | ||
.then(function(localConfig) { | ||
@@ -61,3 +69,3 @@ return extend(true, defaultConfig, localConfig); | ||
// return extended default | ||
return readConfig(defaultConfigPath) | ||
return _readConfig(defaultConfigPath) | ||
.then(function(defaultConfig) { | ||
@@ -73,3 +81,10 @@ | ||
function readConfig(path) { | ||
/** | ||
* Read and JSON.parse config file. | ||
* | ||
* @param {String} path config path | ||
* @return {Object} read config deferred promise | ||
* @private | ||
*/ | ||
function _readConfig(path) { | ||
@@ -83,3 +98,12 @@ return QFS.read(path) | ||
function pluginsFromCOA(defaultConfig, names, active) { | ||
/** | ||
* Extend plugins from COA's --disable and --enable opts. | ||
* | ||
* @param {Object} defaultConfig default config object | ||
* @param {Array} names current plugins array | ||
* @param {Boolean} active disable or enable plugin? | ||
* @return {Object} extended config | ||
* @private | ||
*/ | ||
function _extendPluginsFromCOA(defaultConfig, names, active) { | ||
@@ -86,0 +110,0 @@ var plugins = defaultConfig.plugins; |
@@ -6,9 +6,11 @@ var INHERIT = require('inherit'); | ||
* | ||
* @param {Object} data svg-as-js data object | ||
* @module js2svg | ||
* | ||
* @param {Object} jsdata input data | ||
* @param {Class} [converter] custom converter class | ||
* @return {Converter} Converter instance | ||
* @return {Object} output data | ||
*/ | ||
module.exports = function(data, converter) { | ||
module.exports = function(jsdata, converter) { | ||
return new (converter || Converter)(data).run(data); | ||
return new (converter || Converter)().run(jsdata); | ||
@@ -20,204 +22,199 @@ }; | ||
*/ | ||
var Converter = exports.Converter = INHERIT( | ||
/** | ||
* @lends Nodes.prototype | ||
*/ | ||
{ | ||
var Converter = exports.Converter = INHERIT(/** @lends Nodes.prototype */{ | ||
/** | ||
* @constructs | ||
* @private | ||
*/ | ||
__constructor: function() { | ||
/** | ||
* @constructs | ||
* @private | ||
* Converter options. | ||
* | ||
* @type {Object} | ||
*/ | ||
__constructor: function() { | ||
this.options = { | ||
doctypeStart: '<!DOCTYPE', | ||
doctypeEnd: '>', | ||
/** | ||
* Converter options. | ||
* | ||
* @type {Object} | ||
*/ | ||
this.options = { | ||
doctypeStart: '<!DOCTYPE', | ||
doctypeEnd: '>', | ||
procInstStart: '<?', | ||
procInstEnd: '?>', | ||
procInstStart: '<?', | ||
procInstEnd: '?>', | ||
tagOpenStart: '<', | ||
tagOpenEnd: '>', | ||
tagCloseStart: '</', | ||
tagCloseEnd: '>', | ||
tagOpenStart: '<', | ||
tagOpenEnd: '>', | ||
tagCloseStart: '</', | ||
tagCloseEnd: '>', | ||
tagShortStart: '<', | ||
tagShortEnd: '/>', | ||
tagShortStart: '<', | ||
tagShortEnd: '/>', | ||
attrStart: '="', | ||
attrEnd: '"', | ||
attrStart: '="', | ||
attrEnd: '"', | ||
commentStart: '<!--', | ||
commentEnd: '-->', | ||
commentStart: '<!--', | ||
commentEnd: '-->', | ||
cdataStart: '<![CDATA[', | ||
cdataEnd: ']]>' | ||
}; | ||
cdataStart: '<![CDATA[', | ||
cdataEnd: ']]>' | ||
}; | ||
}, | ||
}, | ||
/** | ||
* Start conversion. | ||
* | ||
* @param {Object} svg-as-js data object | ||
* @return {String} | ||
*/ | ||
run: function(data) { | ||
/** | ||
* Start conversion. | ||
* | ||
* @param {Object} svg-as-js data object | ||
* @return {String} | ||
*/ | ||
run: function(data) { | ||
var svg = ''; | ||
var svg = ''; | ||
if (data.content) { | ||
if (data.content) { | ||
data.content.forEach(function(item) { | ||
data.content.forEach(function(item) { | ||
if (item.isElem()) { | ||
svg += this.createElem(item); | ||
} else if (item.isText()) { | ||
svg += item.text; | ||
} else if (item.isDoctype()) { | ||
svg += this.createDoctype(item.doctype); | ||
} else if (item.isProcInst()) { | ||
svg += this.createProcInst(item.processinginstruction); | ||
} else if (item.isComment()) { | ||
svg += this.createComment(item.comment); | ||
} else if (item.isCDATA()) { | ||
svg += this.createCDATA(item.cdata); | ||
} | ||
if (item.isElem()) { | ||
svg += this.createElem(item); | ||
} else if (item.isText()) { | ||
svg += item.text; | ||
} else if (item.isDoctype()) { | ||
svg += this.createDoctype(item.doctype); | ||
} else if (item.isProcInst()) { | ||
svg += this.createProcInst(item.processinginstruction); | ||
} else if (item.isComment()) { | ||
svg += this.createComment(item.comment); | ||
} else if (item.isCDATA()) { | ||
svg += this.createCDATA(item.cdata); | ||
} | ||
}, this); | ||
}, this); | ||
} | ||
} | ||
return svg; | ||
return svg; | ||
}, | ||
}, | ||
/** | ||
* Create doctype tag. | ||
* | ||
* @param {String} doctype doctype body string | ||
* @return {String} | ||
*/ | ||
createDoctype: function(doctype) { | ||
/** | ||
* Create doctype tag. | ||
* | ||
* @param {String} doctype doctype body string | ||
* @return {String} | ||
*/ | ||
createDoctype: function(doctype) { | ||
return this.options.doctypeStart + | ||
doctype + | ||
this.options.doctypeEnd; | ||
return this.options.doctypeStart + | ||
doctype + | ||
this.options.doctypeEnd; | ||
}, | ||
}, | ||
/** | ||
* Create XML Processing Instruction tag. | ||
* | ||
* @param {Object} instruction instruction object | ||
* @return {String} | ||
*/ | ||
createProcInst: function(instruction) { | ||
/** | ||
* Create XML Processing Instruction tag. | ||
* | ||
* @param {Object} instruction instruction object | ||
* @return {String} | ||
*/ | ||
createProcInst: function(instruction) { | ||
return this.options.procInstStart + | ||
instruction.name + | ||
' ' + | ||
instruction.body + | ||
this.options.procInstEnd; | ||
return this.options.procInstStart + | ||
instruction.name + | ||
' ' + | ||
instruction.body + | ||
this.options.procInstEnd; | ||
}, | ||
}, | ||
/** | ||
* Create comment tag. | ||
* | ||
* @param {String} comment comment body | ||
* @return {String} | ||
*/ | ||
createComment: function(comment) { | ||
/** | ||
* Create comment tag. | ||
* | ||
* @param {String} comment comment body | ||
* @return {String} | ||
*/ | ||
createComment: function(comment) { | ||
return this.options.commentStart + | ||
comment + | ||
this.options.commentEnd; | ||
return this.options.commentStart + | ||
comment + | ||
this.options.commentEnd; | ||
}, | ||
}, | ||
/** | ||
* Create CDATA section. | ||
* | ||
* @param {String} cdata CDATA body | ||
* @return {String} | ||
*/ | ||
createCDATA: function(cdata) { | ||
/** | ||
* Create CDATA section. | ||
* | ||
* @param {String} cdata CDATA body | ||
* @return {String} | ||
*/ | ||
createCDATA: function(cdata) { | ||
return this.options.cdataStart + | ||
cdata + | ||
this.options.cdataEnd; | ||
return this.options.cdataStart + | ||
cdata + | ||
this.options.cdataEnd; | ||
}, | ||
}, | ||
/** | ||
* Create element tag. | ||
* | ||
* @param {Object} data element object | ||
* @return {String} | ||
*/ | ||
createElem: function(data) { | ||
/** | ||
* Create element tag. | ||
* | ||
* @param {Object} data element object | ||
* @return {String} | ||
*/ | ||
createElem: function(data) { | ||
// empty element and short tag | ||
if (data.isEmpty()) { | ||
// empty element and short tag | ||
if (data.isEmpty()) { | ||
return this.options.tagShortStart + | ||
data.elem + | ||
this.createAttrs(data) + | ||
this.options.tagShortEnd; | ||
return this.options.tagShortStart + | ||
data.elem + | ||
this.createAttrs(data) + | ||
this.options.tagShortEnd; | ||
// non-empty element | ||
} else { | ||
// non-empty element | ||
} else { | ||
return this.options.tagOpenStart + | ||
data.elem + | ||
this.createAttrs(data) + | ||
this.options.tagOpenEnd + | ||
this.run(data) + | ||
this.options.tagCloseStart + | ||
data.elem + | ||
this.options.tagCloseEnd; | ||
return this.options.tagOpenStart + | ||
data.elem + | ||
this.createAttrs(data) + | ||
this.options.tagOpenEnd + | ||
this.run(data) + | ||
this.options.tagCloseStart + | ||
data.elem + | ||
this.options.tagCloseEnd; | ||
} | ||
} | ||
}, | ||
}, | ||
/** | ||
* Create element attributes. | ||
* | ||
* @param {Object} elem attributes object | ||
* @return {String} | ||
*/ | ||
createAttrs: function(elem) { | ||
/** | ||
* Create element attributes. | ||
* | ||
* @param {Object} elem attributes object | ||
* @return {String} | ||
*/ | ||
createAttrs: function(elem) { | ||
var self = this, | ||
attrs = ''; | ||
var self = this, | ||
attrs = ''; | ||
if (elem.hasAttr()) { | ||
if (elem.hasAttr()) { | ||
elem.eachAttr(function(attr) { | ||
elem.eachAttr(function(attr) { | ||
attrs += ' ' + | ||
attr.name + | ||
self.options.attrStart + | ||
attr.value + | ||
self.options.attrEnd; | ||
attrs += ' ' + | ||
attr.name + | ||
self.options.attrStart + | ||
attr.value + | ||
self.options.attrEnd; | ||
}); | ||
}); | ||
} | ||
} | ||
return attrs; | ||
return attrs; | ||
} | ||
} | ||
}); | ||
} | ||
); | ||
/* | ||
@@ -224,0 +221,0 @@ var MyConv = INHERIT(Converter, { |
351
lib/jsAPI.js
@@ -5,246 +5,243 @@ var INHERIT = require('inherit'), | ||
/** | ||
* @module jsAPI | ||
* | ||
* @class SVG-as-JS Nodes API. | ||
*/ | ||
exports.Nodes = INHERIT( | ||
exports.Nodes = INHERIT(/** @lends Nodes.prototype */{ | ||
/** | ||
* @lends Nodes.prototype | ||
* @constructs | ||
* @private | ||
*/ | ||
{ | ||
__constructor: function(data) { | ||
/** | ||
* @constructs | ||
* @private | ||
*/ | ||
__constructor: function(data) { | ||
extend(this, data); | ||
extend(this, data); | ||
}, | ||
}, | ||
/** | ||
* Remove item. | ||
*/ | ||
remove: function() { | ||
/** | ||
* Remove item. | ||
*/ | ||
remove: function() { | ||
delete this; | ||
delete this; | ||
}, | ||
}, | ||
/** | ||
* Determine if item is an element | ||
* (any, with a specific name or in a names array). | ||
* | ||
* @param {String|Array} [param] element name or names arrays | ||
* @return {Boolean} | ||
*/ | ||
isElem: function(param) { | ||
/** | ||
* Determine if item is an element | ||
* (any, with a specific name or in a names array). | ||
* | ||
* @param {String|Array} [param] element name or names arrays | ||
* @return {Boolean} | ||
*/ | ||
isElem: function(param) { | ||
if (!param) return !!this.elem; | ||
if (!param) return !!this.elem; | ||
if (Array.isArray(param)) return !!this.elem && (param.indexOf(this.elem) > -1); | ||
if (Array.isArray(param)) return !!this.elem && (param.indexOf(this.elem) > -1); | ||
return !!this.elem && this.elem === param; | ||
return !!this.elem && this.elem === param; | ||
}, | ||
}, | ||
/** | ||
* Determine if item is a doctype. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isDoctype: function() { | ||
/** | ||
* Determine if item is a doctype. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isDoctype: function() { | ||
return !!this.doctype; | ||
return !!this.doctype; | ||
}, | ||
}, | ||
/** | ||
* Determine if item is a XML Processing Instruction. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isProcInst: function() { | ||
/** | ||
* Determine if item is a XML Processing Instruction. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isProcInst: function() { | ||
return !!this.processinginstruction; | ||
return !!this.processinginstruction; | ||
}, | ||
}, | ||
/** | ||
* Determine if item is a CDATA block. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isCDATA: function() { | ||
/** | ||
* Determine if item is a CDATA block. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isCDATA: function() { | ||
return !!this.cdata; | ||
return !!this.cdata; | ||
}, | ||
}, | ||
/** | ||
* Determine if item is a comment node. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isComment: function() { | ||
/** | ||
* Determine if item is a comment node. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isComment: function() { | ||
return !!this.comment; | ||
return !!this.comment; | ||
}, | ||
}, | ||
/** | ||
* Determine if item is a text node. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isText: function() { | ||
/** | ||
* Determine if item is a text node. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isText: function() { | ||
return !!this.text; | ||
return !!this.text; | ||
}, | ||
}, | ||
/** | ||
* Determine if element is empty. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isEmpty: function() { | ||
/** | ||
* Determine if element is empty. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isEmpty: function() { | ||
return !this.content || !this.content.length; | ||
return !this.content || !this.content.length; | ||
}, | ||
}, | ||
/** | ||
* Iterates over all attributes. | ||
* | ||
* @param {Function} callback | ||
* @return {Boolean} false if the are no any attributes | ||
*/ | ||
eachAttr: function(callback) { | ||
/** | ||
* Iterates over all attributes. | ||
* | ||
* @param {Function} callback | ||
* @return {Boolean} false if the are no any attributes | ||
*/ | ||
eachAttr: function(callback) { | ||
if (!this.hasAttr()) return false; | ||
if (!this.hasAttr()) return false; | ||
Object.getOwnPropertyNames(this.attrs).forEach(function(name) { | ||
callback(this.attrs[name]); | ||
}, this); | ||
Object.getOwnPropertyNames(this.attrs).forEach(function(name) { | ||
callback(this.attrs[name]); | ||
}, this); | ||
}, | ||
}, | ||
/** | ||
* Determine if element has an attribute | ||
* (any, or by name or by name + value). | ||
* | ||
* @param {String|Object} [name] attribute name or object | ||
* @param {String} [val] attribute value (will be toString()'ed) | ||
* @return {Boolean} | ||
*/ | ||
hasAttr: function(attr, val) { | ||
/** | ||
* Determine if element has an attribute | ||
* (any, or by name or by name + value). | ||
* | ||
* @param {String|Object} [name] attribute name or object | ||
* @param {String} [val] attribute value (will be toString()'ed) | ||
* @return {Boolean} | ||
*/ | ||
hasAttr: function(attr, val) { | ||
if (!this.attrs || !Object.keys(this.attrs).length) return false; | ||
if (!this.attrs || !Object.keys(this.attrs).length) return false; | ||
if (!arguments.length) return !!this.attrs; | ||
if (!arguments.length) return !!this.attrs; | ||
if (typeof attr === 'object') { | ||
val = attr.value; | ||
attr = attr.name; | ||
} | ||
if (typeof attr === 'object') { | ||
val = attr.value; | ||
attr = attr.name; | ||
} | ||
if (val !== undefined) return !!this.attrs[attr] && this.attrs[attr].value === val.toString(); | ||
if (val !== undefined) return !!this.attrs[attr] && this.attrs[attr].value === val.toString(); | ||
return !!this.attrs[attr]; | ||
return !!this.attrs[attr]; | ||
}, | ||
}, | ||
/** | ||
* Get a specific attribute from an element | ||
* (by name or name + value). | ||
* | ||
* @param {String} [name] attribute name | ||
* @param {String} [val] attribute value (will be toString()'ed) | ||
* @return {Object} | ||
*/ | ||
attr: function(name, val) { | ||
/** | ||
* Get a specific attribute from an element | ||
* (by name or name + value). | ||
* | ||
* @param {String} [name] attribute name | ||
* @param {String} [val] attribute value (will be toString()'ed) | ||
* @return {Object} | ||
*/ | ||
attr: function(name, val) { | ||
if (!this.hasAttr() || !arguments.length) return undefined; | ||
if (!this.hasAttr() || !arguments.length) return undefined; | ||
if (val !== undefined) return this.hasAttr(name, val) && this.attrs[name]; | ||
if (val !== undefined) return this.hasAttr(name, val) && this.attrs[name]; | ||
return this.hasAttr(name) && this.attrs[name]; | ||
return this.hasAttr(name) && this.attrs[name]; | ||
}, | ||
}, | ||
/** | ||
* Remove a specific attribute. | ||
* | ||
* @param {String|Object} attr attribute name or object | ||
* @param {String} [val] attribute value | ||
* @return {Boolean} | ||
*/ | ||
removeAttr: function(attr, val) { | ||
/** | ||
* Remove a specific attribute. | ||
* | ||
* @param {String|Object} attr attribute name or object | ||
* @param {String} [val] attribute value | ||
* @return {Boolean} | ||
*/ | ||
removeAttr: function(attr, val) { | ||
if (!this.hasAttr(attr)) return false; | ||
if (!this.hasAttr(attr)) return false; | ||
if (!arguments.length) { | ||
delete this.attrs; | ||
return true; | ||
} | ||
if (!arguments.length) { | ||
delete this.attrs; | ||
return true; | ||
} | ||
if (typeof attr === 'object') { | ||
val = attr.value; | ||
attr = attr.name; | ||
} | ||
if (typeof attr === 'object') { | ||
val = attr.value; | ||
attr = attr.name; | ||
} | ||
if (val && this.attrs[attr].value !== val) return false; | ||
if (val && this.attrs[attr].value !== val) return false; | ||
delete this.attrs[attr]; | ||
delete this.attrs[attr]; | ||
if (!Object.keys(this.attrs).length) delete this.attrs; | ||
if (!Object.keys(this.attrs).length) delete this.attrs; | ||
return true; | ||
return true; | ||
}, | ||
}, | ||
/** | ||
* Add attribute. | ||
* | ||
* @param {Object} attr attribute object | ||
*/ | ||
addAttr: function(attr, val) { | ||
/** | ||
* Add attribute. | ||
* | ||
* @param {Object} attr attribute object | ||
*/ | ||
addAttr: function(attr, val) { | ||
this.attrs = this.attrs || {}; | ||
this.attrs = this.attrs || {}; | ||
this.attrs[attr.name] = attr; | ||
this.attrs[attr.name] = attr; | ||
}, | ||
}, | ||
/** | ||
* Determine if item's content has an element(s) | ||
* (any or by name or by names array). | ||
* | ||
* @param {String|Array} [name] element name or names array | ||
* @return {Boolean} | ||
*/ | ||
hasElem: function(name) { | ||
/** | ||
* Determine if item's content has an element(s) | ||
* (any or by name or by names array). | ||
* | ||
* @param {String|Array} [name] element name or names array | ||
* @return {Boolean} | ||
*/ | ||
hasElem: function(name) { | ||
return this.content.some(function(item) { | ||
return name ? item.isElem(name) : item.isElem(); | ||
}); | ||
return this.content.some(function(item) { | ||
return name ? item.isElem(name) : item.isElem(); | ||
}); | ||
}, | ||
}, | ||
/** | ||
* Determine if item's content has only elements | ||
* (any or by name or by names array). | ||
* | ||
* @param {String|Array} [name] element name or names array | ||
* @return {Boolean} | ||
*/ | ||
hasAllElems: function(name) { | ||
/** | ||
* Determine if item's content has only elements | ||
* (any or by name or by names array). | ||
* | ||
* @param {String|Array} [name] element name or names array | ||
* @return {Boolean} | ||
*/ | ||
hasAllElems: function(name) { | ||
return this.content.every(function(item) { | ||
return name ? item.isElem(name) : item.isElem(); | ||
}); | ||
return this.content.every(function(item) { | ||
return name ? item.isElem(name) : item.isElem(); | ||
}); | ||
} | ||
} | ||
} | ||
); | ||
}); |
var INHERIT = require('inherit'); | ||
module.exports = function(json, plugins, pluginsEngine) { | ||
/** | ||
* Plugins engine. | ||
* | ||
* @module plugins | ||
* | ||
* @param {Object} jsdata input data | ||
* @param {Object} plugins plugins object from config | ||
* @param {Class} [pluginsEngine] custom plugins engine class | ||
* @return {Object} output data | ||
*/ | ||
module.exports = function(jsdata, plugins, pluginsEngine) { | ||
var engine = new (pluginsEngine || PluginsEngine)(); | ||
json = engine.pass(json, plugins.directPass); | ||
json = engine.pass(json, plugins.reversePass, true); | ||
jsdata = engine.pass(jsdata, plugins.directPass); | ||
jsdata = engine.pass(jsdata, plugins.reversePass, true); | ||
return json; | ||
return jsdata; | ||
}; | ||
var PluginsEngine = exports.PluginsEngine = INHERIT({ | ||
/** | ||
* Class PluginsEngine. | ||
*/ | ||
var PluginsEngine = exports.PluginsEngine = INHERIT(/** @lends Nodes.prototype */{ | ||
/** | ||
* Require all plugins into array. | ||
* | ||
* @param {Array} arr original plugins list | ||
* @return {Array} require'ed plugins list | ||
* @private | ||
*/ | ||
_makePluginsList: function(arr) { | ||
@@ -26,3 +46,11 @@ | ||
pass: function(json, plugins, reverse) { | ||
/** | ||
* Plugins pass function. | ||
* | ||
* @param {Object} jsdata input data | ||
* @param {Array} plugins list of the current plugins type | ||
* @param {Boolean} [reverse] reverse pass? | ||
* @return {Object} output data | ||
*/ | ||
pass: function(jsdata, plugins, reverse) { | ||
@@ -35,2 +63,3 @@ plugins = this._makePluginsList(plugins); | ||
// reverse pass | ||
if (reverse && item.content) { | ||
@@ -40,10 +69,8 @@ monkeys.call(this, item); | ||
var filter = true; | ||
plugins.forEach(function(plugin) { | ||
if (plugin.active && plugin.fn(item, plugin.params) === false) { | ||
filter = false; | ||
} | ||
// main filter | ||
var filter = plugins.some(function(plugin) { | ||
return plugin.active && plugin.fn(item, plugin.params) === false; | ||
}); | ||
// direct pass | ||
if (!reverse && item.content) { | ||
@@ -53,3 +80,3 @@ monkeys.call(this, item); | ||
return filter; | ||
return !filter; | ||
@@ -62,15 +89,20 @@ }, this); | ||
return monkeys.call(this, json); | ||
return monkeys.call(this, jsdata); | ||
}, | ||
full: function() { | ||
/** | ||
* "Full" plugins. | ||
* | ||
* @return {[type]} [description] | ||
*/ | ||
full: function(jsdata) { | ||
this.fullList.forEach(function(plugin) { | ||
if (plugin.active) { | ||
json = plugin.fn(json, plugin.params); | ||
jsdata = plugin.fn(jsdata, plugin.params); | ||
} | ||
}); | ||
return json; | ||
return jsdata; | ||
@@ -77,0 +109,0 @@ } |
@@ -9,7 +9,9 @@ var Q = require('q'), | ||
* | ||
* @param {String} svg SVG (XML) string | ||
* @module svg2js | ||
* | ||
* @param {String} svgdata input data | ||
* @param {Object} config sax xml parser config | ||
* @return {Object} | ||
* @return {Object} output data deferred promise | ||
*/ | ||
module.exports = function(svg, config) { | ||
module.exports = function(svgdata, config) { | ||
@@ -123,3 +125,3 @@ config = config || { | ||
sax.write(svg).close(); | ||
sax.write(svgdata).close(); | ||
@@ -126,0 +128,0 @@ return deferred.promise; |
@@ -6,3 +6,18 @@ var CONFIG = require('./config'), | ||
module.exports = function(svg, options) { | ||
/** | ||
* SVGO is a Nodejs-based tool for optimizing SVG vector graphics files. | ||
* | ||
* @see http://deepsweet.github.com/svgo/ | ||
* | ||
* @module svgo | ||
* | ||
* @param {String} svgdata input data | ||
* @param {Object} [options] options | ||
* @return {String} output data deferred promise | ||
* | ||
* @author Kir Belevich <kir@soulshine.in> (https://github.com/deepsweet) | ||
* @copyright © 2012 Kir Belevich | ||
* @license MIT https://raw.github.com/deepsweet/svgo/master/LICENSE | ||
*/ | ||
module.exports = function(svgdata, options) { | ||
@@ -12,6 +27,6 @@ return CONFIG(options) | ||
return SVG2JS(svg, config.saxXMLParser) | ||
.then(function(json) { | ||
return SVG2JS(svgdata, config.saxXMLParser) | ||
.then(function(jsdata) { | ||
return JS2SVG(PLUGINS(json, config.plugins)); | ||
return JS2SVG(PLUGINS(jsdata, config.plugins)); | ||
@@ -18,0 +33,0 @@ }); |
@@ -80,5 +80,4 @@ /** | ||
exports.flatten = function(array) { | ||
var result = [], | ||
that = arguments.callee; | ||
exports.flattenOneLevel = function(array) { | ||
var result = []; | ||
@@ -88,3 +87,3 @@ array.forEach(function(item) { | ||
result, | ||
Array.isArray(item) ? that(item) : [item] | ||
Array.isArray(item) ? item : [item] | ||
); | ||
@@ -111,2 +110,4 @@ }); | ||
if (!Object.keys(c).length) return false; | ||
return c; | ||
@@ -113,0 +114,0 @@ }; |
{ | ||
"name": "svgo", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Nodejs-based tool for optimizing SVG vector graphics files", | ||
"keywords": ["svgo", "svg", "optimize", "minify"], | ||
"keywords": [ | ||
"svgo", | ||
"svg", | ||
"optimize", | ||
"minify" | ||
], | ||
"homepage": "http://deepsweet.github.com/svgo", | ||
@@ -28,4 +33,3 @@ "bugs": { | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha --reporter min", | ||
"test-v": "./node_modules/.bin/mocha --reporter spec" | ||
"test": "./node_modules/.bin/mocha --reporter min" | ||
}, | ||
@@ -32,0 +36,0 @@ "dependencies": { |
@@ -1,2 +0,2 @@ | ||
var flatten = require('../lib/tools').flatten; | ||
var flattenOneLevel = require('../lib/tools').flattenOneLevel; | ||
@@ -32,2 +32,4 @@ /* | ||
var unflatten = false; | ||
item.content.forEach(function(g, i) { | ||
@@ -54,4 +56,5 @@ | ||
if (!g.hasAttr()) { | ||
unflatten = true; | ||
item.content.splice(i, 1, g.content); | ||
item.content = flatten(item.content); | ||
} | ||
@@ -62,4 +65,8 @@ } | ||
if (unflatten) { | ||
item.content = flattenOneLevel(item.content); | ||
} | ||
} | ||
}; |
@@ -37,3 +37,5 @@ var intersectAttrs = require('../lib/tools').intersectAttrs; | ||
} else { | ||
intersection = intersectAttrs(intersection, g.attrs) | ||
intersection = intersectAttrs(intersection, g.attrs); | ||
if (!intersection) return false; | ||
} | ||
@@ -45,4 +47,4 @@ | ||
if (every && Object.keys(intersection).length) { | ||
if (every) { | ||
item.content.forEach(function(g) { | ||
@@ -54,3 +56,2 @@ for (var name in intersection) { | ||
}); | ||
} | ||
@@ -57,0 +58,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83688
2349