|
| 1 | + |
| 2 | +/** |
| 3 | + * juice |
| 4 | + * Copyright(c) 2011 LearnBoost <dev@learnboost.com> |
| 5 | + * MIT Licensed |
| 6 | + */ |
| 7 | + |
| 8 | +module.exports = exports = juice; |
| 9 | + |
| 10 | +/** |
| 11 | + * Module dependencies. |
| 12 | + */ |
| 13 | + |
| 14 | +var soupselect = require('soupselect') |
| 15 | + , utils = require('./utils') |
| 16 | + , Selector = require('./selector') |
| 17 | + , Property = require('./property') |
| 18 | + |
| 19 | +/** |
| 20 | + * Export Selector. |
| 21 | + */ |
| 22 | + |
| 23 | +exports.Selector = Selector; |
| 24 | + |
| 25 | +/** |
| 26 | + * Export Property. |
| 27 | + */ |
| 28 | + |
| 29 | +exports.Property = Property; |
| 30 | + |
| 31 | +/** |
| 32 | + * Export utils. |
| 33 | + */ |
| 34 | + |
| 35 | +exports.utils = require('./utils'); |
| 36 | + |
| 37 | +/** |
| 38 | + * Inlines the CSS specified by `css` into the `html` |
| 39 | + * |
| 40 | + * @param {String} html |
| 41 | + * @param {String} css |
| 42 | + * @api public |
| 43 | + */ |
| 44 | + |
| 45 | +function juice (html, css, options) { |
| 46 | + var rules = utils.parseCSS(css) |
| 47 | + , dom = utils.parseHTML(html) |
| 48 | + , editedElements = [] |
| 49 | + , topmost = new Selector('<style attribute>', [1, 0, 0, 0]) |
| 50 | + |
| 51 | + function select (sel) { |
| 52 | + return soupselect.select(dom, sel); |
| 53 | + } |
| 54 | + |
| 55 | + rules.forEach(function (rule) { |
| 56 | + var sel = rule[0] |
| 57 | + , style = rule[1] |
| 58 | + , matches = select(sel) |
| 59 | + , selector |
| 60 | + |
| 61 | + console.log('\ngot selector "%s" - matches: %s ', sel, matches.length); |
| 62 | + matches.forEach(function (el) { |
| 63 | + // we initialize the Selector lazily to avoid needless parsing |
| 64 | + if (!selector) { |
| 65 | + selector = new Selector(sel) |
| 66 | + console.log( |
| 67 | + 'selector "%s" has specificity "%s"' |
| 68 | + , sel |
| 69 | + , JSON.stringify(selector.specificity()) |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + if (!el.styleProps) { |
| 74 | + el.styleProps = {} |
| 75 | + |
| 76 | + // if the element has inline styles, fake selector with topmost specificity |
| 77 | + if (el.attribs && el.attribs.style) { |
| 78 | + console.log('element has inline style - caching properties'); |
| 79 | + addProps( |
| 80 | + utils.parseCSS(el.attribs.style) |
| 81 | + , topmost |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + // store reference to an element we need to compile style="" attr for |
| 86 | + editedElements.push(el); |
| 87 | + } |
| 88 | + |
| 89 | + // go through the properties |
| 90 | + function addProps (style, selector) { |
| 91 | + for (var i = 0, l = style.length; i < l; i++) { |
| 92 | + var name = style[i] |
| 93 | + , value = style[name] |
| 94 | + , sel = style._importants[name] |
| 95 | + ? new Selector('!important', [2,0,0,0]) |
| 96 | + : selector |
| 97 | + , prop = new Property(name, value, sel) |
| 98 | + , existing = el.styleProps[name] |
| 99 | + |
| 100 | + if (existing) { |
| 101 | + var winner = existing.compare(prop) |
| 102 | + , loser = prop == winner ? existing : prop |
| 103 | + |
| 104 | + console.log( |
| 105 | + ' - already existing from selector %s, selector %s beats %s' |
| 106 | + , name |
| 107 | + , existing.selector.text |
| 108 | + , winner.text |
| 109 | + , loser.text |
| 110 | + ); |
| 111 | + |
| 112 | + if (winner == prop) { |
| 113 | + console.log(' + new value "%s"', value); |
| 114 | + } |
| 115 | + } else { |
| 116 | + el.styleProps[name] = prop; |
| 117 | + console.log(' - property "%s" added with value "%s"', name, value); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + addProps(style, selector); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + console.log('elements affected "%s"', editedElements.length); |
| 127 | + editedElements.forEach(function (el) { |
| 128 | + var style = ''; |
| 129 | + |
| 130 | + for (var i in el.styleProps) { |
| 131 | + style += el.styleProps[i].toString(); |
| 132 | + } |
| 133 | + |
| 134 | + if (!el.attribs) el.attribs = {}; |
| 135 | + |
| 136 | + el.attribs.style = style; |
| 137 | + }); |
| 138 | + |
| 139 | + return utils.domToHTML(dom); |
| 140 | +} |
0 commit comments