inline-css
Advanced tools
Comparing version 1.0.0 to 1.0.1
36
index.js
'use strict'; | ||
var assert = require('assert'), | ||
extractCss = require('extract-css'), | ||
var extractCss = require('extract-css'), | ||
inlineCss = require('./lib/inline-css'); | ||
@@ -18,15 +17,3 @@ | ||
function getDefaultOptions(options) { | ||
return extend({ | ||
extraCss: '', | ||
applyStyleTags: true, | ||
removeStyleTags: true, | ||
applyLinkTags: true, | ||
removeLinkTags: true, | ||
preserveMediaQueries: false, | ||
applyWidthAttributes: false, | ||
}, options); | ||
} | ||
function inlineDocumentWithCb(html, css, options, callback) { | ||
function inlineCssWithCb(html, css, options, callback) { | ||
var content; | ||
@@ -42,5 +29,3 @@ | ||
function juiceDocument(src, options, callback) { | ||
assert.ok(options.url, 'options.url is required'); | ||
options = getDefaultOptions(options); | ||
function inlineContent(src, options, callback) { | ||
extractCss(src, options, function (err, html, css) { | ||
@@ -52,3 +37,3 @@ if (err) { | ||
css += '\n' + options.extraCss; | ||
inlineDocumentWithCb(html, css, options, callback); | ||
inlineCssWithCb(html, css, options, callback); | ||
}); | ||
@@ -58,6 +43,13 @@ } | ||
module.exports = function (html, options, callback) { | ||
assert.ok(options.url, 'options.url is required'); | ||
options = getDefaultOptions(options); | ||
var opt = extend({ | ||
extraCss: '', | ||
applyStyleTags: true, | ||
removeStyleTags: true, | ||
applyLinkTags: true, | ||
removeLinkTags: true, | ||
preserveMediaQueries: false, | ||
applyWidthAttributes: false, | ||
}, options); | ||
juiceDocument(html, options, function (err, content) { | ||
inlineContent(html, opt, function (err, content) { | ||
if (err) { | ||
@@ -64,0 +56,0 @@ callback(err); |
@@ -12,30 +12,3 @@ 'use strict'; | ||
function setStyleAttrs(el) { | ||
var style = []; | ||
for (var i in el.styleProps) { | ||
style.push(el.styleProps[i].prop + ': ' + el.styleProps[i].value.replace(/["]/g, '\'') + ';'); | ||
} | ||
// sorting will arrange styles like padding: before padding-bottom: which will preserve the expected styling | ||
style = style.sort(function (a, b) { | ||
var aProp = a.split(':')[0]; | ||
var bProp = b.split(':')[0]; | ||
return (aProp > bProp ? 1 : aProp < bProp ? -1 : 0); | ||
}); | ||
//el.setAttribute('style', style.join(' ')); | ||
} | ||
function setWidthAttrs(el) { | ||
if (widthElements.indexOf(el.nodeName) > -1) { | ||
for (var i in el.styleProps) { | ||
if (el.styleProps[i].prop === 'width' && el.styleProps[i].value.match(/px/)) { | ||
var pxWidth = el.styleProps[i].value.replace('px', ''); | ||
el.setAttribute('width', pxWidth); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
module.exports = function (html, css, options) { | ||
var rules = parseCSS(css), | ||
@@ -45,13 +18,6 @@ editedElements = [], | ||
rules.forEach(handleRule); | ||
editedElements.forEach(setStyleAttrs); | ||
if (options && options.applyWidthAttributes) { | ||
editedElements.forEach(setWidthAttrs); | ||
} | ||
function handleRule(rule) { | ||
var sel = rule[0], | ||
style = rule[1], | ||
selector = new Selector(sel); | ||
style = rule[1], | ||
selector = new Selector(sel); | ||
@@ -80,15 +46,2 @@ // skip rule if the selector has any pseudos which are ignored | ||
$els.each(function (index, el) { | ||
if (!el.styleProps) { | ||
el.styleProps = {}; | ||
// if the element has inline styles, fake selector with topmost specificity | ||
if ($(el).attr('style')) { | ||
var cssText = '* { ' + el.getAttribute('style') + ' } '; | ||
addProps(parseCSS(cssText)[0][1], styleSelector); | ||
} | ||
// store reference to an element we need to compile style="" attr for | ||
editedElements.push(el); | ||
} | ||
// go through the properties | ||
@@ -98,5 +51,5 @@ function addProps(style, selector) { | ||
var name = style[i], | ||
value = style[name], | ||
sel = style._importants[name] ? importantSelector : selector, | ||
prop = new Property(name, value, sel); | ||
value = style[name], | ||
sel = style._importants[name] ? importantSelector : selector, | ||
prop = new Property(name, value, sel); | ||
@@ -113,2 +66,15 @@ if ($(el).attr('style')) { | ||
if (!el.styleProps) { | ||
el.styleProps = {}; | ||
// if the element has inline styles, fake selector with topmost specificity | ||
if ($(el).attr('style')) { | ||
var cssText = '* { ' + el.getAttribute('style') + ' } '; | ||
addProps(parseCSS(cssText)[0][1], styleSelector); | ||
} | ||
// store reference to an element we need to compile style="" attr for | ||
editedElements.push(el); | ||
} | ||
addProps(style, selector); | ||
@@ -118,3 +84,36 @@ }); | ||
function setStyleAttrs(el) { | ||
var style = []; | ||
for (var i in el.styleProps) { | ||
style.push(el.styleProps[i].prop + ': ' + el.styleProps[i].value.replace(/["]/g, '\'') + ';'); | ||
} | ||
// sorting will arrange styles like padding: before padding-bottom: which will preserve the expected styling | ||
style = style.sort(function (a, b) { | ||
var aProp = a.split(':')[0]; | ||
var bProp = b.split(':')[0]; | ||
return (aProp > bProp ? 1 : aProp < bProp ? -1 : 0); | ||
}); | ||
//el.setAttribute('style', style.join(' ')); | ||
} | ||
function setWidthAttrs(el) { | ||
if (widthElements.indexOf(el.nodeName) > -1) { | ||
for (var i in el.styleProps) { | ||
if (el.styleProps[i].prop === 'width' && el.styleProps[i].value.match(/px/)) { | ||
var pxWidth = el.styleProps[i].value.replace('px', ''); | ||
el.setAttribute('width', pxWidth); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
rules.forEach(handleRule); | ||
editedElements.forEach(setStyleAttrs); | ||
if (options && options.applyWidthAttributes) { | ||
editedElements.forEach(setWidthAttrs); | ||
} | ||
return $.html(); | ||
}; |
{ | ||
"name": "inline-css", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Inline css into an html file.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -60,2 +60,12 @@ # inline-css | ||
## Usage | ||
```js | ||
var inlineCss = require('inline-css'); | ||
inlineCss('/path/to/file.html', function(err, html) { | ||
console.log(html); | ||
}); | ||
``` | ||
## API | ||
@@ -62,0 +72,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
131
9603
146