Skip to content
This repository has been archived by the owner on Dec 30, 2019. It is now read-only.

Commit

Permalink
added support to inline images in output using option --inlineImages
Browse files Browse the repository at this point in the history
  • Loading branch information
gcoop committed Jun 23, 2012
1 parent 2d3d018 commit 71d5b62
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 1 deletion.
45 changes: 45 additions & 0 deletions lib/compile/inline-images.js
@@ -0,0 +1,45 @@
// ==========================================
// RECESS
// COMPILE: replaces image links with base64 image data
// ==========================================
// Copyright 2012 Twitter, Inc
// Licensed under the Apache License v2.0
// http://www.apache.org/licenses/LICENSE-2.0
// ==========================================

'use strict'

var less = require('less')
, fs = require('fs')
, seperator = (process.platform == 'win32') ? '\\' : '/'
, toCSS
, path

function compile () {
// strip units from 0 values
var props = toCSS.apply(this, arguments)

// do we have a url here?
if (/url\(([a-zA-Z\"\'.]+)\)/.test(props)) {
var fileName = props.replace(/url\((\"|\'|)/, '').replace(/(\"|\'|)\)/, '')
, chunks = fileName.split('.')
, ext = chunks[chunks.length - 1]
, mimetype = 'image/' + ext.replace(/jpg/, 'jpeg')
, pathParts = path.split('/')
, filePath = pathParts.slice(0, pathParts.length - 1).join(seperator)

return props.replace(/url\(([a-zA-Z\"\'.]+)\)/, 'url(data:' + mimetype + ';base64,' + new Buffer(fs.readFileSync(filePath+seperator+fileName)).toString('base64') + ')')
}

return props
}

module.exports.on = function () {
path = this.path
toCSS = less.tree.Value.prototype.toCSS
less.tree.Value.prototype.toCSS = compile
}

module.exports.off = function () {
less.tree.Value.prototype.toCSS = toCSS
}
2 changes: 1 addition & 1 deletion lib/core.js
Expand Up @@ -149,7 +149,7 @@ RECESS.prototype = {
Object.keys(this.options).forEach(function (key) {
that.options[key]
&& RECESS.COMPILERS[key]
&& RECESS.COMPILERS[key].on()
&& RECESS.COMPILERS[key].on.call(that)
})

// iterate over defintions and compress them (join with new lines)
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -81,6 +81,7 @@ module.exports.DEFAULTS = RECESS.DEFAULTS = {
, stripColors: false
, watch: false
, zeroUnits: true
, inlineImages: true
}


Expand Down
60 changes: 60 additions & 0 deletions lib/lint/inline-images.js
@@ -0,0 +1,60 @@
// ===================================================
// RECESS
// RULE: Linked images should be embeded.
// ===================================================
// Copyright 2012 Twitter, Inc
// Licensed under the Apache License v2.0
// http://www.apache.org/licenses/LICENSE-2.0
// ===================================================

'use strict'

var util = require('../util')
, RULE = {
type: 'inlineImages'
, exp: /url\(([a-zA-Z\"\'.]+)\)/
, message: 'Linked images should be embeded.'
}

// validation method
module.exports = function (def, data) {

// default validation to true
var isValid = true

// return if no selector to validate
if (!def.rules) return isValid

// loop over selectors
def.rules.forEach(function (rule) {
var extract

// continue to next rule if no url is present
if ( !(rule.value
&& rule.value.is == 'value'
&& RULE.exp.test(rule.value.toCSS({}))) ) return

// calculate line number for the extract
extract = util.getLine(rule.index, data)
extract = util.padLine(extract)

// highlight invalid 0 units
extract += rule.toCSS({}).replace(RULE.exp, function ($1) {
return $1.magenta
})

// set invalid flag to false
isValid = false

// set error object on defintion token
util.throwError(def, {
type: RULE.type
, message: RULE.message
, extract: extract
})

})

// return validation state
return isValid
}
11 changes: 11 additions & 0 deletions test/compiled/inline-images.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/fixtures/inline-images.css
@@ -0,0 +1,9 @@
.foo {
background-image: url("sprite.png");
}
.bar {
background: url('sprite.png');
}
.fat {
background: url(sprite.png);
}
Binary file added test/fixtures/sprite.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/types/compile.js
Expand Up @@ -4,6 +4,10 @@ var fs = require('fs')
, RECESS = require('../../lib')

fs.readdirSync('test/fixtures').forEach(function (file, index) {
// Ignore anything not a less/css file.
if (file.indexOf('css') === -1 && file.indexOf('less') === -1) {
return
}

RECESS('test/fixtures/' + file, { compile: true }, function (err, fat) {
file = file.replace(/less$/, 'css')
Expand Down
26 changes: 26 additions & 0 deletions test/types/lint.js
Expand Up @@ -214,4 +214,30 @@ var assert = require('assert')

}()

//VALIDATIONS.inlineImage
!function () {

var path = 'test/fixtures/inline-images.css'
, Recess = new RECESS.Constructor()
, validate = RECESS.Constructor.prototype.validate
, def

RECESS.Constructor.prototype.validate = noop

Recess.data = fs.readFileSync(path, 'utf8')

Recess.parse()

def = Recess.definitions[0]

RECESS.Constructor.RULES.inlineImages(def, Recess.data)

assert.ok(def.errors)
assert.equal(def.errors.length, 1, 'one error found')
assert.equal(def.errors[0].type, 'inlineImages')

RECESS.Constructor.prototype.validate = validate

}()

console.log("✓ linting".green)

0 comments on commit 71d5b62

Please sign in to comment.