assetgraph-sprite
Advanced tools
Comparing version 0.2.5 to 0.2.6
@@ -5,2 +5,4 @@ var _ = require('underscore'), | ||
passError = require('./util/passError'), | ||
AssetGraph = require('assetgraph'), | ||
vendorPrefix = AssetGraph.assets.Css.vendorPrefix, | ||
urlTools = require('assetgraph/lib/util/urlTools'), | ||
@@ -27,2 +29,21 @@ packers = require('./packers'); | ||
function getImageAssetFromCanvas(canvas, assetType, cb) { | ||
if (assetType === 'Png') { | ||
canvas.toBuffer(passError(cb, function (pngBuffer) { | ||
cb(null, new AssetGraph.assets.Png({ | ||
rawSrc: pngBuffer | ||
})); | ||
})); | ||
} else { | ||
var jpegChunks = []; | ||
canvas.createJPEGStream().on('data', function (chunk) { | ||
jpegChunks.push(chunk); | ||
}).on('end', function () { | ||
cb(null, new AssetGraph.assets.Jpeg({ | ||
rawSrc: Buffer.concat(jpegChunks) | ||
})); | ||
}).on('error', cb); | ||
} | ||
} | ||
function calculateSpritePadding(paddingStr) { | ||
@@ -60,7 +81,5 @@ if (paddingStr) { | ||
var assets = assetGraph.constructor.assets, // Hack | ||
relations = assetGraph.constructor.relations, // Hack | ||
spriteGroups = {}; | ||
var spriteGroups = {}; | ||
assetGraph.findRelations({type: 'CssImage'}).forEach(function (relation) { | ||
var spriteInfo = assets.Css.extractInfoFromRule(relation.cssRule, assets.Css.vendorPrefix + '-sprite-'), | ||
var spriteInfo = AssetGraph.assets.Css.extractInfoFromRule(relation.cssRule, vendorPrefix + '-sprite-'), | ||
asset = relation.to; | ||
@@ -92,5 +111,5 @@ if (spriteInfo.group) { | ||
assetGraph.findAssets({type: 'Css'}).forEach(function (cssAsset) { | ||
assets.Css.eachRuleInParseTree(cssAsset.parseTree, function (cssRule) { | ||
if ((assets.Css.vendorPrefix + '-sprite-selector-for-group') in cssRule.style) { | ||
var spriteInfo = assets.Css.extractInfoFromRule(cssRule, assets.Css.vendorPrefix + '-sprite-'), | ||
AssetGraph.assets.Css.eachRuleInParseTree(cssAsset.parseTree, function (cssRule) { | ||
if ((vendorPrefix + '-sprite-selector-for-group') in cssRule.style) { | ||
var spriteInfo = AssetGraph.assets.Css.extractInfoFromRule(cssRule, vendorPrefix + '-sprite-'), | ||
spriteGroupName = spriteInfo.selectorForGroup; | ||
@@ -115,3 +134,3 @@ if (spriteGroupName in spriteGroups) { | ||
imageInfos = _.values(spriteGroup.imageInfosById), | ||
spriteInfo = spriteGroup.placeHolder ? spriteGroup.placeHolder : {}; | ||
spriteInfo = spriteGroup.placeHolder || {}; | ||
@@ -141,3 +160,3 @@ seq(imageInfos) | ||
if ('backgroundColor' in spriteInfo) { | ||
ctx.fillStyle = spriteInfo.imageBackgroundColor; | ||
ctx.fillStyle = spriteInfo.backgroundColor; | ||
ctx.fillRect(0, 0, canvas.width, canvas.height); | ||
@@ -148,8 +167,5 @@ } | ||
}); | ||
canvas.toBuffer(this); | ||
getImageAssetFromCanvas(canvas, /^jpe?g$/.test(spriteInfo.imageFormat) ? 'Jpeg' : 'Png', this); | ||
}) | ||
.seq(function (spriteBuffer) { | ||
var spriteAsset = new assets.Png({ | ||
rawSrc: spriteBuffer | ||
}); | ||
.seq(function (spriteAsset) { | ||
spriteAsset.url = urlTools.resolveUrl(assetGraph.root, spriteAsset.id + spriteAsset.defaultExtension); | ||
@@ -159,3 +175,3 @@ assetGraph.addAsset(spriteAsset); | ||
var cssRule = spriteGroup.placeHolder.cssRule, | ||
relation = new relations.CssImage({ | ||
relation = new AssetGraph.relations.CssImage({ | ||
cssRule: cssRule, | ||
@@ -171,3 +187,3 @@ propertyName: 'background-image', | ||
['selector-for-group', 'packer', 'image-format', 'background-color', 'important'].forEach(function (propertyName) { | ||
spriteGroup.placeHolder.cssRule.style.removeProperty(assets.Css.vendorPrefix + '-sprite-' + propertyName); | ||
spriteGroup.placeHolder.cssRule.style.removeProperty(vendorPrefix + '-sprite-' + propertyName); | ||
}); | ||
@@ -178,3 +194,3 @@ } | ||
incomingRelation.from.markDirty(); | ||
var relationSpriteInfo = assets.Css.extractInfoFromRule(incomingRelation.cssRule, assets.Css.vendorPrefix + '-sprite-'); | ||
var relationSpriteInfo = AssetGraph.assets.Css.extractInfoFromRule(incomingRelation.cssRule, vendorPrefix + '-sprite-'); | ||
incomingRelation.cssRule.style.setProperty('background-position', | ||
@@ -185,3 +201,3 @@ (imageInfo.x ? (-imageInfo.x) + "px " : "0 ") + | ||
['group', 'padding', 'no-group-selector', 'important'].forEach(function (propertyName) { | ||
incomingRelation.cssRule.style.removeProperty(assets.Css.vendorPrefix + '-sprite-' + propertyName); | ||
incomingRelation.cssRule.style.removeProperty(vendorPrefix + '-sprite-' + propertyName); | ||
}); | ||
@@ -192,3 +208,3 @@ if (relationSpriteInfo.noGroupSelector) { | ||
// matched by the sprite group's "main" selector, which would have been preferable. | ||
var relation = new relations.CssImage({ | ||
var relation = new AssetGraph.relations.CssImage({ | ||
cssRule: incomingRelation.cssRule, | ||
@@ -195,0 +211,0 @@ propertyName: incomingRelation.propertyName, |
@@ -5,3 +5,3 @@ { | ||
"repository": "git://github.com/One-com/assetgraph-sprite.git", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"maintainers": [ | ||
@@ -19,7 +19,7 @@ { | ||
"bufferjs": "= 1.0.2", | ||
"canvas": "= 0.8.1", | ||
"canvas": "= 0.10.0", | ||
"seq": "= 0.3.5" | ||
}, | ||
"devDependencies": { | ||
"assetgraph": "~ 0.2.0", | ||
"assetgraph": "latest", | ||
"vows": ">= 0.5.6" | ||
@@ -33,3 +33,6 @@ }, | ||
}, | ||
"scripts": { | ||
"prepublish": "npm install && vows" | ||
}, | ||
"main": "lib/index.js" | ||
} |
@@ -34,3 +34,25 @@ var vows = require('vows'), | ||
} | ||
}, | ||
'After loading the same test case again, set the -one-sprite-image-format to jpg and sprite the background images': { | ||
topic: function () { | ||
new AssetGraph({root: __dirname + '/spriteBackgroundImages'}).queue( | ||
transforms.loadAssets('style.css'), | ||
transforms.populate(), | ||
function (assetGraph) { | ||
var cssAsset = assetGraph.findAssets({type: 'Css'})[0]; | ||
cssAsset.parseTree.cssRules[0].style.setProperty('-one-sprite-image-format', 'jpg'); | ||
cssAsset.markDirty(); | ||
}, | ||
require('../lib')() | ||
).run(this.callback); | ||
}, | ||
'there should be no Png assets left in the graph': function (assetGraph) { | ||
assert.equal(assetGraph.findAssets({type: 'Png'}).length, 0); | ||
}, | ||
'the graph should contain an Jpeg asset': function (assetGraph) { | ||
var jpegAssets = assetGraph.findAssets({type: 'Jpeg'}); | ||
assert.equal(jpegAssets.length, 1); | ||
assert.equal(jpegAssets[0].rawSrc.slice(6, 10).toString('ascii'), 'JFIF'); | ||
} | ||
} | ||
})['export'](module); |
Sorry, the diff of this file is not supported yet
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
30390
461
Updatedcanvas@= 0.10.0