@codesandbox/sandpack-client
Version:
<img style="width:100%" src="https://user-images.githubusercontent.com/4838076/143581035-ebee5ba2-9cb1-4fe8-a05b-2f44bd69bb4b.gif" alt="Component toolkit for live running code editing experiences" />
1,321 lines (1,320 loc) • 264 kB
JavaScript
if (
Function.prototype.name === undefined &&
Object.defineProperty !== undefined
) {
Object.defineProperty(Function.prototype, 'name', {
get: function() {
var regex = /function\s([^(]{1,})\(/,
match = regex.exec(this.toString());
return match && match.length > 1 ? match[1].trim() : '';
},
});
}
if (String.prototype.trimRight === undefined) {
String.prototype.trimRight = function() {
return String(this).replace(/\s+$/, '');
};
}
var stylus = (function() {
function require(p) {
var path = require.resolve(p),
mod = require.modules[path];
if (!mod) throw new Error('failed to require "' + p + '"');
if (!mod.exports) {
mod.exports = {};
mod.call(mod.exports, mod, mod.exports, require.relative(path));
}
return mod.exports;
}
var bifs =
"called-from = ()\n\nvendors = moz webkit o ms official\n\n// stringify the given arg\n\n-string(arg)\n type(arg) + ' ' + arg\n\n// require a color\n\nrequire-color(color)\n unless color is a 'color'\n error('RGB or HSL value expected, got a ' + -string(color))\n\n// require a unit\n\nrequire-unit(n)\n unless n is a 'unit'\n error('unit expected, got a ' + -string(n))\n\n// require a string\n\nrequire-string(str)\n unless str is a 'string' or str is a 'ident'\n error('string expected, got a ' + -string(str))\n\n// Math functions\n\nabs(n) { math(n, 'abs') }\nmin(a, b) { a < b ? a : b }\nmax(a, b) { a > b ? a : b }\n\n// Trigonometrics\nPI = -math-prop('PI')\n\nradians-to-degrees(angle)\n angle * (180 / PI)\n\ndegrees-to-radians(angle)\n unit(angle * (PI / 180),'')\n\nsin(n)\n n = degrees-to-radians(n) if unit(n) == 'deg'\n round(math(n, 'sin'), 9)\n\ncos(n)\n n = degrees-to-radians(n) if unit(n) == 'deg'\n round(math(n, 'cos'), 9)\n\n// Rounding Math functions\n\nceil(n, precision = 0)\n multiplier = 10 ** precision\n math(n * multiplier, 'ceil') / multiplier\n\nfloor(n, precision = 0)\n multiplier = 10 ** precision\n math(n * multiplier, 'floor') / multiplier\n\nround(n, precision = 0)\n multiplier = 10 ** precision\n math(n * multiplier, 'round') / multiplier\n\n// return the sum of the given numbers\n\nsum(nums)\n sum = 0\n sum += n for n in nums\n\n// return the average of the given numbers\n\navg(nums)\n sum(nums) / length(nums)\n\n// return a unitless number, or pass through\n\nremove-unit(n)\n if typeof(n) is 'unit'\n unit(n, '')\n else\n n\n\n// convert a percent to a decimal, or pass through\n\npercent-to-decimal(n)\n if unit(n) is '%'\n remove-unit(n) / 100\n else\n n\n\n// check if n is an odd number\n\nodd(n)\n 1 == n % 2\n\n// check if n is an even number\n\neven(n)\n 0 == n % 2\n\n// check if color is light\n\nlight(color)\n lightness(color) >= 50%\n\n// check if color is dark\n\ndark(color)\n lightness(color) < 50%\n\n// desaturate color by amount\n\ndesaturate(color, amount)\n adjust(color, 'saturation', - amount)\n\n// saturate color by amount\n\nsaturate(color = '', amount = 100%)\n if color is a 'color'\n adjust(color, 'saturation', amount)\n else\n unquote( 'saturate(' + color + ')' )\n\n// darken by the given amount\n\ndarken(color, amount)\n adjust(color, 'lightness', - amount)\n\n// lighten by the given amount\n\nlighten(color, amount)\n adjust(color, 'lightness', amount)\n\n// decrease opacity by amount\n\nfade-out(color, amount)\n color - rgba(black, percent-to-decimal(amount))\n\n// increase opacity by amount\n\nfade-in(color, amount)\n color + rgba(black, percent-to-decimal(amount))\n\n// spin hue by a given amount\n\nspin(color, amount)\n color + unit(amount, deg)\n\n// mix two colors by a given amount\n\nmix(color1, color2, weight = 50%)\n unless weight in 0..100\n error('Weight must be between 0% and 100%')\n\n if length(color1) == 2\n weight = color1[0]\n color1 = color1[1]\n\n else if length(color2) == 2\n weight = 100 - color2[0]\n color2 = color2[1]\n\n require-color(color1)\n require-color(color2)\n\n p = unit(weight / 100, '')\n w = p * 2 - 1\n\n a = alpha(color1) - alpha(color2)\n\n w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2\n w2 = 1 - w1\n\n channels = (red(color1) red(color2)) (green(color1) green(color2)) (blue(color1) blue(color2))\n rgb = ()\n\n for pair in channels\n push(rgb, floor(pair[0] * w1 + pair[1] * w2))\n\n a1 = alpha(color1) * p\n a2 = alpha(color2) * (1 - p)\n alpha = a1 + a2\n\n rgba(rgb[0], rgb[1], rgb[2], alpha)\n\n// invert colors, leave alpha intact\n\ninvert(color = '')\n if color is a 'color'\n rgba(#fff - color, alpha(color))\n else\n unquote( 'invert(' + color + ')' )\n\n// give complement of the given color\n\ncomplement( color )\n spin( color, 180 )\n\n// give grayscale of the given color\n\ngrayscale( color = '' )\n if color is a 'color'\n desaturate( color, 100% )\n else\n unquote( 'grayscale(' + color + ')' )\n\n// mix the given color with white\n\ntint( color, percent )\n mix( white, color, percent )\n\n// mix the given color with black\n\nshade( color, percent )\n mix( black, color, percent )\n\n// return the last value in the given expr\n\nlast(expr)\n expr[length(expr) - 1]\n\n// return keys in the given pairs or object\n\nkeys(pairs)\n ret = ()\n if type(pairs) == 'object'\n for key in pairs\n push(ret, key)\n else\n for pair in pairs\n push(ret, pair[0]);\n ret\n\n// return values in the given pairs or object\n\nvalues(pairs)\n ret = ()\n if type(pairs) == 'object'\n for key, val in pairs\n push(ret, val)\n else\n for pair in pairs\n push(ret, pair[1]);\n ret\n\n// join values with the given delimiter\n\njoin(delim, vals...)\n buf = ''\n vals = vals[0] if length(vals) == 1\n for val, i in vals\n buf += i ? delim + val : val\n\n// add a CSS rule to the containing block\n\n// - This definition allows add-property to be used as a mixin\n// - It has the same effect as interpolation but allows users\n// to opt for a functional style\n\nadd-property-function = add-property\nadd-property(name, expr)\n if mixin\n {name} expr\n else\n add-property-function(name, expr)\n\nprefix-classes(prefix)\n -prefix-classes(prefix, block)\n\n// Caching mixin, use inside your functions to enable caching by extending.\n\n$stylus_mixin_cache = {}\ncache()\n $key = (current-media() or 'no-media') + '__' + called-from[0] + '__' + arguments\n if $key in $stylus_mixin_cache\n @extend {'$cache_placeholder_for_' + $stylus_mixin_cache[$key]}\n else if 'cache' in called-from\n {block}\n else\n $id = length($stylus_mixin_cache)\n\n &,\n /$cache_placeholder_for_{$id}\n $stylus_mixin_cache[$key] = $id\n {block}\n\n// Percentage function to convert a number, e.g. '.45', into a percentage, e.g. '45%'\n\npercentage(num)\n return unit(num * 100, '%')\n\n// Returns the position of a `value` within a `list`\n\nindex(list, value)\n for val, i in list\n return i if val == value\n";
require.modules = {};
require.resolve = function(path) {
var orig = path,
reg = path + '.js',
index = path + '/index.js';
return (
(require.modules[reg] && reg) || (require.modules[index] && index) || orig
);
};
require.register = function(path, fn) {
require.modules[path] = fn;
};
require.relative = function(parent) {
return function(p) {
if ('.' != p[0]) return require(p);
var path = parent.split('/'),
segs = p.split('/');
path.pop();
for (var i = 0; i < segs.length; i++) {
var seg = segs[i];
if ('..' == seg) path.pop();
else if ('.' != seg) path.push(seg);
}
return require(path.join('/'));
};
};
require.register('path.js', function(module, exports, require) {
var isWindows = false;
function normalizeArray(parts, allowAboveRoot) {
var up = 0;
for (var i = parts.length - 1; i >= 0; i--) {
var last = parts[i];
if (last == '.') {
parts.splice(i, 1);
} else if (last === '..') {
parts.splice(i, 1);
up++;
} else if (up) {
parts.splice(i, 1);
up--;
}
}
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
}
var splitPathRe = /^([\s\S]+\/(?!$)|\/)?((?:[\s\S]+?)?(\.[^.]*)?)$/;
exports.normalize = function(path) {
var isAbsolute = path.charAt(0) === '/',
trailingSlash = path.slice(-1) === '/';
path = normalizeArray(
path.split('/').filter(function(p) {
return !!p;
}),
!isAbsolute,
).join('/');
if (!path && !isAbsolute) {
path = '.';
}
if (path && trailingSlash) {
path += '/';
}
return (isAbsolute ? '/' : '') + path;
};
exports.join = function() {
var paths = Array.prototype.slice.call(arguments, 0);
return exports.normalize(
paths
.filter(function(p, index) {
return p && typeof p === 'string';
})
.join('/'),
);
};
exports.relative = function(from, to) {
from = exports.resolve(from).substr(1);
to = exports.resolve(to).substr(1);
function trim(arr) {
var start = 0;
for (; start < arr.length; start++) {
if (arr[start] !== '') break;
}
var end = arr.length - 1;
for (; end >= 0; end--) {
if (arr[end] !== '') break;
}
if (start > end) return [];
return arr.slice(start, end - start + 1);
}
var fromParts = trim(from.split('/'));
var toParts = trim(to.split('/'));
var length = Math.min(fromParts.length, toParts.length);
var samePartsLength = length;
for (var i = 0; i < length; i++) {
if (fromParts[i] !== toParts[i]) {
samePartsLength = i;
break;
}
}
var outputParts = [];
for (var i = samePartsLength; i < fromParts.length; i++) {
outputParts.push('..');
}
outputParts = outputParts.concat(toParts.slice(samePartsLength));
return outputParts.join('/');
};
exports.dirname = function(path) {
var dir = splitPathRe.exec(path)[1] || '';
if (!dir) {
return '.';
} else if (
dir.length === 1 ||
(isWindows && dir.length <= 3 && dir.charAt(1) === ':')
) {
return dir;
} else {
return dir.substring(0, dir.length - 1);
}
};
exports.basename = function(path, ext) {
var f = splitPathRe.exec(path)[2] || '';
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
return f;
};
exports.extname = function(path) {
return splitPathRe.exec(path)[3] || '';
};
});
require.register('colors.js', function(module, exports, require) {
module.exports = {
aliceblue: [240, 248, 255, 1],
antiquewhite: [250, 235, 215, 1],
aqua: [0, 255, 255, 1],
aquamarine: [127, 255, 212, 1],
azure: [240, 255, 255, 1],
beige: [245, 245, 220, 1],
bisque: [255, 228, 196, 1],
black: [0, 0, 0, 1],
blanchedalmond: [255, 235, 205, 1],
blue: [0, 0, 255, 1],
blueviolet: [138, 43, 226, 1],
brown: [165, 42, 42, 1],
burlywood: [222, 184, 135, 1],
cadetblue: [95, 158, 160, 1],
chartreuse: [127, 255, 0, 1],
chocolate: [210, 105, 30, 1],
coral: [255, 127, 80, 1],
cornflowerblue: [100, 149, 237, 1],
cornsilk: [255, 248, 220, 1],
crimson: [220, 20, 60, 1],
cyan: [0, 255, 255, 1],
darkblue: [0, 0, 139, 1],
darkcyan: [0, 139, 139, 1],
darkgoldenrod: [184, 134, 11, 1],
darkgray: [169, 169, 169, 1],
darkgreen: [0, 100, 0, 1],
darkgrey: [169, 169, 169, 1],
darkkhaki: [189, 183, 107, 1],
darkmagenta: [139, 0, 139, 1],
darkolivegreen: [85, 107, 47, 1],
darkorange: [255, 140, 0, 1],
darkorchid: [153, 50, 204, 1],
darkred: [139, 0, 0, 1],
darksalmon: [233, 150, 122, 1],
darkseagreen: [143, 188, 143, 1],
darkslateblue: [72, 61, 139, 1],
darkslategray: [47, 79, 79, 1],
darkslategrey: [47, 79, 79, 1],
darkturquoise: [0, 206, 209, 1],
darkviolet: [148, 0, 211, 1],
deeppink: [255, 20, 147, 1],
deepskyblue: [0, 191, 255, 1],
dimgray: [105, 105, 105, 1],
dimgrey: [105, 105, 105, 1],
dodgerblue: [30, 144, 255, 1],
firebrick: [178, 34, 34, 1],
floralwhite: [255, 250, 240, 1],
forestgreen: [34, 139, 34, 1],
fuchsia: [255, 0, 255, 1],
gainsboro: [220, 220, 220, 1],
ghostwhite: [248, 248, 255, 1],
gold: [255, 215, 0, 1],
goldenrod: [218, 165, 32, 1],
gray: [128, 128, 128, 1],
green: [0, 128, 0, 1],
greenyellow: [173, 255, 47, 1],
grey: [128, 128, 128, 1],
honeydew: [240, 255, 240, 1],
hotpink: [255, 105, 180, 1],
indianred: [205, 92, 92, 1],
indigo: [75, 0, 130, 1],
ivory: [255, 255, 240, 1],
khaki: [240, 230, 140, 1],
lavender: [230, 230, 250, 1],
lavenderblush: [255, 240, 245, 1],
lawngreen: [124, 252, 0, 1],
lemonchiffon: [255, 250, 205, 1],
lightblue: [173, 216, 230, 1],
lightcoral: [240, 128, 128, 1],
lightcyan: [224, 255, 255, 1],
lightgoldenrodyellow: [250, 250, 210, 1],
lightgray: [211, 211, 211, 1],
lightgreen: [144, 238, 144, 1],
lightgrey: [211, 211, 211, 1],
lightpink: [255, 182, 193, 1],
lightsalmon: [255, 160, 122, 1],
lightseagreen: [32, 178, 170, 1],
lightskyblue: [135, 206, 250, 1],
lightslategray: [119, 136, 153, 1],
lightslategrey: [119, 136, 153, 1],
lightsteelblue: [176, 196, 222, 1],
lightyellow: [255, 255, 224, 1],
lime: [0, 255, 0, 1],
limegreen: [50, 205, 50, 1],
linen: [250, 240, 230, 1],
magenta: [255, 0, 255, 1],
maroon: [128, 0, 0, 1],
mediumaquamarine: [102, 205, 170, 1],
mediumblue: [0, 0, 205, 1],
mediumorchid: [186, 85, 211, 1],
mediumpurple: [147, 112, 219, 1],
mediumseagreen: [60, 179, 113, 1],
mediumslateblue: [123, 104, 238, 1],
mediumspringgreen: [0, 250, 154, 1],
mediumturquoise: [72, 209, 204, 1],
mediumvioletred: [199, 21, 133, 1],
midnightblue: [25, 25, 112, 1],
mintcream: [245, 255, 250, 1],
mistyrose: [255, 228, 225, 1],
moccasin: [255, 228, 181, 1],
navajowhite: [255, 222, 173, 1],
navy: [0, 0, 128, 1],
oldlace: [253, 245, 230, 1],
olive: [128, 128, 0, 1],
olivedrab: [107, 142, 35, 1],
orange: [255, 165, 0, 1],
orangered: [255, 69, 0, 1],
orchid: [218, 112, 214, 1],
palegoldenrod: [238, 232, 170, 1],
palegreen: [152, 251, 152, 1],
paleturquoise: [175, 238, 238, 1],
palevioletred: [219, 112, 147, 1],
papayawhip: [255, 239, 213, 1],
peachpuff: [255, 218, 185, 1],
peru: [205, 133, 63, 1],
pink: [255, 192, 203, 1],
plum: [221, 160, 221, 1],
powderblue: [176, 224, 230, 1],
purple: [128, 0, 128, 1],
red: [255, 0, 0, 1],
rosybrown: [188, 143, 143, 1],
royalblue: [65, 105, 225, 1],
saddlebrown: [139, 69, 19, 1],
salmon: [250, 128, 114, 1],
sandybrown: [244, 164, 96, 1],
seagreen: [46, 139, 87, 1],
seashell: [255, 245, 238, 1],
sienna: [160, 82, 45, 1],
silver: [192, 192, 192, 1],
skyblue: [135, 206, 235, 1],
slateblue: [106, 90, 205, 1],
slategray: [112, 128, 144, 1],
slategrey: [112, 128, 144, 1],
snow: [255, 250, 250, 1],
springgreen: [0, 255, 127, 1],
steelblue: [70, 130, 180, 1],
tan: [210, 180, 140, 1],
teal: [0, 128, 128, 1],
thistle: [216, 191, 216, 1],
tomato: [255, 99, 71, 1],
transparent: [0, 0, 0, 0],
turquoise: [64, 224, 208, 1],
violet: [238, 130, 238, 1],
wheat: [245, 222, 179, 1],
white: [255, 255, 255, 1],
whitesmoke: [245, 245, 245, 1],
yellow: [255, 255, 0, 1],
yellowgreen: [154, 205, 50, 1],
rebeccapurple: [102, 51, 153, 1],
};
});
require.register('errors.js', function(module, exports, require) {
exports.ParseError = ParseError;
exports.SyntaxError = SyntaxError;
SyntaxError.prototype.__proto__ = Error.prototype;
function ParseError(msg) {
this.name = 'ParseError';
this.message = msg;
Error.captureStackTrace(this, ParseError);
}
ParseError.prototype.__proto__ = Error.prototype;
function SyntaxError(msg) {
this.name = 'SyntaxError';
this.message = msg;
Error.captureStackTrace(this, ParseError);
}
SyntaxError.prototype.__proto__ = Error.prototype;
});
require.register('units.js', function(module, exports, require) {
module.exports = [
'em',
'ex',
'ch',
'rem',
'vw',
'vh',
'vmin',
'vmax',
'cm',
'mm',
'in',
'pt',
'pc',
'px',
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
'x',
'%',
'fr',
];
});
require.register('functions/index.js', function(module, exports, require) {
exports['add-property'] = require('./add-property');
exports.adjust = require('./adjust');
exports.alpha = require('./alpha');
exports['base-convert'] = require('./base-convert');
exports.basename = require('./basename');
exports.blend = require('./blend');
exports.blue = require('./blue');
exports.clone = require('./clone');
exports.component = require('./component');
exports.contrast = require('./contrast');
exports.convert = require('./convert');
exports['current-media'] = require('./current-media');
exports.define = require('./define');
exports.dirname = require('./dirname');
exports.error = require('./error');
exports.extname = require('./extname');
exports.green = require('./green');
exports.hsl = require('./hsl');
exports.hsla = require('./hsla');
exports.hue = require('./hue');
exports.length = require('./length');
exports.lightness = require('./lightness');
exports['list-separator'] = require('./list-separator');
exports.lookup = require('./lookup');
exports.luminosity = require('./luminosity');
exports.match = require('./match');
exports.math = require('./math');
exports.merge = exports.extend = require('./merge');
exports.operate = require('./operate');
exports['opposite-position'] = require('./opposite-position');
exports.p = require('./p');
exports.pathjoin = require('./pathjoin');
exports.pop = require('./pop');
exports.push = exports.append = require('./push');
exports.range = require('./range');
exports.red = require('./red');
exports.remove = require('./remove');
exports.replace = require('./replace');
exports.rgb = require('./rgb');
exports.rgba = require('./rgba');
exports.s = require('./s');
exports.saturation = require('./saturation');
exports['selector-exists'] = require('./selector-exists');
exports.selector = require('./selector');
exports.selectors = require('./selectors');
exports.shift = require('./shift');
exports.split = require('./split');
exports.substr = require('./substr');
exports.slice = require('./slice');
exports.tan = require('./tan');
exports.trace = require('./trace');
exports.transparentify = require('./transparentify');
exports.type = exports.typeof = exports['type-of'] = require('./type');
exports.unit = require('./unit');
exports.unquote = require('./unquote');
exports.unshift = exports.prepend = require('./unshift');
exports.warn = require('./warn');
exports['-math-prop'] = require('./math-prop');
exports['-prefix-classes'] = require('./prefix-classes');
});
require.register('functions/url.js', function(module, exports, require) {
var Compiler = require('../visitor/compiler'),
events = require('../renderer').events,
nodes = require('../nodes'),
extname = require('../path').extname,
utils = require('../utils');
var defaultMimes = {
'.gif': 'image/gif',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.svg': 'image/svg+xml',
'.webp': 'image/webp',
'.ttf': 'application/x-font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.woff': 'application/font-woff',
'.woff2': 'application/font-woff2',
};
var encodingTypes = { BASE_64: 'base64', UTF8: 'charset=utf-8' };
module.exports = function(options) {
options = options || {};
var _paths = options.paths || [];
var sizeLimit = null != options.limit ? options.limit : 3e4;
var mimes = options.mimes || defaultMimes;
function fn(url, enc) {
var compiler = new Compiler(url),
encoding = encodingTypes.BASE_64;
compiler.isURL = true;
url = url.nodes
.map(function(node) {
return compiler.visit(node);
})
.join('');
url = parse(url);
var ext = extname(url.pathname),
mime = mimes[ext],
hash = url.hash || '',
literal = new nodes.Literal('url("' + url.href + '")'),
paths = _paths.concat(this.paths),
buf,
result;
if (!mime) return literal;
if (url.protocol) return literal;
var found = utils.lookup(url.pathname, paths);
if (!found) {
events.emit(
'file not found',
'File ' + literal + ' could not be found, literal url retained!',
);
return literal;
}
buf = fs.readFileSync(found);
if (false !== sizeLimit && buf.length > sizeLimit) return literal;
if (enc && 'utf8' == enc.first.val.toLowerCase()) {
encoding = encodingTypes.UTF8;
result = buf
.toString('utf8')
.replace(/\s+/g, ' ')
.replace(/[{}\|\\\^~\[\]`"<>#%]/g, function(match) {
return '%' + match[0].charCodeAt(0).toString(16).toUpperCase();
})
.trim();
} else {
result = buf.toString(encoding) + hash;
}
return new nodes.Literal(
'url("data:' + mime + ';' + encoding + ',' + result + '")',
);
}
fn.raw = true;
return fn;
};
module.exports.mimes = defaultMimes;
});
require.register('functions/add-property.js', function(
module,
exports,
require,
) {
var utils = require('../utils'),
nodes = require('../nodes');
(module.exports = function addProperty(name, expr) {
utils.assertType(name, 'expression', 'name');
name = utils.unwrap(name).first;
utils.assertString(name, 'name');
utils.assertType(expr, 'expression', 'expr');
var prop = new nodes.Property([name], expr);
var block = this.closestBlock;
var len = block.nodes.length,
head = block.nodes.slice(0, block.index),
tail = block.nodes.slice(block.index++, len);
head.push(prop);
block.nodes = head.concat(tail);
return prop;
}).raw = true;
});
require.register('functions/adjust.js', function(module, exports, require) {
var utils = require('../utils');
module.exports = function adjust(color, prop, amount) {
utils.assertColor(color, 'color');
utils.assertString(prop, 'prop');
utils.assertType(amount, 'unit', 'amount');
var hsl = color.hsla.clone();
prop = { hue: 'h', saturation: 's', lightness: 'l' }[prop.string];
if (!prop) throw new Error('invalid adjustment property');
var val = amount.val;
if ('%' == amount.type) {
val =
'l' == prop && val > 0
? (100 - hsl[prop]) * val / 100
: hsl[prop] * (val / 100);
}
hsl[prop] += val;
return hsl.rgba;
};
});
require.register('functions/alpha.js', function(module, exports, require) {
var nodes = require('../nodes'),
rgba = require('./rgba');
module.exports = function alpha(color, value) {
color = color.rgba;
if (value) {
return rgba(
new nodes.Unit(color.r),
new nodes.Unit(color.g),
new nodes.Unit(color.b),
value,
);
}
return new nodes.Unit(color.a, '');
};
});
require.register('functions/base-convert.js', function(
module,
exports,
require,
) {
var utils = require('../utils'),
nodes = require('../nodes');
(module.exports = function(num, base, width) {
utils.assertPresent(num, 'number');
utils.assertPresent(base, 'base');
num = utils.unwrap(num).nodes[0].val;
base = utils.unwrap(base).nodes[0].val;
width = (width && utils.unwrap(width).nodes[0].val) || 2;
var result = Number(num).toString(base);
while (result.length < width) {
result = '0' + result;
}
return new nodes.Literal(result);
}).raw = true;
});
require.register('functions/basename.js', function(module, exports, require) {
var utils = require('../utils'),
path = require('../path');
module.exports = function basename(p, ext) {
utils.assertString(p, 'path');
return path.basename(p.val, ext && ext.val);
};
});
require.register('functions/blend.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes');
module.exports = function blend(top, bottom) {
utils.assertColor(top);
top = top.rgba;
bottom = bottom || new nodes.RGBA(255, 255, 255, 1);
utils.assertColor(bottom);
bottom = bottom.rgba;
return new nodes.RGBA(
top.r * top.a + bottom.r * (1 - top.a),
top.g * top.a + bottom.g * (1 - top.a),
top.b * top.a + bottom.b * (1 - top.a),
top.a + bottom.a - top.a * bottom.a,
);
};
});
require.register('functions/blue.js', function(module, exports, require) {
var nodes = require('../nodes'),
rgba = require('./rgba');
module.exports = function blue(color, value) {
color = color.rgba;
if (value) {
return rgba(
new nodes.Unit(color.r),
new nodes.Unit(color.g),
value,
new nodes.Unit(color.a),
);
}
return new nodes.Unit(color.b, '');
};
});
require.register('functions/clone.js', function(module, exports, require) {
var utils = require('../utils');
(module.exports = function clone(expr) {
utils.assertPresent(expr, 'expr');
return expr.clone();
}).raw = true;
});
require.register('functions/component.js', function(
module,
exports,
require,
) {
var utils = require('../utils'),
nodes = require('../nodes');
var componentMap = {
red: 'r',
green: 'g',
blue: 'b',
alpha: 'a',
hue: 'h',
saturation: 's',
lightness: 'l',
};
var unitMap = { hue: 'deg', saturation: '%', lightness: '%' };
var typeMap = {
red: 'rgba',
blue: 'rgba',
green: 'rgba',
alpha: 'rgba',
hue: 'hsla',
saturation: 'hsla',
lightness: 'hsla',
};
module.exports = function component(color, name) {
utils.assertColor(color, 'color');
utils.assertString(name, 'name');
var name = name.string,
unit = unitMap[name],
type = typeMap[name],
name = componentMap[name];
if (!name) throw new Error('invalid color component "' + name + '"');
return new nodes.Unit(color[type][name], unit);
};
});
require.register('functions/contrast.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes'),
blend = require('./blend'),
luminosity = require('./luminosity');
module.exports = function contrast(top, bottom) {
if ('rgba' != top.nodeName && 'hsla' != top.nodeName) {
return new nodes.Literal(
'contrast(' + (top.isNull ? '' : top.toString()) + ')',
);
}
var result = new nodes.Object();
top = top.rgba;
bottom = bottom || new nodes.RGBA(255, 255, 255, 1);
utils.assertColor(bottom);
bottom = bottom.rgba;
function contrast(top, bottom) {
if (1 > top.a) {
top = blend(top, bottom);
}
var l1 = luminosity(bottom).val + 0.05,
l2 = luminosity(top).val + 0.05,
ratio = l1 / l2;
if (l2 > l1) {
ratio = 1 / ratio;
}
return Math.round(ratio * 10) / 10;
}
if (1 <= bottom.a) {
var resultRatio = new nodes.Unit(contrast(top, bottom));
result.set('ratio', resultRatio);
result.set('error', new nodes.Unit(0));
result.set('min', resultRatio);
result.set('max', resultRatio);
} else {
var onBlack = contrast(top, blend(bottom, new nodes.RGBA(0, 0, 0, 1))),
onWhite = contrast(
top,
blend(bottom, new nodes.RGBA(255, 255, 255, 1)),
),
max = Math.max(onBlack, onWhite);
function processChannel(topChannel, bottomChannel) {
return Math.min(
Math.max(
0,
(topChannel - bottomChannel * bottom.a) / (1 - bottom.a),
),
255,
);
}
var closest = new nodes.RGBA(
processChannel(top.r, bottom.r),
processChannel(top.g, bottom.g),
processChannel(top.b, bottom.b),
1,
);
var min = contrast(top, blend(bottom, closest));
result.set('ratio', new nodes.Unit(Math.round((min + max) * 50) / 100));
result.set('error', new nodes.Unit(Math.round((max - min) * 50) / 100));
result.set('min', new nodes.Unit(min));
result.set('max', new nodes.Unit(max));
}
return result;
};
});
require.register('functions/convert.js', function(module, exports, require) {
var utils = require('../utils');
module.exports = function convert(str) {
utils.assertString(str, 'str');
return utils.parseString(str.string);
};
});
require.register('functions/current-media.js', function(
module,
exports,
require,
) {
var nodes = require('../nodes');
module.exports = function currentMedia() {
var self = this;
return new nodes.String(lookForMedia(this.closestBlock.node) || '');
function lookForMedia(node) {
if ('media' == node.nodeName) {
node.val = self.visit(node.val);
return node.toString();
} else if (node.block.parent.node) {
return lookForMedia(node.block.parent.node);
}
}
};
});
require.register('functions/define.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes');
module.exports = function define(name, expr, global) {
utils.assertType(name, 'string', 'name');
expr = utils.unwrap(expr);
var scope = this.currentScope;
if (global && global.toBoolean().isTrue) {
scope = this.global.scope;
}
var node = new nodes.Ident(name.val, expr);
scope.add(node);
return nodes.nil;
};
});
require.register('functions/dirname.js', function(module, exports, require) {
var utils = require('../utils'),
path = require('../path');
module.exports = function dirname(p) {
utils.assertString(p, 'path');
return path.dirname(p.val).replace(/\\/g, '/');
};
});
require.register('functions/error.js', function(module, exports, require) {
var utils = require('../utils');
module.exports = function error(msg) {
utils.assertType(msg, 'string', 'msg');
var err = new Error(msg.val);
err.fromStylus = true;
throw err;
};
});
require.register('functions/extname.js', function(module, exports, require) {
var utils = require('../utils'),
path = require('../path');
module.exports = function extname(p) {
utils.assertString(p, 'path');
return path.extname(p.val);
};
});
require.register('functions/green.js', function(module, exports, require) {
var nodes = require('../nodes'),
rgba = require('./rgba');
module.exports = function green(color, value) {
color = color.rgba;
if (value) {
return rgba(
new nodes.Unit(color.r),
value,
new nodes.Unit(color.b),
new nodes.Unit(color.a),
);
}
return new nodes.Unit(color.g, '');
};
});
require.register('functions/hsl.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes'),
hsla = require('./hsla');
module.exports = function hsl(hue, saturation, lightness) {
if (1 == arguments.length) {
utils.assertColor(hue, 'color');
return hue.hsla;
} else {
return hsla(hue, saturation, lightness, new nodes.Unit(1));
}
};
});
require.register('functions/hsla.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes');
module.exports = function hsla(hue, saturation, lightness, alpha) {
switch (arguments.length) {
case 1:
utils.assertColor(hue);
return hue.hsla;
case 2:
utils.assertColor(hue);
var color = hue.hsla;
utils.assertType(saturation, 'unit', 'alpha');
var alpha = saturation.clone();
if ('%' == alpha.type) alpha.val /= 100;
return new nodes.HSLA(color.h, color.s, color.l, alpha.val);
default:
utils.assertType(hue, 'unit', 'hue');
utils.assertType(saturation, 'unit', 'saturation');
utils.assertType(lightness, 'unit', 'lightness');
utils.assertType(alpha, 'unit', 'alpha');
var alpha = alpha.clone();
if (alpha && '%' == alpha.type) alpha.val /= 100;
return new nodes.HSLA(
hue.val,
saturation.val,
lightness.val,
alpha.val,
);
}
};
});
require.register('functions/hue.js', function(module, exports, require) {
var nodes = require('../nodes'),
hsla = require('./hsla'),
component = require('./component');
module.exports = function hue(color, value) {
if (value) {
var hslaColor = color.hsla;
return hsla(
value,
new nodes.Unit(hslaColor.s),
new nodes.Unit(hslaColor.l),
new nodes.Unit(hslaColor.a),
);
}
return component(color, new nodes.String('hue'));
};
});
require.register('functions/length.js', function(module, exports, require) {
var utils = require('../utils');
(module.exports = function length(expr) {
if (expr) {
if (expr.nodes) {
var nodes = utils.unwrap(expr).nodes;
if (1 == nodes.length && 'object' == nodes[0].nodeName) {
return nodes[0].length;
} else {
return nodes.length;
}
} else {
return 1;
}
}
return 0;
}).raw = true;
});
require.register('functions/lightness.js', function(
module,
exports,
require,
) {
var nodes = require('../nodes'),
hsla = require('./hsla'),
component = require('./component');
module.exports = function lightness(color, value) {
if (value) {
var hslaColor = color.hsla;
return hsla(
new nodes.Unit(hslaColor.h),
new nodes.Unit(hslaColor.s),
value,
new nodes.Unit(hslaColor.a),
);
}
return component(color, new nodes.String('lightness'));
};
});
require.register('functions/list-separator.js', function(
module,
exports,
require,
) {
var utils = require('../utils'),
nodes = require('../nodes');
(module.exports = function listSeparator(list) {
list = utils.unwrap(list);
return new nodes.String(list.isList ? ',' : ' ');
}).raw = true;
});
require.register('functions/lookup.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes');
module.exports = function lookup(name) {
utils.assertType(name, 'string', 'name');
var node = this.lookup(name.val);
if (!node) return nodes.nil;
return this.visit(node);
};
});
require.register('functions/luminosity.js', function(
module,
exports,
require,
) {
var utils = require('../utils'),
nodes = require('../nodes');
module.exports = function luminosity(color) {
utils.assertColor(color);
color = color.rgba;
function processChannel(channel) {
channel = channel / 255;
return 0.03928 > channel
? channel / 12.92
: Math.pow((channel + 0.055) / 1.055, 2.4);
}
return new nodes.Unit(
0.2126 * processChannel(color.r) +
0.7152 * processChannel(color.g) +
0.0722 * processChannel(color.b),
);
};
});
require.register('functions/match.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes');
var VALID_FLAGS = 'igm';
module.exports = function match(pattern, val, flags) {
utils.assertType(pattern, 'string', 'pattern');
utils.assertString(val, 'val');
var re = new RegExp(
pattern.val,
validateFlags(flags) ? flags.string : '',
);
return val.string.match(re);
};
function validateFlags(flags) {
flags = flags && flags.string;
if (flags) {
return flags.split('').every(function(flag) {
return ~VALID_FLAGS.indexOf(flag);
});
}
return false;
}
});
require.register('functions/math-prop.js', function(
module,
exports,
require,
) {
var nodes = require('../nodes');
module.exports = function math(prop) {
return new nodes.Unit(Math[prop.string]);
};
});
require.register('functions/math.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes');
module.exports = function math(n, fn) {
utils.assertType(n, 'unit', 'n');
utils.assertString(fn, 'fn');
return new nodes.Unit(Math[fn.string](n.val), n.type);
};
});
require.register('functions/merge.js', function(module, exports, require) {
var utils = require('../utils');
(module.exports = function merge(dest) {
utils.assertPresent(dest, 'dest');
dest = utils.unwrap(dest).first;
utils.assertType(dest, 'object', 'dest');
var last = utils.unwrap(arguments[arguments.length - 1]).first,
deep = true === last.val;
for (var i = 1, len = arguments.length - deep; i < len; ++i) {
utils.merge(dest.vals, utils.unwrap(arguments[i]).first.vals, deep);
}
return dest;
}).raw = true;
});
require.register('functions/operate.js', function(module, exports, require) {
var utils = require('../utils');
module.exports = function operate(op, left, right) {
utils.assertType(op, 'string', 'op');
utils.assertPresent(left, 'left');
utils.assertPresent(right, 'right');
return left.operate(op.val, right);
};
});
require.register('functions/opposite-position.js', function(
module,
exports,
require,
) {
var utils = require('../utils'),
nodes = require('../nodes');
(module.exports = function oppositePosition(positions) {
var expr = [];
utils.unwrap(positions).nodes.forEach(function(pos, i) {
utils.assertString(pos, 'position ' + i);
pos = (function() {
switch (pos.string) {
case 'top':
return 'bottom';
case 'bottom':
return 'top';
case 'left':
return 'right';
case 'right':
return 'left';
case 'center':
return 'center';
default:
throw new Error('invalid position ' + pos);
}
})();
expr.push(new nodes.Literal(pos));
});
return expr;
}).raw = true;
});
require.register('functions/p.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes');
(module.exports = function p() {
[].slice.call(arguments).forEach(function(expr) {
expr = utils.unwrap(expr);
if (!expr.nodes.length) return;
console.log(
'[90minspect:[0m %s',
expr.toString().replace(/^\(|\)$/g, ''),
);
});
return nodes.nil;
}).raw = true;
});
require.register('functions/pathjoin.js', function(module, exports, require) {
var path = require('../path');
(module.exports = function pathjoin() {
var paths = [].slice.call(arguments).map(function(path) {
return path.first.string;
});
return path.join.apply(null, paths).replace(/\\/g, '/');
}).raw = true;
});
require.register('functions/pop.js', function(module, exports, require) {
var utils = require('../utils');
(module.exports = function pop(expr) {
expr = utils.unwrap(expr);
return expr.nodes.pop();
}).raw = true;
});
require.register('functions/prefix-classes.js', function(
module,
exports,
require,
) {
var utils = require('../utils');
module.exports = function prefixClasses(prefix, block) {
utils.assertString(prefix, 'prefix');
utils.assertType(block, 'block', 'block');
var _prefix = this.prefix;
this.options.prefix = this.prefix = prefix.string;
block = this.visit(block);
this.options.prefix = this.prefix = _prefix;
return block;
};
});
require.register('functions/push.js', function(module, exports, require) {
var utils = require('../utils');
(module.exports = function(expr) {
expr = utils.unwrap(expr);
for (var i = 1, len = arguments.length; i < len; ++i) {
expr.nodes.push(utils.unwrap(arguments[i]).clone());
}
return expr.nodes.length;
}).raw = true;
});
require.register('functions/range.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes');
module.exports = function range(start, stop, step) {
utils.assertType(start, 'unit', 'start');
utils.assertType(stop, 'unit', 'stop');
if (step) {
utils.assertType(step, 'unit', 'step');
if (0 == step.val) {
throw new Error('ArgumentError: "step" argument must not be zero');
}
} else {
step = new nodes.Unit(1);
}
var list = new nodes.Expression();
for (var i = start.val; i <= stop.val; i += step.val) {
list.push(new nodes.Unit(i, start.type));
}
return list;
};
});
require.register('functions/red.js', function(module, exports, require) {
var nodes = require('../nodes'),
rgba = require('./rgba');
module.exports = function red(color, value) {
color = color.rgba;
if (value) {
return rgba(
value,
new nodes.Unit(color.g),
new nodes.Unit(color.b),
new nodes.Unit(color.a),
);
}
return new nodes.Unit(color.r, '');
};
});
require.register('functions/remove.js', function(module, exports, require) {
var utils = require('../utils');
module.exports = function remove(object, key) {
utils.assertType(object, 'object', 'object');
utils.assertString(key, 'key');
delete object.vals[key.string];
return object;
};
});
require.register('functions/replace.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes');
module.exports = function replace(pattern, replacement, val) {
utils.assertString(pattern, 'pattern');
utils.assertString(replacement, 'replacement');
utils.assertString(val, 'val');
pattern = new RegExp(pattern.string, 'g');
var res = val.string.replace(pattern, replacement.string);
return val instanceof nodes.Ident
? new nodes.Ident(res)
: new nodes.String(res);
};
});
require.register('functions/rgb.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes'),
rgba = require('./rgba');
module.exports = function rgb(red, green, blue) {
switch (arguments.length) {
case 1:
utils.assertColor(red);
var color = red.rgba;
return new nodes.RGBA(color.r, color.g, color.b, 1);
default:
return rgba(red, green, blue, new nodes.Unit(1));
}
};
});
require.register('functions/rgba.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes');
module.exports = function rgba(red, green, blue, alpha) {
switch (arguments.length) {
case 1:
utils.assertColor(red);
return red.rgba;
case 2:
utils.assertColor(red);
var color = red.rgba;
utils.assertType(green, 'unit', 'alpha');
alpha = green.clone();
if ('%' == alpha.type) alpha.val /= 100;
return new nodes.RGBA(color.r, color.g, color.b, alpha.val);
default:
utils.assertType(red, 'unit', 'red');
utils.assertType(green, 'unit', 'green');
utils.assertType(blue, 'unit', 'blue');
utils.assertType(alpha, 'unit', 'alpha');
var r = '%' == red.type ? Math.round(red.val * 2.55) : red.val,
g = '%' == green.type ? Math.round(green.val * 2.55) : green.val,
b = '%' == blue.type ? Math.round(blue.val * 2.55) : blue.val;
alpha = alpha.clone();
if (alpha && '%' == alpha.type) alpha.val /= 100;
return new nodes.RGBA(r, g, b, alpha.val);
}
};
});
require.register('functions/s.js', function(module, exports, require) {
var utils = require('../utils'),
nodes = require('../nodes'),
Compiler = require('../visitor/compiler');
(module.exports = function s(fmt) {
fmt = utils.unwrap(fmt).nodes[0];
utils.assertString(fmt);
var self = this,
str = fmt.string,
args = arguments,
i = 1;
str = str.replace(/%(s|d)/g, function(_, specifier) {
var arg = args[i++] || nodes.nil;
switch (specifier) {
case 's':
return new Compiler(arg, self.options).compile();
case 'd':
arg = utils.unwrap(arg).first;
if ('unit' != arg.nodeName) throw new Error('%d requires a unit');
return arg.val;
}
});
return new nodes.Literal(str);
}).raw = true;
});
require.register('functions/saturation.js', function(
module,
exports,
require,
) {
var nodes = require('../nodes'),
hsla = require('./hsla'),
component = require('./component');
module.exports = function saturation(color, value) {
if (value) {
var hslaColor = color.hsla;
return hsla(
new nodes.Unit(hslaColor.h),
value,
new nodes.Unit(hslaColor.l),
new nodes.Unit(hslaColor.a),
);
}
return component(color, new nodes.String('saturation'));
};
});
require.register('functions/selector-exists.js', function(
module,
exports,
require,
) {
var utils = require('../utils');
module.exports = function selectorExists(sel) {
utils.assertString(sel, 'selector');
if (!this.__selectorsMap__) {
var Normalizer = require('../visitor/normalizer'),
visitor = new Normalizer(this.root.clone());
visitor.visit(visitor.root);
this.__selectorsMap__ = visitor.map;
}
return sel.string in this.__selectorsMap__;
};
});
require.register('functions/selector.js', function(module, exports, require) {
var utils = require('../utils');
(module.exports = function selector() {
var stack = this.selectorStack,
args = [].slice.call(arguments);
if (1 == args.length) {
var expr = utils.unwrap(args[0]),
len = expr.nodes.length;
if (1 == len) {
utils.assertString(expr.first, 'selector');
var SelectorParser = require('../selector-parser'),
val = expr.first.string,
parsed = new SelectorParser(val).parse().val;
if (parsed == val) return val;
stack.push(parse(val));
} else if (len > 1) {
if (expr.isList) {
pushToStack(expr.nodes, stack);
} else {
stack.push(
parse(
expr.nodes
.map(function(node) {
utils.assertString(node, 'selector');
return node.string;
})
.join(' '),
),
);
}
}
} else if (args.length > 1) {
pushToStack(args, stack);
}
return stack.length ? utils.compileSelectors(stack).join(',') : '&';
}).raw = true;
function pushToStack(selectors, stack) {
selectors.forEach(function(sel) {
sel = sel.first;
utils.assertString(sel, 'selector');
stack.push(parse(sel.string));
});
}
function parse(selector) {