|
| 1 | +async = require("async") |
| 2 | +request = require("request") |
| 3 | +css = require("css") |
| 4 | +CleanCSS = require("clean-css") |
| 5 | +_ = require("lodash") |
| 6 | +url = require("url") |
| 7 | + |
| 8 | +getElementAttributes = (el, ignore...) -> |
| 9 | + attrs = {} |
| 10 | + |
| 11 | + for attr of el.attributes when typeof el.attributes[attr] is "object" and typeof el.attributes[attr].data is "string" |
| 12 | + continue if ignore.indexOf(attr) isnt -1 |
| 13 | + attrs[attr] = el.attributes[attr].data |
| 14 | + |
| 15 | + return attrs |
| 16 | + |
| 17 | +module.exports = exports = |
| 18 | + extractStylesheets: (window, document, options, next) -> |
| 19 | + options.cout "Extracting stylesheets from page" |
| 20 | + |
| 21 | + media = _.union(["", "all", "screen"], options.cssMedia) |
| 22 | + links = document.querySelectorAll("link[rel='stylesheet']") |
| 23 | + sheets = [] |
| 24 | + |
| 25 | + for link in links |
| 26 | + attrs = getElementAttributes(link, "media", "rel") |
| 27 | + continue unless typeof attrs.href is "string" |
| 28 | + |
| 29 | + # Domino bug/incorrect usage? |
| 30 | + realHref = attrs.href |
| 31 | + |
| 32 | + delete attrs.href |
| 33 | + |
| 34 | + sheets.push |
| 35 | + href: url.resolve(options.url, realHref) |
| 36 | + media: link.media |
| 37 | + attributes: attrs |
| 38 | + |
| 39 | + urlParsed = url.parse(options.url) |
| 40 | + |
| 41 | + sheets = sheets.filter (sheet) -> |
| 42 | + _.every options.ignoreSheets, (ignore) -> |
| 43 | + if ignore instanceof RegExp and ignore.test(sheet.href) |
| 44 | + options.cout "|> Ignoring stylesheet (#{sheet.href}) because of ignore-rule #{ignore.toString()}" |
| 45 | + return no |
| 46 | + |
| 47 | + return sheet.href isnt ignore |
| 48 | + |
| 49 | + if options.ignoreExternalSheets and urlParsed.hostname isnt url.parse(sheet.href).hostname |
| 50 | + options.cout "|> Ignoring stylesheet (#{sheet.href}) because external stylesheets are ignored" |
| 51 | + return no |
| 52 | + |
| 53 | + return yes |
| 54 | + |
| 55 | + sheets = sheets.filter((sheet) -> return media.indexOf(sheet.media) isnt -1) |
| 56 | + |
| 57 | + next(null, sheets) |
| 58 | + |
| 59 | + downloadStylesheets: (window, document, options, sheets, next) -> |
| 60 | + if not sheets or sheets.length is 0 |
| 61 | + options.cout "Refusing to continue: 0 stylesheets" |
| 62 | + return next("Refusing to continue: 0 stylesheets") |
| 63 | + |
| 64 | + options.cout "Downloading #{sheets.length} stylesheet(s)" |
| 65 | + |
| 66 | + mapFn = (sheet, done) -> |
| 67 | + requestOptions = |
| 68 | + headers: |
| 69 | + "User-Agent": options.userAgent |
| 70 | + |
| 71 | + request sheet.href, requestOptions, (error, response, body) -> |
| 72 | + if error |
| 73 | + options.cout "Failed to download stylesheet #{sheet.href}: #{error.toString()}" |
| 74 | + else if response.statusCode < 200 or response.statusCode > 399 |
| 75 | + options.cout "Failed to download stylesheet #{sheet.href}: Status code not in valid range #{response.statusCode.toString()}" |
| 76 | + else |
| 77 | + options.cout "|> Stylesheet downloaded: #{sheet.href}" |
| 78 | + |
| 79 | + body = "" if not error and (response.statusCode < 200 or response.statusCode > 399) |
| 80 | + |
| 81 | + # Rebase url()'s |
| 82 | + body = body.replace /url\((["']|)([^"'\(\)]+)\1\)/g, (m, quote, uri) -> |
| 83 | + return "url(#{quote}#{url.resolve(sheet.href, uri)}#{quote})" |
| 84 | + |
| 85 | + sheet.body = body |
| 86 | + |
| 87 | + done(error, sheet) |
| 88 | + |
| 89 | + async.map(sheets, mapFn, next) |
| 90 | + |
| 91 | + parseStylesheets: (window, document, options, sheets, next) -> |
| 92 | + if sheets |
| 93 | + cssString = "" |
| 94 | + |
| 95 | + for sheet in sheets |
| 96 | + cssString += sheet.body |
| 97 | + |
| 98 | + options.cout "Parsing #{sheets.length} stylesheet(s) - #{cssString.split("\n").length} lines" |
| 99 | + |
| 100 | + try |
| 101 | + styles = css.parse(cssString).stylesheet |
| 102 | + catch e |
| 103 | + if e.line |
| 104 | + line = cssString.split("\n")[e.line - 1] |
| 105 | + line = line.substring(e.column - 40, e.column) if line.length > 40 and e.column |
| 106 | + e.message = "node-css-inliner: #{e.message}\n\t -> #{line.substring(0, 40)}" |
| 107 | + |
| 108 | + return next(e) |
| 109 | + |
| 110 | + next(null, sheets, styles) |
| 111 | + else |
| 112 | + next("Failed to download stylesheets") |
| 113 | + |
| 114 | + filter: (window, document, options, sheets, styles, next) -> |
| 115 | + options.cout "Starting to filter out rules ..." |
| 116 | + |
| 117 | + filterChain = require("./filter") |
| 118 | + |
| 119 | + for action of filterChain |
| 120 | + filterChain[action] = filterChain[action].bind(null, document, options, styles) |
| 121 | + |
| 122 | + async.waterfall _.toArray(filterChain), (error, usedStyles) -> |
| 123 | + next(error, sheets, styles, usedStyles) |
| 124 | + |
| 125 | + generateStyles: (window, document, options, sheets, styles, usedStyles, next) -> |
| 126 | + try |
| 127 | + finalCSS = css.stringify(stylesheet: rules: usedStyles) |
| 128 | + |
| 129 | + if options.cssMinify isnt no |
| 130 | + finalCSS = new CleanCSS(if typeof options.cssMinify is "object" then options.cssMinify else {}).minify(finalCSS) |
| 131 | + |
| 132 | + sheets = sheets.filter (sheet) -> |
| 133 | + delete sheet["body"] |
| 134 | + return yes |
| 135 | + |
| 136 | + next(null, sheets, finalCSS) |
| 137 | + catch e |
| 138 | + next(error) |
| 139 | + |
| 140 | + |
0 commit comments