Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
Initial commit!
Browse files Browse the repository at this point in the history
  • Loading branch information
jansepar committed Dec 31, 2014
0 parents commit e350f93
Show file tree
Hide file tree
Showing 17 changed files with 12,014 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
node_modules/
bower_components/
*.bak
*.log
137 changes: 137 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,137 @@
module.exports = function(grunt) {
grunt.initConfig({
express: {
custom: {
options: {
hostname: '0.0.0.0',
port: 3000,
base: '.',
debug: true
}
}
},
qunit: {
all: {
options: {
timeout: 20000,
urls: [
'http://localhost:3000/tests/index.html',
]
}
}
},
'saucelabs-qunit': {
all: {
options: {
// Set the Saucelabs username and key in your environment variables
// by setting SAUCE_USERNAME and SAUCE_ACCESS_KEY
urls: [
'http://localhost:3000/tests/index.html'
],
concurrency: 16,
tunneled: true,
detailedError: true,
browsers: [ //https://saucelabs.com/docs/platforms
{ // Only working version of IE compatable
browserName: 'internet explorer',
platform: 'Windows 2012',
version: '10'
},
{ // Only working version of IE compatable
browserName: 'internet explorer',
platform: 'Windows 8.1',
version: '11'
},
{ // Lowest known working version of FF
browserName: 'opera',
platform: 'Windows 2003',
version: '11'
},
{ // Highest known working version of Opera
browserName: 'opera',
platform: 'Windows 2008',
version: '12'
},
{ // Latest Chrome on Windows XP
browserName: 'chrome',
platform: 'Windows 2003'
},
{ // Latest Chrome on Windows 7
browserName: 'chrome',
platform: 'Windows 2008'
},
{ // Latest Chrome on Linux (unknown distro)
browserName: 'chrome',
platform: 'Linux'
},
{ // Latest Chrome on Linux (unknown distro)
browserName: 'chrome',
platform: 'OS X 10.8'
},
{ // Lowest known working version of FF
browserName: 'firefox',
version: '4.0'
},
{ // Highest known working version of FF on Windows
browserName: 'firefox',
version: '18.0'
},
{ // Highest FF on OSX
browserName: 'firefox',
platform: 'Mac 10.6',
version: '14.0'
},
{ // Lowest iPad on OSX (simulator)
browserName: 'ipad',
platform: 'Mac 10.6',
version: '4.3'
},
{ // Highest iPad on OSX (simulator)
// NOTE: iOS 6 is available, but it hangs on SauceLabs...
browserName: 'ipad',
platform: 'Mac 10.6',
version: '5'
},
{ // Lowest iPhone on OSX (simulator)
browserName: 'iphone',
platform: 'Mac 10.6',
version: '4.3'
},
{ // Highest iPhone on OSX (simulator)
// NOTE: iOS 6 is available, but it hangs on SauceLabs...
browserName: 'iphone',
platform: 'Mac 10.6',
version: '5'
},
{ // Android 4.0 (simulator)
browserName: 'android',
platform: 'Linux',
version: '4'
}
], // https://saucelabs.com/docs/browsers
onTestComplete: function(){
// Called after a qunit unit is done, per page, per browser
// Return true or false, passes or fails the test
// Returning undefined does not alter the test result

// For async return, call
var done = this.async();
setTimeout(function(){
// Return to this test after 1000 milliseconds
done(/*true or false changes the test result, undefined does not alter the result*/);
}, 1000);
}
}
}
}
});

grunt.loadNpmTasks('grunt-express');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-saucelabs');


grunt.registerTask('serve', ['express', 'express-keepalive']);
grunt.registerTask('saucelabs', ['test', 'saucelabs-qunit']);
grunt.registerTask('test', ['express', 'qunit']);
};
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
Copyright (c) Mobify R&D Inc.
http://www.mobify.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.
27 changes: 27 additions & 0 deletions README.md
@@ -0,0 +1,27 @@
# cssOptimize

This repo contains the client library for Mobify's CSS Concatenation
service, Jazzcat. The client API is MIT licensed, but use of it with the
backend service requires you to be a Mobify Cloud user (https://cloud.mobify.com)

To use it standalone in your project, please checkout www.mobifyjs.com, which is
a library that packages up Mobify's performance libraries in one package.

## Installation

First, you will need to install grunt-cli and bower globally:

npm install -g grunt-cli bower

Then install the local dependencies:

npm install
bower install

## Testing

grunt test

And if you have Saucelabs credentials:

grunt saucelabs
19 changes: 19 additions & 0 deletions bower.json
@@ -0,0 +1,19 @@
{
"name": "jazzcat-client",
"version": "1.0.0",
"main": "jazzcat.js",
"dependencies": {
"mobifyjs-utils": "git@github.com:mobify/mobifyjs-utils.git#1.0.0"
},
"devDependencies": {
"requirejs": "2.1.10",
"qunit": "1.14.0",
"jquery": "1.11.0",
"mobifyjs": "git@github.com:mobify/mobifyjs.git#v2.0"
},
"ignore": [
"test",
"Gruntfile.js",
"*.md"
]
}
14 changes: 14 additions & 0 deletions circle.yml
@@ -0,0 +1,14 @@
machine:
node:
version: 0.10.22

dependencies:
pre:
- npm install -g grunt-cli bower
override:
- npm install
- bower install

test:
override:
- grunt test
10 changes: 10 additions & 0 deletions package.json
@@ -0,0 +1,10 @@
{
"name": "cssOptimize",
"version": "1.0.0",
"devDependencies": {
"grunt": "~0.4.0",
"grunt-express": "~0.3.6",
"grunt-contrib-qunit": "~0.2.0",
"grunt-saucelabs": "4.0.4"
}
}
103 changes: 103 additions & 0 deletions src/cssOptimize.js
@@ -0,0 +1,103 @@
/**
* cssOptimize - Client code to a css optimization service
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['mobifyjs/utils'], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
var Utils = require('../bower_components/mobifyjs-utils/utils.js');
module.exports = factory(Utils);
} else {
// Browser globals (root is window)
root.CssOptimize = factory(root.Utils);
}
}(this, function (Utils) {

var CssOptimize = window.cssOptimize = {};

/**
* Takes an original, absolute url of a stylesheet, returns a url for that
* stylesheet going through the css service.
*/

CssOptimize.getCssUrl = function(url, options) {
var opts = Utils.extend({}, defaults, options);
var bits = [opts.protoAndHost];

if (opts.projectName) {
bits.push('project-' + opts.projectName);
}

bits.push(opts.endpoint);
bits.push(url);

return bits.join('/');
};

/**
* Rewrite the href of a stylesheet referencing `<link>` element to go through
* our service.
*/
CssOptimize._rewriteHref = function(element, options) {
var attributeVal = element.getAttribute(options.targetAttribute);
var url;
if (attributeVal) {
url = Utils.absolutify(attributeVal);
if (Utils.httpUrl(url)) {
element.setAttribute('data-orig-href', attributeVal);
element.setAttribute(options.targetAttribute,
CssOptimize.getCssUrl(url, options));
if (options.onerror) {
element.setAttribute('onerror', options.onerror);
}
}
}
};

/**
* Takes an array-like object of `<link>` elements
*/
CssOptimize.optimize = function(elements, options) {
var opts = Utils.extend({}, defaults, options);
var element;

for(var i = 0, len = elements.length; i < len; i++) {
element = elements[i];
if (element.nodeName === 'LINK' &&
element.getAttribute('rel') === 'stylesheet' &&
element.getAttribute(opts.targetAttribute) &&
!element.hasAttribute('mobify-optimized')) {
element.setAttribute('mobify-optimized', '');
CssOptimize._rewriteHref(element, opts);
}
}
};

/**
* An 'error' event handler designed to be set using an "onerror" attribute that
* will set the target elements "href" attribute to the value of its
* "data-orig-href" attribute, if one exists.
*/
var restoreOriginalHref = CssOptimize.restoreOriginalHref = function(event) {
var origHref;
event.target.removeAttribute('onerror'); //remove error handler
if(origHref = event.target.getAttribute('data-orig-href')) {
event.target.setAttribute('href', origHref);
}
};

var defaults = CssOptimize._defaults = {
protoAndHost: '//jazzcat.mobify.com',
endpoint: 'cssoptimizer',
projectName: 'oss-' + location.hostname.replace(/[^\w]/g, '-'),
targetAttribute: 'x-href',
onerror: 'Mobify.CssOptimize.restoreOriginalHref(event);'
};

return CssOptimize;

}));

0 comments on commit e350f93

Please sign in to comment.