svg-cleaner0.0.4 • Public • Published
Visit the original Scour - an SVG scrubber, http://codedread.com/scour/
Scour was created by Jeff Schiller.
Please note that this is a partial port, which means it is not finsihed at all. For thoose who want to clean their SVG files and have them as clean as possible as I highly recommend to use the original Scour.py. (Please see the list of implemented and missing processing steps below.)
Recommended! Be sure to check out SVGO, a Nodejs-based tool for optimizing SVG vector graphics files.
Installation & Usage
As Command Line Tool
npm install svg-cleaner -g svg-cleaner INPUT_FILE OUTPUT_FILE
As module - simple interface:
var cleanedSvgString = ;
As module - chainable interface
var SVGCleaner = ;var mySVGCleaner = SVGCleaner;mySVGCleaner;var svgStringWithShortIDAndWithoutComments = mySVGCleaner;
I needed a library to work with SVG-Stacker, that could rename IDs and keep the references inside the SVG document structure intact. SVG-Stacker merges different svg files and needs to make sure that the ids from different files are unique in the merged version. I found that Scour implemented that feature.
I tried to keep the ideas and way how Scour cleans SVG files. I translated the processing steps and copied most of the original comments from scour into the new source code, as they describe the original ideas best. I marked all of these orginial comments by putting 'Scour:' in the first line and used the markdown syntax for quotes (>) to indent the original comment.
- Missing processing steps are marked with an comment '@missing'.
- Changed processing steps are marked with an comment containern '@extended' or '@changed'
- Some functions and variable names are changed to (hopefully) be more descriptive.
Tests are based on mocha, run
Visual tests are based on phantomjs. The idea is to render and rasterize
both, the original SVG file and cleaned version of that SVG file to compare the rasterized results
to ensure they are visualy identical.
Make sure you have installed phantomjs, then run
SVGCleaner;// creates SVG Cleaner instanceSVGCleaner;// loads an SVG StringSVGCleaner;// loads an SVG fileSVGCleaner;// performs all processing stepsSVGCleaner;// returns SVG as StringSVGCleaner;// writes SVG to file
It makes sense to use clean(), as processing steps need to be performend in a specific order. To make use of single processing steps, you can call these steps directly. See description below:
Implemented processing steps
Removal of namespaced elements
namespacesToRemove: array of namespace prefixes, e.g.: ['dc', 'rdf', 'sodipodi', 'cc', 'inkscape']
Removal of namespaced attributes
namespacesToRemove: array of namespace prefixes
Removal of comments
Repairation of styles
Removal of unreferenced elements
Removal of empty elements
Shortening of id attribute values
startNumber: default: 1, optional Shortens the IDs, spreadsheet-style, i.e. from a to z, then from aa to az, ba to bz, etc., until zz.
Removal of unreferenced id attributes
Processing steps in scour, that are not implemented yet:
- remove the xmlns: declarations now
- ensure namespace for SVG is declared
- check for redundant SVG namespace declaration
- convert colors to #RRGGBB format
- remove if the user wants to
- flattend defs elements into just one defs element
- remove gradients that are only referenced by one other gradient
- remove duplicate gradients
- move common attributes to parent group
- remove unused attributes from parent
- remove unnecessary closing point of polygons and scour points
- scour points of polyline
- clean path data
- scour lengths (including coordinates)
- convert rasters references to base64-encoded strings
- properly size the SVG document
SVG-Cleaner is released under the same license as Scour: