diff --git a/lib/css.js b/lib/css.js index 28d606f..aa6fe00 100644 --- a/lib/css.js +++ b/lib/css.js @@ -8,6 +8,9 @@ module.exports = { var Promise = require('es6-promise').Promise; // jshint ignore:line var debug = require('debug')('inliner'); var basename = require('path').basename; +var CHARTSET_RE = /@charset\s+["']([\w\-_]+)["'];/i; +var iconv = require('iconv-lite'); + function getImages(root, css) { var inliner = this; @@ -46,6 +49,7 @@ function replace(body, source, target) { } function getImports(root, css) { + css = convertCharset(css); // change to a string in case the CSS is a buffer, which is the case // when we're reading off the local file system if (typeof css !== 'string') { @@ -66,7 +70,7 @@ function getImports(root, css) { // if url has a length > 1, then we have media types to target var resolvedURL = inliner.resolve(root, url[0]); return inliner.get(resolvedURL).then(function then(res) { - var importedCSS = res.body; + var importedCSS = convertCharset(res.body); inliner.jobs.done.links(); inliner.emit('progress', 'import ' + basename(resolvedURL)); if (url.length > 1) { @@ -98,3 +102,26 @@ function compress(css) { .replace(/; /g, ';') .replace(/\n+/g, ''); } + +function convertCharset(cssBody) { + var end = 0; + var maxLength = 64; + if (cssBody) { + end = cssBody.length > maxLength ? maxLength : cssBody.length; + var charsetData = cssBody.slice(0, end).toString(); + var matchs = CHARTSET_RE.exec(charsetData); + if (matchs) { + var charset = matchs[1].toLowerCase(); + if (charset && charset !== 'utf-8' && charset !== 'utf8') { + debug('decoding from: %s', charset); + if (iconv.encodingExists(charset)) { + return iconv.encode(iconv.decode(cssBody, charset), 'utf-8') + .toString() + .replace(CHARTSET_RE, ''); + } + console.warn('no such charset: ' + charset); + } + } + } + return cssBody; +} diff --git a/test/fixtures/css-ext-charset-import.css b/test/fixtures/css-ext-charset-import.css new file mode 100644 index 0000000..5cf0acb --- /dev/null +++ b/test/fixtures/css-ext-charset-import.css @@ -0,0 +1 @@ +@charset "shift_jis";.body2 {width:100%;font-family: "ƒƒCƒŠƒI", "ƒqƒ‰ƒMƒmŠpƒS Pro W3", "MS PGothic", "MS UI Gothic", Helvetica, Arial, sans-serif;} diff --git a/test/fixtures/css-ext-charset.css b/test/fixtures/css-ext-charset.css new file mode 100644 index 0000000..8f43dab --- /dev/null +++ b/test/fixtures/css-ext-charset.css @@ -0,0 +1,6 @@ +@charset "shift_jis"; +@import url('css-ext-charset-import.css'); + +body { + font-family: "ƒƒCƒŠƒI", "ƒqƒ‰ƒMƒmŠpƒS Pro W3", "MS PGothic", "MS UI Gothic", Helvetica, Arial, sans-serif; +} diff --git a/test/fixtures/css-ext-charset.result.html b/test/fixtures/css-ext-charset.result.html new file mode 100644 index 0000000..f9ccb61 --- /dev/null +++ b/test/fixtures/css-ext-charset.result.html @@ -0,0 +1 @@ + External css and multibyte characters - ๆ—ฅๆœฌ่ชž \ No newline at end of file diff --git a/test/fixtures/css-ext-charset.src.html b/test/fixtures/css-ext-charset.src.html new file mode 100644 index 0000000..4a22fe3 --- /dev/null +++ b/test/fixtures/css-ext-charset.src.html @@ -0,0 +1,11 @@ + + + + + External css and multibyte characters - “๚–{Œ๊ + + + + + + \ No newline at end of file