Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MoOx committed Aug 24, 2014
0 parents commit 5290b32
Show file tree
Hide file tree
Showing 32 changed files with 496 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
@@ -0,0 +1,16 @@
# editorconfig.org
root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules
test/fixtures/*.actual.css
130 changes: 130 additions & 0 deletions .jscsrc
@@ -0,0 +1,130 @@
{
"excludeFiles": [
"node_modules/**"
],
"fileExtensions": [
".js"
],
"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"try",
"catch"
],
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"return",
"try",
"catch"
],
"requireSpaceBeforeBlockStatements": true,
"requireParenthesesAroundIIFE": true,
"requireSpacesInConditionalExpression": {
"afterTest": true,
"beforeConsequent": true,
"afterConsequent": true,
"beforeAlternate": true
},
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"disallowSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowMultipleVarDecl": true,
"requireBlocksOnNewline": 1,
"disallowPaddingNewlinesInBlocks": true,
"disallowEmptyBlocks": true,
"disallowSpacesInsideObjectBrackets": true,
"disallowSpacesInsideArrayBrackets": true,
"disallowSpacesInsideParentheses": true,
"disallowQuotedKeysInObjects": "allButReserved",
"disallowSpaceAfterObjectKeys": true,
"requireCommaBeforeLineBreak": true,
"requireOperatorBeforeLineBreak": [
"?",
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
],
"disallowSpaceAfterPrefixUnaryOperators": [
"++",
"--",
"+",
"-",
"~",
"!"
],
"disallowSpaceBeforePostfixUnaryOperators": [
"++",
"--"
],
"requireSpaceBeforeBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
],
"requireSpaceAfterBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
],
"disallowImplicitTypeConversion": [
"numeric",
"boolean",
"binary",
"string"
],
"requireCamelCaseOrUpperCaseIdentifiers": "ignoreProperties",
"disallowKeywords": [
"with"
],
"disallowMultipleLineStrings": true,
"validateQuoteMarks": "\"",
"validateIndentation": 2,
"disallowMixedSpacesAndTabs": true,
"disallowTrailingWhitespace": true,
"requireKeywordsOnNewLine": [
"else"
],
"requireLineFeedAtFileEnd": true,
"requireCapitalizedConstructors": true,
"safeContextKeyword": "that",
"requireDotNotation": true,
"validateJSDoc": {
"checkParamNames": true,
"checkRedundantParams": true,
"requireParamTypes": true
},
"requireSpaceAfterLineComment": true
}
9 changes: 9 additions & 0 deletions .jshintrc
@@ -0,0 +1,9 @@
{
"newcap": false,
"undef": true,
"unused": true,
"asi": true,
"esnext": true,
"node": true,
"browser": true
}
1 change: 1 addition & 0 deletions .travis.yml
@@ -0,0 +1 @@
language: node_js
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,3 @@
# 1.0.0 - 2014-08-24

First release
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 "MoOx" Maxime Thirouin

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
61 changes: 61 additions & 0 deletions README.md
@@ -0,0 +1,61 @@
# postcss-url [![Build Status](https://travis-ci.org/postcss/postcss-url.png)](https://travis-ci.org/postcss/postcss-url)

> [PostCSS](https://github.com/postcss/postcss) plugin to rebase or inline on url().
## Installation

$ npm install postcss-url

## Usage

```js
// dependencies
var fs = require("fs")
var postcss = require("postcss")
var url = require("postcss-url")

// css to be processed
var css = fs.readFileSync("input.css", "utf8")

// process css
var output = postcss()
.use(url({
url: "rebase" // or "inline"
}))
.process(css, {
// "rebase" mode need at least one of those options
// "inline" mode might need `from` option only
from: "src/stylesheet/index.css"
to: "dist/index.css"
})
.css
```

Checkout [tests](test) for examples.

### Options

#### `url` (default: `"rebase"`)

##### `url: "rebase"`

Allow you to fix `url()` according to postcss `to` and/or `from` options (rebase to `to` first if available, otherwise `from` or `process.cwd()`).

##### `url: "inline"`

Allow you to inline assets using base64 syntax. Can use postcss `from` option to find ressources.

---

## Contributing

Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.

$ git clone https://github.com/postcss/postcss-url.git
$ git checkout -b patch-1
$ npm install
$ npm test

## [Changelog](CHANGELOG.md)

## [License](LICENSE)
106 changes: 106 additions & 0 deletions index.js
@@ -0,0 +1,106 @@
/**
* Module dependencies.
*/
var fs = require("fs")
var path = require("path")
var reduceFunctionCall = require("reduce-function-call")
var base64 = require("js-base64").Base64
var mime = require("mime")

/**
* Fix url() according to source (`from`) or destination (`to`)
*
* @param {Object} options
*/
module.exports = function fixUrl(options) {
options = options || {}
var mode = options.url !== undefined ? options.url : "rebase"

return function(styles, postcssOptions) {
var from = postcssOptions.from ? path.dirname(postcssOptions.from) : "."
var to = postcssOptions.to ? path.dirname(postcssOptions.to) : from

styles.eachDecl(function checkUrl(decl) {
if (!decl.value) {
return
}

if (decl.value.indexOf("url(") > -1) {
var dirname = path.dirname(decl.source.file)
decl.value = reduceFunctionCall(decl.value, "url", function(value) {
// save quote style
var quote = getQuote(value)
value = unquote(value, quote)

// ignore absolute url
if (value.indexOf("/") === 0) {
return value
}

var newPath = value

if (mode === "rebase") {
if (dirname !== from) {
newPath = path.relative(from, dirname + path.sep + newPath)
}
newPath = path.resolve(from, newPath)
newPath = path.relative(to, newPath)
}
else if (mode === "inline") {
var file = path.resolve(from, dirname !== from ? dirname + path.sep + value : value)
if (!fs.existsSync(file)) {
console.warn("Can't read file '" + file + "', ignoring")
}
else {
var mimeType = mime.lookup(file)
if (!mimeType) {
console.warn("Unable to find asset mime-type for " + file)
}
else {
newPath = "data:" + mimeType + ";base64," + base64.encode(file)
}
}
}
else {
throw new Error("Unknow mode for postcss-url: " + mode)
}

return "url(" + quote + newPath + quote + ")"
})
}
})
}
}

/**
* remove quote around a string
*
* @param {String} string
* @param {String} quote
* @return {String} unquoted string
*/
function unquote(string, quote) {
if (quote) {
return string.substr(1, string.length - 2)
}

return string
}


/**
* return quote type
*
* @param {String} string quoted (or not) value
* @return {String} quote if any, or empty string
*/
function getQuote(string) {
var quote = ""
Array("\"", "'").forEach(function(q) {
if (string.charAt(0) === q && string.charAt(string.length - 1) === q) {
quote = q
}
})

return quote
}

0 comments on commit 5290b32

Please sign in to comment.