Permalink
Browse files

refactoring + code style fix

  • Loading branch information...
1 parent fdf2c39 commit dfc5d9eb603185d335791246a02152da6d95c896 @Panya Panya committed with slang800 Oct 16, 2012
Showing with 43 additions and 40 deletions.
  1. +43 −40 lib/nodes/vendor-helpers.js
View
83 lib/nodes/vendor-helpers.js
@@ -4,69 +4,72 @@
*/
var stylus = require('stylus')
- , nodes = stylus.nodes
- , utils = stylus.utils
+ , 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`.
*/
-module.exports = function(property,value,prefix) {
- var result = value.toString();
+exports = module.exports = normalize;
+
+function normalize(property, value, prefix){
+ var result = value.toString()
+ , args;
/* Fixing the gradients */
- if (result.indexOf('gradient(') > -1) {
+ if (~result.indexOf('gradient(')) {
/* Normalize color stops */
- result = result.replace(/([\(\,]\s*)(-?(?:\d*\.)?\d+(?:%|px|em))(\s+)((hsl|rgb)a?\([^\)]+\)|#[^\)\,]+)/g,'$1$4$3$2');
+ result = result.replace(RE_GRADIENT_STOPS,'$1$4$3$2');
/* Normalize legacy gradients */
- result = result.replace(/(\(\s*)(?:(-?(\d*\.)?\d+)deg|((to )?(top|bottom|left|right)( (top|bottom|left|right))?))/g,function(match,p1,p2,p3,p4,p5,p6,p7,p8){
- /* Fix the degrees to the legacy syntax */
- var result = p1;
- if (p2) {
- result += (prefix ? parseFloat((Math.abs(450 - p2) % 360).toFixed(3)) : p2) + 'deg';
- }
-
- /* Fix the directions to the legacy syntax */
- var sides = {'top':'bottom','bottom':'top','left':'right','right':'left'};
- if (prefix && p5) {
- // `to top` to `bottom` etc.
- if (p6) {
- result += sides[p6];
- }
- if (p7) {
- result += ' ' + sides[p8];
- }
- } else if (!prefix && !p5) {
- // `top` to `to bottom` etc.
- if (p6) {
- result += 'to ' + sides[p6];
- }
- if (p7) {
- result += ' ' + sides[p8];
- }
- } else if (p4) {
- result += p4;
- }
- return result;
+ 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(/((repeating-)?(linear|radial)-gradient\()/g,'-' + prefix + '-$1');
- }
+ 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(/\b(transform)\b/g,'-' + prefix + '-$1');
+ 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(/\s*\b(fill)\b\s*/g,' ');
+ 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;
+}

0 comments on commit dfc5d9e

Please sign in to comment.