@@ -0,0 +1,13 @@ | |||
#!/usr/bin/env node | |||
var prefix = require("./index.js"); | |||
var str = ""; | |||
process.stdin.on("data", function(data) { | |||
str += data; | |||
}); | |||
process.stdin.on("end", function() { | |||
console.log(prefix(str)); | |||
}); |
@@ -0,0 +1,26 @@ | |||
var regStr = "@prefix\\s+([^;]+);"; | |||
var globalRegex = new RegExp(regStr, "g"); | |||
var localRegex = new RegExp(regStr); | |||
var prefixes = ["-moz-", "-webkit-", "-o-", "-ms-", ""]; | |||
module.exports = function(str) { | |||
var matches = str.match(globalRegex); | |||
if (!matches) | |||
return str; | |||
matches.forEach(function(m) { | |||
var cssProperty = m.match(localRegex)[1]; | |||
var res = ""; | |||
prefixes.forEach(function(prefix) { | |||
res += prefix+cssProperty+"; "; | |||
}); | |||
res = res.substring(0, res.length - 1); | |||
str = str.replace(m, res); | |||
}); | |||
return str; | |||
} |
@@ -0,0 +1,14 @@ | |||
{ | |||
"name": "browser-prefix", | |||
"version": "0.0.0", | |||
"description": "A utility to add browser prefixes to CSS.", | |||
"main": "index.js", | |||
"scripts": { | |||
"test": "echo \"Error: no test specified\" && exit 1" | |||
}, | |||
"author": "Martin Dørum Nygaard", | |||
"license": "MIT", | |||
"bin": { | |||
"browser-prefix": "./cli.js" | |||
} | |||
} |
@@ -0,0 +1,39 @@ | |||
# browser-prefix | |||
This is a simple utility to add browser prefixes to CSS. Its syntax is really simple; just add `@prefix` to the beginning of any line, and it will add all relevant browser prefixes to that line. All prefixes will be on the same line, which means all line numbers will match between your source files and the result. | |||
## Installation | |||
Install with npm: | |||
`npm install browser-prefix` | |||
To install as a command line application: | |||
`npm install -g browser-prefix` | |||
## Usage | |||
### In node.js | |||
``` | |||
var prefix = require("browser-prefix"); | |||
prefix("@prefix foo: bar;"); | |||
//returns '-moz-foo: bar; -webkit-foo: bar; -o-foo: bar; -ms-foo: bar; foo: bar;' | |||
``` | |||
### As a CLI application | |||
From stdin: | |||
``` | |||
$ echo "@prefix foo: bar" | browser-prefix | |||
# returns '-moz-foo: bar; -webkit-foo: bar; -o-foo: bar; -ms-foo: bar; foo: bar;' | |||
``` | |||
From file: | |||
``` | |||
$ browser-prefix < infile.css > outfile.css | |||
``` |
@@ -0,0 +1,85 @@ | |||
var assert = require("assert"); | |||
var prefix = require("../index.js"); | |||
describe("browser-prefix", function() { | |||
it("doesn't mess with things without @prefix", function(done) { | |||
var str = "foo: bar; prefix: none;"; | |||
if (str !== prefix(str)) | |||
throw "Input string doesn't match output string."; | |||
done(); | |||
}); | |||
it("manages basic prefixing", function(done) { | |||
assert.equal( | |||
prefix("@prefix foo: bar;"), | |||
"-moz-foo: bar; -webkit-foo: bar; -o-foo: bar; "+ | |||
"-ms-foo: bar; foo: bar;" | |||
); | |||
done(); | |||
}); | |||
it("deals with multiple @prefixes on the same line", function(done) { | |||
assert.equal( | |||
prefix("@prefix foo: bar; @prefix bar: foo;"), | |||
"-moz-foo: bar; -webkit-foo: bar; -o-foo: bar; "+ | |||
"-ms-foo: bar; foo: bar; "+ | |||
"-moz-bar: foo; -webkit-bar: foo; -o-bar: foo; "+ | |||
"-ms-bar: foo; bar: foo;" | |||
); | |||
done(); | |||
}); | |||
it("deals with multiple @prefixes on separate lines", function(done) { | |||
assert.equal( | |||
prefix( | |||
"@prefix foo: bar;\n"+ | |||
"foo: bar;\n"+ | |||
"@prefix bar: foo;" | |||
), | |||
"-moz-foo: bar; -webkit-foo: bar; -o-foo: bar; "+ | |||
"-ms-foo: bar; foo: bar;\n"+ | |||
"foo: bar;\n"+ | |||
"-moz-bar: foo; -webkit-bar: foo; -o-bar: foo; "+ | |||
"-ms-bar: foo; bar: foo;" | |||
); | |||
done(); | |||
}); | |||
it("deals with tabs for indentation", function(done) { | |||
assert.equal( | |||
prefix( | |||
"\t\t\t"+ | |||
"@prefix foo: bar;" | |||
), | |||
"\t\t\t"+ | |||
"-moz-foo: bar; -webkit-foo: bar; -o-foo: bar; "+ | |||
"-ms-foo: bar; foo: bar;" | |||
); | |||
done(); | |||
}); | |||
it("deals with spaces for indentation", function(done) { | |||
assert.equal( | |||
prefix( | |||
" "+ | |||
"@prefix foo: bar;" | |||
), | |||
" "+ | |||
"-moz-foo: bar; -webkit-foo: bar; -o-foo: bar; "+ | |||
"-ms-foo: bar; foo: bar;" | |||
); | |||
done(); | |||
}); | |||
}); |