Sha256: aa0b6a9ea97fd1140d6b8340c996a435c694e1d6ffe466861866cdf72e53fe33

Contents?: true

Size: 1.7 KB

Versions: 20

Compression:

Stored size: 1.7 KB

Contents

/**
 * Module dependencies.
 */

var Compiler = require('../visitor/compiler')
  , nodes = require('../nodes')
  , parse = require('url').parse
  , relative = require('path').relative
  , dirname = require('path').dirname
  , extname = require('path').extname
  , utils = require('../utils');

/**
 * Return a url() function with the given `options`.
 *
 * Options:
 *
 *    - `paths` resolution path(s), merged with general lookup paths
 *
 * Examples:
 *
 *    stylus(str)
 *      .set('filename', __dirname + '/css/test.styl')
 *      .define('url', stylus.resolver({ paths: [__dirname + '/public'] }))
 *      .render(function(err, css){ ... })
 *
 * @param {Object} options
 * @return {Function}
 * @api public
 */

module.exports = function(options) {
  options = options || {};

  var _paths = options.paths || [];

  function url(url) {
    // Compile the url
    var compiler = new Compiler(url);
    compiler.isURL = true;
    var url = url.nodes.map(function(node){
      return compiler.visit(node);
    }).join('');

    // Parse literal 
    var url = parse(url)
      , literal = new nodes.Literal('url("' + url.href + '")')
      , paths = _paths.concat(this.paths)
      , tail = ''
      , found;

    // Absolute
    if (url.protocol) return literal;

    // Lookup
    var found = utils.lookup(url.pathname, paths, '', true);

    // Failed to lookup
    if (!found) return literal;

    if (url.search) tail += url.search;
    if (url.hash) tail += url.hash;

    if (this.includeCSS && extname(found) == '.css') {
      return new nodes.Literal(found + tail);
    } else {
      return new nodes.Literal('url("' + relative(dirname(this.filename), found) + tail + '")');
    }
  };

  url.raw = true;
  return url;
};

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
ela-4.1.6 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-4.1.5 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-4.1.4 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-4.1.3 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-4.1.2 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-4.1.1 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-4.1.0 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-4.0.0 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-3.4.3 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-3.4.2 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-3.4.0 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-3.3.1 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-3.3.0 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-3.2.0 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-3.1.1 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-3.1.0 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-3.0.0 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-2.0.0 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
ela-1.1.0 node_modules/codo/node_modules/stylus/lib/functions/resolver.js
stylus-source-0.38.0 vendor/lib/functions/resolver.js