Socket
Socket
Sign inDemoInstall

gulp-edit-xml

Package Overview
Dependencies
16
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.0 to 3.0.0

56

index.js

@@ -1,12 +0,11 @@

var Stream = require('stream'),
xml2js = require('xml2js'),
isFunction = require('lodash.isfunction'),
isObject = require('lodash.isobject'),
assign = require("object-assign"),
gutil = require('gulp-util');
const Stream = require('stream');
const xml2js = require('xml2js');
const isFunction = require('lodash.isfunction');
const isObject = require('lodash.isobject');
const assign = require('object-assign');
const PluginError = require('plugin-error');
var xmlEdit = function(transform, options){
if(!isFunction(transform)){
transform = function(data){
const xmlEdit = function(transform, options) {
if (!isFunction(transform)) {
transform = function(data) {
return data;

@@ -16,20 +15,19 @@ };

var defaults = {
const defaults = {
parserOptions: {},
builderOptions: {
headless:true,
renderOpts:{
headless: true,
renderOpts: {
pretty: false
}
}
}
};
var settings = assign(defaults, options);
const settings = assign(defaults, options);
var stream = new Stream.Transform({ objectMode: true });
const stream = new Stream.Transform({ objectMode: true });
stream._transform = function(file, unused, done){
stream._transform = function(file, unused, done) {
const that = this;
var that = this;
if (file.isNull()) {

@@ -40,18 +38,20 @@ return done(null, file);

if (file.isStream()) {
return done(new gutil.PluginError('gulp-xml-edit', 'Streaming not supported'));
return done(new PluginError('gulp-xml-edit', 'Streaming not supported'));
}
const content = file.contents.toString('utf-8');
const parser = new xml2js.Parser(settings.parserOptions);
const builder = new xml2js.Builder(settings.builderOptions);
var content = file.contents.toString('utf-8'),
parser = new xml2js.Parser(settings.parserOptions),
builder = new xml2js.Builder(settings.builderOptions);
parser.parseString(content, function(err, data) {
let content = transform.call(null, data);
parser.parseString(content, function(err,data){
var content = transform.call(null, data);
if(!isObject(content)){
done(new gutil.PluginError('gulp-xml-edit', 'transformation does not returns an object'));
if (!isObject(content)) {
done(new PluginError('gulp-xml-edit', 'transformation does not returns an object'));
return;
}
content = builder.buildObject(content);
file.contents = new Buffer(content);
return done(null, file);

@@ -62,4 +62,4 @@ });

return stream;
}
};
module.exports = xmlEdit;
{
"name": "gulp-edit-xml",
"version": "2.0.0",
"description": "Gulp plugin for editing xml files",
"main": "index.js",
"scripts": {
"test": "jasmine-node test/edit-xml-spec.js"
},
"repository": {
"type": "git",
"url": "https://github.com/vkbansal/gulp-edit-xml.git"
},
"keywords": [
"gulp",
"plugin",
"gulpplugin",
"xml",
"svg"
],
"author": "Vivek Kumar Bansal <contact@vkbansal.me>",
"license": "MIT",
"bugs": {
"url": "https://github.com/vkbansal/gulp-edit-xml/issues"
},
"homepage": "https://github.com/vkbansal/gulp-edit-xml",
"dependencies": {
"gulp-util": "^3.0.4",
"lodash.isfunction": "^3.0.6",
"lodash.isobject": "^3.0.2",
"object-assign": "^4.0.1",
"xml2js": "^0.4.8"
},
"devDependencies": {
"event-stream": "^3.3.1",
"jasmine-node": "^1.14.5"
}
"name": "gulp-edit-xml",
"version": "3.0.0",
"description": "Gulp plugin for editing xml files",
"main": "index.js",
"scripts": {
"test": "jasmine-node test/edit-xml-spec.js"
},
"repository": {
"type": "git",
"url": "https://github.com/vkbansal/gulp-edit-xml.git"
},
"keywords": [
"gulp",
"plugin",
"gulpplugin",
"xml",
"svg"
],
"author": "Vivek Kumar Bansal <contact@vkbansal.me>",
"license": "MIT",
"bugs": {
"url": "https://github.com/vkbansal/gulp-edit-xml/issues"
},
"homepage": "https://github.com/vkbansal/gulp-edit-xml",
"dependencies": {
"lodash.isfunction": "^3.0.6",
"lodash.isobject": "^3.0.2",
"object-assign": "^4.0.1",
"plugin-error": "^0.1.2",
"xml2js": "^0.4.8"
},
"devDependencies": {
"event-stream": "^3.3.1",
"jasmine-node": "^1.14.5",
"vinyl": "^2.1.0"
}
}
[![NPM version](https://badge.fury.io/js/gulp-edit-xml.svg)](http://badge.fury.io/js/gulp-edit-xml) [![Build Status](https://travis-ci.org/vkbansal/gulp-edit-xml.svg?branch=master)](https://travis-ci.org/vkbansal/gulp-edit-xml) [![Dependencies](https://david-dm.org/vkbansal/gulp-edit-xml.png)](https://david-dm.org/vkbansal/gulp-edit-xml) [![devDependency Status](https://david-dm.org/vkbansal/gulp-edit-xml/dev-status.svg)](https://david-dm.org/vkbansal/gulp-edit-xml#info=devDependencies)
#gulp-edit-xml
# gulp-edit-xml
A gulp plugin for editing xml files, specially svg files. It makes use of [xml2js](https://www.npmjs.org/package/xml2js). It helps in further manual optimization of a svg file even after using [svgo](https://www.npmjs.org/package/svgo).
##Install
## Install
```bash
npm install gulp-edit-xml
```
##Why I made this
## Why I made this
Recieved a `~400KB` svg file from a client. After editing it in Inkscape and processing it with `svgo`, the file size was reduced to `~140KB`. The file contained `~1600` circle elements which had `transform` and `fill` attributes on each of it.

@@ -20,3 +25,4 @@

##Usage
## Usage
```js

@@ -27,38 +33,45 @@ var gulp = require('gulp'),

gulp.task('svg', function(){
gulp.src('src/img/main.svg')
.pipe(svgo())
.pipe(xmlEdit(function(xml){
var nodes = xml.svg.g[0].circle;
for(var i = 0 , l = nodes.length; i < l; i++){
var cn = nodes[i].$;
if (cn.hasOwnProperty('transform')){
var transforms = cn.transform.match(/translate\(([\d\s\-\.]+)\)/)[1];
transforms = transforms.split(' ');
cn.cx = parseInt(cn.cx) + parseInt(transforms[0]);
cn.cx = Math.round(cn.cx*10)/10;
if (transforms.length == 2){
cn.cy = parseInt(cn.cy) + parseInt(transforms[1]);
cn.cy = Math.round(cn.cy*10)/10;
gulp.task('svg', function() {
gulp
.src('src/img/main.svg')
.pipe(svgo())
.pipe(
xmlEdit(function(xml) {
var nodes = xml.svg.g[0].circle;
for (var i = 0, l = nodes.length; i < l; i++) {
var cn = nodes[i].$;
if (cn.hasOwnProperty('transform')) {
var transforms = cn.transform.match(/translate\(([\d\s\-\.]+)\)/)[1];
transforms = transforms.split(' ');
cn.cx = parseInt(cn.cx) + parseInt(transforms[0]);
cn.cx = Math.round(cn.cx * 10) / 10;
if (transforms.length == 2) {
cn.cy = parseInt(cn.cy) + parseInt(transforms[1]);
cn.cy = Math.round(cn.cy * 10) / 10;
}
delete cn.transform;
delete cn.fill;
}
nodes[i].$ = cn;
}
delete cn.transform;
delete cn.fill;
}
nodes[i].$ = cn;
}
xml.svg.g[0].circle = nodes;
return xml;
}))
.pipe(gulp.dest('dist/img/'))
})
xml.svg.g[0].circle = nodes;
return xml;
})
)
.pipe(gulp.dest('dist/img/'));
});
```
##Options
## Options
Its takes two optional arguments:
```
xmlEdit(transform,options)
```
**transform:** A function that can be used to do manual operations. It takes one argument: The `xml` as an object. *Remember to return the object at end*.
**transform:** A function that can be used to do manual operations. It takes one argument: The `xml` as an object. _Remember to return the object at end_.
default:
```js

@@ -73,2 +86,3 @@ function(data){

default:
```js

@@ -86,3 +100,4 @@ {

###License
### License
[MIT](LICENSE.md)

@@ -1,20 +0,13 @@

var xmlEdit = require('../index.js'),
gutil = require('gulp-util'),
es = require('event-stream');
const xmlEdit = require('../index.js');
const es = require('event-stream');
const Vinyl = require('vinyl');
describe('gulp-xml-edit', function(){
describe('gulp-xml-edit', function() {
it('should work in buffer mode', function(done) {
const stream = xmlEdit();
const fakeBuffer = new Buffer('<svg/>');
const fakeFile = new Vinyl({ contents: fakeBuffer });
it('should work in buffer mode', function(done){
var stream = xmlEdit(),
fakeBuffer = new Buffer('<svg/>'),
fakeFile = new gutil.File({ contents: fakeBuffer });
stream.on('data', function(file){
expect(file.contents.toString()).toEqual(fakeBuffer.toString());
});
stream.on('end', function() {
done();
});
stream.on('data', file => expect(file.contents.toString()).toEqual(fakeBuffer.toString()));
stream.on('end', () => done());
stream.write(fakeFile);

@@ -24,18 +17,21 @@ stream.end();

it('should let null files pass through', function(done) {
var stream = xmlEdit(),
it('should let null files pass through', function(done) {
const stream = xmlEdit();
const fakeFile = new Vinyl({ path: 'null.md', contents: null });
let n = 0;
fakeFile = new gutil.File({path: 'null.md', contents: null}),
stream.pipe(
es.through(
file => {
expect(file.path).toBe('null.md');
expect(file.contents).toBe(null);
n++;
},
() => {
expect(n).toBe(1);
done();
}
)
);
n = 0;
stream.pipe(es.through(function(file) {
expect(file.path).toBe('null.md');
expect(file.contents).toBe(null);
n++;
}, function() {
expect(n).toBe(1);
done();
}));
stream.write(fakeFile);

@@ -45,29 +41,33 @@ stream.end();

it('should transform as expected', function(done){
it('should transform as expected', function(done) {
const stream = xmlEdit(data => {
delete data.svg.g[0].circle[0].$.transform;
return data;
});
const fakeBuffer = new Buffer(
"<svg><g><circle cx='20' cy='20' cr='20' transform='translate(20 20)'/></g></svg>"
);
const fakeFile = new Vinyl({
contents: fakeBuffer
});
let n = 0;
var stream = xmlEdit(function(data){
delete data.svg.g[0].circle[0].$.transform;
return data;
}),
stream.pipe(
es.through(
file => {
expect(file.contents.toString()).toBe(
'<svg><g><circle cx="20" cy="20" cr="20"/></g></svg>'
);
n++;
},
() => {
expect(n).toBe(1);
done();
}
)
);
fakeBuffer = new Buffer("<svg><g><circle cx='20' cy='20' cr='20' transform='translate(20 20)'/></g></svg>"),
fakeFile = new gutil.File({
contents: fakeBuffer
}),
n = 0;
stream.pipe(es.through(function(file) {
expect(file.contents.toString()).toBe('<svg><g><circle cx="20" cy="20" cr="20"/></g></svg>');
n++;
}, function() {
expect(n).toBe(1);
done();
}));
stream.write(fakeFile);
stream.end();
});
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc