Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Import url plugin from stylus

  • Loading branch information...
commit ff4167de3479562e7dd43f27b5d4ead6196b95cd 1 parent c1e8655
Kevin Decker authored
Showing with 120 additions and 0 deletions.
  1. +22 −0 LICENSE.stylus
  2. +98 −0 lib/url.js
22 LICENSE.stylus
View
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2010 LearnBoost <dev@learnboost.com>
+
+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.
98 lib/url.js
View
@@ -0,0 +1,98 @@
+
+/*!
+ * Stylus - plugin - url
+ * Copyright(c) 2010 LearnBoost <dev@learnboost.com>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var Compiler = require('../visitor/compiler')
+ , nodes = require('../nodes')
+ , parse = require('url').parse
+ , extname = require('path').extname
+ , utils = require('../utils')
+ , fs = require('fs');
+
+/**
+ * Mime table.
+ */
+
+var mimes = {
+ '.gif': 'image/gif'
+ , '.png': 'image/png'
+ , '.jpg': 'image/jpeg'
+ , '.jpeg': 'image/jpeg'
+ , '.svg': 'image/svg+xml'
+};
+
+/**
+ * Return a url() function with the given `options`.
+ *
+ * Options:
+ *
+ * - `limit` bytesize limit defaulting to 30Kb
+ * - `paths` image resolution path(s), merged with general lookup paths
+ *
+ * Examples:
+ *
+ * stylus(str)
+ * .set('filename', __dirname + '/css/test.styl')
+ * .define('url', stylus.url({ paths: [__dirname + '/public'] }))
+ * .render(function(err, css){ ... })
+ *
+ * @param {Object} options
+ * @return {Function}
+ * @api public
+ */
+
+module.exports = function(options) {
+ options = options || {};
+
+ var sizeLimit = options.limit || 30000
+ , _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)
+ , ext = extname(url.pathname)
+ , mime = mimes[ext]
+ , literal = new nodes.Literal('url("' + url.href + '")')
+ , paths = _paths.concat(this.paths)
+ , founds
+ , buf;
+
+ // Not supported
+ if (!mime) return literal;
+
+ // Absolute
+ if (url.protocol) return literal;
+
+ // Lookup
+ found = utils.lookup(url.pathname, paths);
+
+ // Failed to lookup
+ if (!found) return literal;
+
+ // Read data
+ buf = fs.readFileSync(found);
+
+ // To large
+ if (buf.length > sizeLimit) return literal;
+
+ // Encode
+ return new nodes.Literal('url("data:' + mime + ';base64,' + buf.toString('base64') + '")');
+ };
+
+ url.raw = true;
+ return url;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.