-
Notifications
You must be signed in to change notification settings - Fork 250
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 #3 from slang800/gradients
Gradients
- Loading branch information
Showing
13 changed files
with
392 additions
and
174 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
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,75 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var stylus = require('stylus') | ||
, nodes = stylus.nodes; | ||
|
||
var RE_GRADIENT_STOPS = /([\(\,]\s*)(-?(?:\d*\.)?\d+(?:%|px|em))(\s+)((hsl|rgb)a?\([^\)]+\)|#[^\)\,]+)/g | ||
, RE_GRADIENT_VAL = /(\(\s*)(?:(-?(\d*\.)?\d+)deg|((to )?(top|bottom|left|right)( (top|bottom|left|right))?))/g | ||
, RE_GRADIENT_TYPE = /((repeating-)?(linear|radial)-gradient\()/g | ||
, RE_TRANSFORM = /\b(transform)\b/g | ||
, RE_FILL_KEYWORD = /\s*\b(fill)\b\s*/g; | ||
|
||
var DIRECTIONS = { top: 'bottom', bottom: 'top', left: 'right', right:'left' }; | ||
|
||
/** | ||
* Expose `normalize`. | ||
*/ | ||
|
||
exports = module.exports = normalize; | ||
|
||
function normalize(property, value, prefix){ | ||
var result = value.toString() | ||
, args; | ||
|
||
/* Fixing the gradients */ | ||
if (~result.indexOf('gradient(')) { | ||
|
||
/* Normalize color stops */ | ||
result = result.replace(RE_GRADIENT_STOPS,'$1$4$3$2'); | ||
|
||
/* Normalize legacy gradients */ | ||
result = result.replace(RE_GRADIENT_VAL, function(){ | ||
args = [].slice.call(arguments, 1); | ||
return normalizeGradient(args, prefix); | ||
}); | ||
|
||
/* Adding prefixes to the legacy gradients */ | ||
if (prefix) result = result.replace(RE_GRADIENT_TYPE, '-' + prefix + '-$1'); | ||
} | ||
|
||
/* Adding prefixes to the `transform` values of legacy `transition` property */ | ||
if (prefix && (property == "'transition'" || property == "'transition-property'")) { | ||
result = result.replace(RE_TRANSFORM, '-' + prefix + '-$1'); | ||
} | ||
|
||
/* Removing `fill` keyword from the legacy `border-image` property */ | ||
if (prefix && (property == "'border-image'" || property == "'border-image-slice'")) { | ||
result = result.replace(RE_FILL_KEYWORD, ' '); | ||
} | ||
|
||
return new nodes.Ident(result); | ||
} | ||
|
||
function normalizeGradient(parts, prefix){ | ||
/* Fix the degrees to the legacy syntax */ | ||
var val = parts[0]; | ||
if (parts[1]) val += (prefix ? parseFloat((Math.abs(450 - parts[1]) % 360).toFixed(3)) : parts[1]) + 'deg'; | ||
|
||
/* Fix the directions to the legacy syntax */ | ||
if (prefix && parts[4]) { | ||
// `to top` to `bottom` etc. | ||
if (parts[5]) val += DIRECTIONS[parts[5]]; | ||
if (parts[6]) val += ' ' + DIRECTIONS[parts[7]]; | ||
} else if (!prefix && !parts[4]) { | ||
// `top` to `to bottom` etc. | ||
if (parts[5]) val += 'to ' + DIRECTIONS[parts[5]]; | ||
if (parts[6]) val += ' ' + DIRECTIONS[parts[7]]; | ||
} else { | ||
if (parts[3]) val += parts[3]; | ||
} | ||
|
||
return val; | ||
} |
Oops, something went wrong.