Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from ai/function
Add common rework.function to clean function filter
- Loading branch information
Showing
10 changed files
with
150 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
input { | ||
box-shadow: 0 0 5px black(0.7); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
var rework = require('..') | ||
, read = require('fs').readFileSync; | ||
|
||
var css = rework(read('examples/function.css', 'utf8')) | ||
.use(rework.function({ | ||
black: black | ||
})) | ||
.toString() | ||
|
||
function black(opacity) { | ||
return 'rgba(0, 0, 0, ' + opacity + ')'; | ||
} | ||
|
||
console.log(css); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var visit = require('../visit'); | ||
|
||
/** | ||
* Define custom function. | ||
*/ | ||
|
||
module.exports = function(functions) { | ||
if (!functions) throw new Error('functions object required'); | ||
return function(style, rework){ | ||
visit.declarations(style, function(declarations){ | ||
for (var name in functions) { | ||
func(declarations, name, functions[name]); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
/** | ||
* Escape regexp codes in string. | ||
* | ||
* @param {String} s | ||
* @api private | ||
*/ | ||
function escape(s) { | ||
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
} | ||
|
||
/** | ||
* Visit declarations and apply functions. | ||
* | ||
* @param {Array} declarations | ||
* @param {Object} functions | ||
* @api private | ||
*/ | ||
function func(declarations, name, func) { | ||
var regexp = new RegExp(escape(name) + '\\(([^\)]+)\\)', 'g'); | ||
declarations.forEach(function(decl){ | ||
if (!~decl.value.indexOf(name + '(')) return; | ||
decl.value = decl.value.replace(regexp, function(_, args){ | ||
args = args.split(/,\s*/); | ||
return func.apply(decl, args); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@font-face { | ||
scr: fonts(a.woff,a.ttf, a.otf); | ||
font-family: A | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@font-face { | ||
scr: url(a.woff) format("woff"), url(a.ttf) format("truetype"), url(a.otf) format("opentype"); | ||
font-family: A | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters