Code coverage report for modules/css.js

Statements: 100% (43 / 43)      Branches: 100% (0 / 0)      Functions: 100% (13 / 13)      Lines: 100% (43 / 43)      Ignored: none     

All files » modules/ » css.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108            1 1 1 1   1   8   8     8 8 8 8 8 8   8   5 5 5 5   5 5           8 8                     1   4   4 4   4   3 3 3 3       4   4   3   3     3 3           4 4 4 4                               1   4      
/**
 * Created by armin on 11/1/14.
 */
 
'use strict';
 
var url = require('url');
var BPromise = require('bluebird');
var cheerio = require('cheerio');
var helpers = require('./helpers.js');
 
function inlineCSS(cssUrl) {
 
    return new BPromise(function(resolve) {
 
        helpers.getHTTP(cssUrl)
            .then(function(response) {
 
                console.log('INLINE BODY: %s', response.body.length);
                var re = /@import\surl\([\"|\']([\w|\/|\.]+)[\"|\']\)/g;
                console.log('CHECKING');
                var matches = helpers.getMatches(response.body.toString(), re);
                console.log('Matches: ' + matches);
                var embedCssString = '';
 
                BPromise.map(matches, function(match) {
 
                    console.log('URL: %s, match: %s', cssUrl, match);
                    var embedCssUrl = url.resolve(cssUrl, match);
                    console.log(embedCssUrl);
                    return inlineCSS(embedCssUrl)
                        .then(function(embedBody) {
                            console.log('Embed body: %s', embedBody);
                            embedCssString += embedBody;
                        });
 
                })
                    .then(function() {
 
                        var combinedBody = response.body.toString() + embedCssString;
                        resolve(combinedBody);
 
                    });
 
            });
 
    });
 
}
 
// TODO: remove @embed url tags after inlining
function inlineAllCSS(baseUrl, html) {
 
    return new BPromise(function(resolve) {
 
        var $ = cheerio.load(html);
        var linkArray = [];
 
        $('link').each(function() {
 
            console.log('Found %s', $(this));
            var link = url.resolve(baseUrl, $(this).attr('href'));
            console.log('Link is %s', link);
            linkArray.push(link);
 
        });
 
        var cssString = '';
 
        BPromise.map(linkArray, function(link) {
 
            console.log('LINK: %s', link);
 
            return inlineCSS(link)
                .then(function(cssData) {
 
                    console.log('cssData: %s', cssData);
                    return cssString += cssData;
 
                });
        })
            .then(function() {
 
                console.log('CSSString:');
                console.log(cssString);
                $('head').append('<style>\n' + cssString + '</style>\n');
                resolve($.html());
 
            });
 
    });
}
 
/*
exports.inlineCSS = function(cssUrl) {
 
    return inlineCSS(cssUrl);
 
};
*/
 
// TODO: remove @embed url tags after inlining
exports.inlineAllCSS = function(baseUrl, html) {
 
    return inlineAllCSS(baseUrl, html);
 
};