Comparing version 1.2.0 to 1.3.0
@@ -0,1 +1,7 @@ | ||
1.3.0 / 2015-07-02 | ||
================== | ||
* Add option `preserveImportant` | ||
* Make lib `use_strict` compliant and lint files | ||
1.2.0 / 2015-05-21 | ||
@@ -2,0 +8,0 @@ ================== |
106
lib/juice.js
@@ -8,10 +8,27 @@ | ||
"use strict"; | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var utils = require('./utils'); | ||
var Selector = require('./selector'); | ||
var Property = require('./property'); | ||
var packageJson = require('../package'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var styleSelector = new Selector('<style attribute>', [1, 0, 0, 0]); | ||
var importantSelector = new Selector('<!important>', [2, 0, 0, 0]); | ||
var juice = function (html,options) { | ||
var $ = utils.cheerio(html, { xmlMode: options && options.xmlMode}); | ||
doc = juiceDocument($,options) | ||
var doc = juiceDocument($,options); | ||
if (options && options.xmlMode) | ||
if (options && options.xmlMode){ | ||
return doc.xml(); | ||
else | ||
} | ||
else { | ||
return doc.html(); | ||
} | ||
}; | ||
@@ -22,16 +39,2 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var utils = require('./utils') | ||
, Selector = require('./selector') | ||
, Property = require('./property') | ||
, packageJson = require('../package') | ||
, fs = require('fs') | ||
, path = require('path') | ||
, assert = require('assert') | ||
, styleSelector = new Selector('<style attribute>', [1, 0, 0, 0]) | ||
, importantSelector = new Selector('<!important>', [2, 0, 0, 0]); | ||
/** | ||
* Package version | ||
@@ -79,4 +82,4 @@ */ | ||
var rules = utils.parseCSS(css) | ||
, editedElements = []; | ||
var rules = utils.parseCSS(css); | ||
var editedElements = []; | ||
@@ -99,7 +102,7 @@ rules.forEach(handleRule); | ||
function handleRule(rule) { | ||
var sel = rule[0] | ||
, style = rule[1] | ||
, selector = new Selector(sel) | ||
, parsedSelector = selector.parsed() | ||
, pseudoElementType = getPseudoElementType(parsedSelector); | ||
var sel = rule[0]; | ||
var style = rule[1]; | ||
var selector = new Selector(sel); | ||
var parsedSelector = selector.parsed(); | ||
var pseudoElementType = getPseudoElementType(parsedSelector); | ||
@@ -112,3 +115,5 @@ // skip rule if the selector has any pseudos which are ignored | ||
var subSelPseudo = subSel.pseudos[j]; | ||
if (juice.ignoredPseudos.indexOf(subSelPseudo.name) >= 0) return; | ||
if (juice.ignoredPseudos.indexOf(subSelPseudo.name) >= 0) { | ||
return; | ||
} | ||
} | ||
@@ -121,3 +126,3 @@ } | ||
var pseudos = last.pseudos; | ||
last.pseudos = filterElementPseudos(last.pseudos), | ||
last.pseudos = filterElementPseudos(last.pseudos); | ||
sel = parsedSelector.toString(); | ||
@@ -151,7 +156,7 @@ last.pseudos = pseudos; | ||
if (!el.styleProps) { | ||
el.styleProps = {} | ||
el.styleProps = {}; | ||
// if the element has inline styles, fake selector with topmost specificity | ||
if ($(el).attr('style')) { | ||
var cssText = '* { ' + $(el).attr('style') + ' } ' | ||
var cssText = '* { ' + $(el).attr('style') + ' } '; | ||
addProps(utils.parseCSS(cssText)[0][1], styleSelector); | ||
@@ -167,14 +172,9 @@ } | ||
for (var i = 0, l = style.length; i < l; i++) { | ||
var name = style[i] | ||
, value = style[name] | ||
, sel = style._importants[name] ? importantSelector : selector | ||
, prop = new Property(name, value, sel) | ||
, existing = el.styleProps[name]; | ||
var name = style[i]; | ||
var value = style[name] + (options.preserveImportant && style._importants[name] ? ' !important' : ''); | ||
var sel = style._importants[name] ? importantSelector : selector; | ||
var prop = new Property(name, value, sel); | ||
var existing = el.styleProps[name]; | ||
if (existing) { | ||
var winner = existing.compare(prop) | ||
, loser = prop === winner ? existing : prop | ||
if (winner === prop) el.styleProps[name] = prop; | ||
} else { | ||
if (existing && existing.compare(prop) === prop || !existing) { | ||
el.styleProps[name] = prop; | ||
@@ -314,6 +314,8 @@ } | ||
if (options.xmlMode) | ||
callback(null, $.xml()); | ||
else | ||
callback(null, $.html()); | ||
if (options.xmlMode){ | ||
return callback(null, $.xml()); | ||
} | ||
else { | ||
return callback(null, $.html()); | ||
} | ||
}; | ||
@@ -344,3 +346,5 @@ | ||
fs.readFile(filePath, 'utf8', function(err, content) { | ||
if (err) return callback(err); | ||
if (err) { | ||
return callback(err); | ||
} | ||
options = getDefaultOptions(options); // so we can mutate options without guilt | ||
@@ -350,3 +354,3 @@ if(!options.webResources.relativeTo){ | ||
options.webResources.relativeTo = rel; | ||
}; | ||
} | ||
juiceResources(content, options, callback); | ||
@@ -360,6 +364,8 @@ }); | ||
if (options && options.xmlMode) | ||
if (options && options.xmlMode){ | ||
return $.xml(); | ||
else | ||
} | ||
else { | ||
return $.html(); | ||
} | ||
} | ||
@@ -370,3 +376,3 @@ | ||
var stylesList = $("style"); | ||
var i, styleDataList, styleData, styleElement; | ||
var styleDataList, styleData, styleElement; | ||
stylesList.each(function () { | ||
@@ -379,3 +385,5 @@ styleElement = this; | ||
styleData = styleDataList[0].data; | ||
if ( options.applyStyleTags ) results.push( styleData ); | ||
if ( options.applyStyleTags ) { | ||
results.push( styleData ); | ||
} | ||
if ( options.removeStyleTags ) | ||
@@ -382,0 +390,0 @@ { |
@@ -8,2 +8,4 @@ | ||
"use strict"; | ||
module.exports = exports = Property; | ||
@@ -15,3 +17,3 @@ | ||
var compare = require('./utils').compare | ||
var compare = require('./utils').compare; | ||
@@ -30,3 +32,3 @@ /** | ||
this.value = value; | ||
this.selector = selector | ||
this.selector = selector; | ||
} | ||
@@ -41,7 +43,9 @@ | ||
Property.prototype.compare = function (property) { | ||
var a = this.selector.specificity() | ||
, b = property.selector.specificity() | ||
, winner = compare(a, b) | ||
var a = this.selector.specificity(); | ||
var b = property.selector.specificity(); | ||
var winner = compare(a, b); | ||
if (winner === a && a !== b) return this; | ||
if (winner === a && a !== b) { | ||
return this; | ||
} | ||
return property; | ||
@@ -48,0 +52,0 @@ }; |
"use strict"; | ||
/** | ||
@@ -49,9 +51,9 @@ * Module dependencies. | ||
function specificity (text, parsed) { | ||
var expressions = parsed || parse(text) | ||
, spec = [0, 0, 0, 0] | ||
, nots = [] | ||
var expressions = parsed || parse(text); | ||
var spec = [0, 0, 0, 0]; | ||
var nots = []; | ||
for (var i = 0; i < expressions.length; i++) { | ||
var expression = expressions[i] | ||
, pseudos = expression.pseudos | ||
var expression = expressions[i]; | ||
var pseudos = expression.pseudos; | ||
@@ -88,3 +90,3 @@ // id awards a point in the second column | ||
} | ||
} | ||
}; | ||
@@ -91,0 +93,0 @@ /** |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
@@ -6,8 +7,7 @@ /** | ||
var fs = require('fs') | ||
, cssom = require('cssom') | ||
, cheerio = require('cheerio') | ||
, own = {}.hasOwnProperty | ||
, os = require('os') | ||
, inline = require('web-resource-inliner'); | ||
var cssom = require('cssom'); | ||
var cheerio = require('cheerio'); | ||
var own = {}.hasOwnProperty; | ||
var os = require('os'); | ||
var inline = require('web-resource-inliner'); | ||
@@ -23,5 +23,5 @@ /** | ||
exports.extract = function extract (selectorText) { | ||
var attr = 0 | ||
, sels = [] | ||
, sel = '' | ||
var attr = 0; | ||
var sels = []; | ||
var sel = ''; | ||
@@ -45,6 +45,8 @@ for (var i = 0, l = selectorText.length; i < l; i++) { | ||
if (sel.length) sels.push(sel); | ||
if (sel.length) { | ||
sels.push(sel); | ||
} | ||
return sels; | ||
} | ||
}; | ||
@@ -61,9 +63,9 @@ /** | ||
exports.parseCSS = function (css) { | ||
var rules = cssom.parse(css).cssRules || [] | ||
, ret = []; | ||
var rules = cssom.parse(css).cssRules || []; | ||
var ret = []; | ||
for (var i = 0, l = rules.length; i < l; i++) { | ||
if (rules[i].selectorText) { // media queries don't have selectorText | ||
var rule = rules[i] | ||
, selectors = exports.extract(rule.selectorText) | ||
var rule = rules[i]; | ||
var selectors = exports.extract(rule.selectorText); | ||
@@ -77,3 +79,3 @@ for (var ii = 0, ll = selectors.length; ii < ll; ii++) { | ||
return ret; | ||
} | ||
}; | ||
@@ -90,3 +92,3 @@ | ||
{ | ||
var rules = cssom.parse( css ).cssRules || [] | ||
var rules = cssom.parse( css ).cssRules || []; | ||
var queries = []; | ||
@@ -137,3 +139,3 @@ | ||
return queries.join( os.EOL ); | ||
} | ||
}; | ||
@@ -169,4 +171,5 @@ /** | ||
for (var i = 0, l = arr.length; i < l; i++) | ||
for (var i = 0, l = arr.length; i < l; i++){ | ||
ret.push(arr[i]); | ||
} | ||
@@ -193,7 +196,11 @@ return ret; | ||
return b; | ||
} | ||
}; | ||
exports.extend = function (obj, src) { | ||
for (var key in src) if (own.call(src, key)) obj[key] = src[key]; | ||
for (var key in src) { | ||
if (own.call(src, key)){ | ||
obj[key] = src[key]; | ||
} | ||
} | ||
return obj; | ||
} | ||
}; |
{ | ||
"name": "juice", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Inlines css into html source", | ||
@@ -5,0 +5,0 @@ "bin": "./bin/juice", |
@@ -56,2 +56,3 @@ [![Build Status](https://travis-ci.org/Automattic/juice.png?branch=master)](https://travis-ci.org/Automattic/juice) | ||
* `xmlMode` - whether to output XML/XHTML with all tags closed. Note that the input *must* also be valid XML/XHTML or you will get undesirable results. Defaults to `false`. | ||
* `preserveImportant` - preserves `!important` in values. Defaults to `false`. | ||
@@ -58,0 +59,0 @@ ### Methods |
Sorry, the diff of this file is not supported yet
239247
692
161
1