Skip to content

Commit

Permalink
Merge pull request #62 from w0rm/remove-cache-cheerio
Browse files Browse the repository at this point in the history
Remove cache of the cheerio object
  • Loading branch information
w0rm committed Mar 12, 2016
2 parents 48de00a + f755817 commit 5bf5231
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 55 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,31 +193,35 @@ 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'));
});
```
## 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
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'),
Expand Down
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand All @@ -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()
}
Expand Down
44 changes: 0 additions & 44 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<svg><rect x="1" y="1" width="2" height="2"/></svg>')
, path: 'square.svg'
})

file.cheerio = cheerio.load('<svg><circle cx="2" cy="2" r="1"/></svg>', { xmlMode: true })

stream.on('data', function (file) {
var result = file.contents.toString()
var target =
'<svg xmlns="http://www.w3.org/2000/svg">' +
'<symbol id="square"><circle cx="2" cy="2" r="1"/></symbol>' +
'</svg>'
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('<svg viewBox="0 0 4 4"><circle cx="2" cy="2" r="1"/></svg>')
, path: 'circle.svg'
}))

stream.end()

})

it('should merge defs to parent svg file', function (done) {

var stream = svgstore({ inlineSvg: true })
Expand Down

0 comments on commit 5bf5231

Please sign in to comment.