| 1 |
/* |
| 2 |
* @license MIT http://www.opensource.org/licenses/mit-license.php |
| 3 |
* @author Hovhannes Babayan <bhovhannes at gmail dot com> |
| 4 |
*/ |
| 5 |
var loaderUtils = require('loader-utils'); |
| 6 |
|
| 7 |
var REGEX_STYLE = /<style[\s\S]*?>[\s\S]*?<\/style>/i |
| 8 |
var REGEX_DECLARATION = /^\s*<\?xml [^>]*>\s*/i |
| 9 |
|
| 10 |
var REGEX_DOUBLE_QUOTE = /"/g |
| 11 |
var REGEX_MULTIPLE_SPACES = /\s+/g |
| 12 |
var REGEX_UNSAFE_CHARS = /[{}\|\\\^~\[\]`"<>#%]/g |
| 13 |
|
| 14 |
module.exports = function(content) { |
| 15 |
this.cacheable && this.cacheable(); |
| 16 |
|
| 17 |
var query = loaderUtils.getOptions(this) || {}; |
| 18 |
query.encoding = query.encoding || "none"; |
| 19 |
|
| 20 |
var limit = query.limit ? parseInt(query.limit, 10) : 0; |
| 21 |
|
| 22 |
if (limit <= 0 || content.length < limit) { |
| 23 |
var newContent = content.toString('utf8'); |
| 24 |
|
| 25 |
var hasStyleElement = REGEX_STYLE.test(newContent) |
| 26 |
|
| 27 |
if (query.stripdeclarations) { |
| 28 |
newContent = newContent.replace(REGEX_DECLARATION, ""); |
| 29 |
} |
| 30 |
|
| 31 |
var data; |
| 32 |
if (query.encoding === "base64") { |
| 33 |
if (typeof newContent === "string") { |
| 34 |
newContent = new Buffer(newContent); |
| 35 |
} |
| 36 |
data = "data:image/svg+xml;base64," + newContent.toString("base64"); |
| 37 |
} else { |
| 38 |
newContent = newContent.replace(REGEX_DOUBLE_QUOTE, "'"); |
| 39 |
newContent = newContent.replace(REGEX_MULTIPLE_SPACES, " "); |
| 40 |
newContent = newContent.replace(REGEX_UNSAFE_CHARS, function(match) { |
| 41 |
return '%'+match[0].charCodeAt(0).toString(16).toUpperCase(); |
| 42 |
}); |
| 43 |
|
| 44 |
data = 'data:image/svg+xml,' + newContent.trim(); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
if (!(query.iesafe && hasStyleElement && data.length > 4096)) { |
| 49 |
// if (query.encoding === "none" && !query.noquotes) { |
| 50 |
// data = '"'+data+'"'; |
| 51 |
// } |
| 52 |
|
| 53 |
return 'module.exports = ' + JSON.stringify(data); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
var fileLoader = require('file-loader'); |
| 58 |
return fileLoader.call(this, content); |
| 59 |
}; |
| 60 |
|
| 61 |
module.exports.raw = true; |