diff --git a/README.md b/README.md index 50ed976..5d8e4ae 100644 --- a/README.md +++ b/README.md @@ -193,8 +193,11 @@ gulp.task('svgstore', function () { return gulp .src('test/src/*.svg') .pipe(svgstore({ inlineSvg: true })) - .pipe(cheerio(function ($) { - $('svg').attr('style', 'display:none'); + .pipe(cheerio({ + run: function ($) { + $('svg').attr('style', 'display:none'); + }, + parserOptions: { xmlMode: true } })) .pipe(gulp.dest('test/dest')); }); @@ -202,8 +205,8 @@ gulp.task('svgstore', function () { ## Extracting metadata from combined svg -Since gulp-svgstore and gulp-cheerio plugins cache cheerio in gulp file object, -you may use it in your pipeline to extract metadata from svg sources or combined svg. +You can extract data with cheerio. + The following example extracts viewBox and id from each symbol in combined svg. ```js @@ -211,13 +214,14 @@ var gulp = require('gulp'); var gutil = require('gulp-util'); var svgstore = require('gulp-svgstore'); var through2 = require('through2'); +var cheerio = require('cheerio'); gulp.task('metadata', function () { return gulp .src('test/src/*.svg') .pipe(svgstore()) .pipe(through2.obj(function (file, encoding, cb) { - var $ = file.cheerio; + var $ = cheerio.load(file.contents.toString(), {xmlMode: true}); var data = $('svg > symbol').map(function () { return { name: $(this).attr('id'), diff --git a/index.js b/index.js index 7605d4a..37ebc94 100644 --- a/index.js +++ b/index.js @@ -35,11 +35,8 @@ module.exports = function (config) { if (file.isNull()) return cb() - if (!file.cheerio) { - file.cheerio = cheerio.load(file.contents.toString(), { xmlMode: true }) - } - var $svg = file.cheerio('svg') + var $svg = cheerio.load(file.contents.toString(), { xmlMode: true })('svg') if ($svg.length === 0) return cb() @@ -102,7 +99,7 @@ module.exports = function (config) { } } - var $defs = file.cheerio('defs') + var $defs = $svg.find('defs') if ($defs.length > 0) { $combinedDefs.append($defs.contents()) $defs.remove() @@ -122,7 +119,6 @@ module.exports = function (config) { $combinedSvg.attr(nsName, namespaces[nsName]) } var file = new gutil.File({ path: fileName, contents: new Buffer($.xml()) }) - file.cheerio = $ this.push(file) cb() } diff --git a/test.js b/test.js index 8098326..78a2f77 100644 --- a/test.js +++ b/test.js @@ -166,50 +166,6 @@ describe('gulp-svgstore unit test', function () { }) - it('should use cached cheerio object instead of file contents', function (done) { - - var stream = svgstore({ inlineSvg: true }) - var file = new gutil.File({ - contents: new Buffer('') - , path: 'square.svg' - }) - - file.cheerio = cheerio.load('', { xmlMode: true }) - - stream.on('data', function (file) { - var result = file.contents.toString() - var target = - '' + - '' + - '' - assert.equal( result, target ) - done() - }) - - stream.write(file) - stream.end() - - }) - - it('should cache cheerio object for the result file', function (done) { - - var stream = svgstore() - - stream.on('data', function (file) { - assert.ok(file.cheerio) - assert.equal( file.contents.toString(), file.cheerio.xml() ) - done() - }) - - stream.write(new gutil.File({ - contents: new Buffer('') - , path: 'circle.svg' - })) - - stream.end() - - }) - it('should merge defs to parent svg file', function (done) { var stream = svgstore({ inlineSvg: true })