Skip to content

Commit

Permalink
test: add Jest configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
bhovhannes committed Jun 16, 2019
1 parent 758b647 commit 3bab05d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 63 deletions.
62 changes: 1 addition & 61 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,61 +1 @@
/*
* @license MIT http://www.opensource.org/licenses/mit-license.php
* @author Hovhannes Babayan <bhovhannes at gmail dot com>
*/
var loaderUtils = require('loader-utils');

var REGEX_STYLE = /<style[\s\S]*?>[\s\S]*?<\/style>/i
var REGEX_DECLARATION = /^\s*<\?xml [^>]*>\s*/i

var REGEX_DOUBLE_QUOTE = /"/g
var REGEX_MULTIPLE_SPACES = /\s+/g
var REGEX_UNSAFE_CHARS = /[{}\|\\\^~\[\]`"<>#%]/g

module.exports = function(content) {
this.cacheable && this.cacheable();

var query = loaderUtils.getOptions(this) || {};
query.encoding = query.encoding || "none";

var limit = query.limit ? parseInt(query.limit, 10) : 0;

if (limit <= 0 || content.length < limit) {
var newContent = content.toString('utf8');

var hasStyleElement = REGEX_STYLE.test(newContent)

if (query.stripdeclarations) {
newContent = newContent.replace(REGEX_DECLARATION, "");
}

var data;
if (query.encoding === "base64") {
if (typeof newContent === "string") {
newContent = new Buffer(newContent);
}
data = "data:image/svg+xml;base64," + newContent.toString("base64");
} else {
newContent = newContent.replace(REGEX_DOUBLE_QUOTE, "'");
newContent = newContent.replace(REGEX_MULTIPLE_SPACES, " ");
newContent = newContent.replace(REGEX_UNSAFE_CHARS, function(match) {
return '%'+match[0].charCodeAt(0).toString(16).toUpperCase();
});

data = 'data:image/svg+xml,' + newContent.trim();

}

if (!(query.iesafe && hasStyleElement && data.length > 4096)) {
if (query.encoding === "none" && !query.noquotes) {
data = '"'+data+'"';
}

return 'module.exports = ' + JSON.stringify(data);
}
}

var fileLoader = require('file-loader');
return fileLoader.call(this, content);
};

module.exports.raw = true;
module.exports = require('./src/loader')
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
coverageDirectory: './coverage',
coverageReporters: ['lcov', 'html', 'text-summary'],
collectCoverageFrom: ['./src/**/*.js']
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "svg-url-loader",
"version": "2.3.2",
"description": "Converts SVG file to utf-8 encoded data-uri string",
"main": "src/index.js",
"main": "./index.js",
"scripts": {
"test": "jest --coverage",
"test-watch": "jest --watch",
Expand Down
61 changes: 61 additions & 0 deletions src/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* @license MIT http://www.opensource.org/licenses/mit-license.php
* @author Hovhannes Babayan <bhovhannes at gmail dot com>
*/
var loaderUtils = require('loader-utils');

var REGEX_STYLE = /<style[\s\S]*?>[\s\S]*?<\/style>/i
var REGEX_DECLARATION = /^\s*<\?xml [^>]*>\s*/i

var REGEX_DOUBLE_QUOTE = /"/g
var REGEX_MULTIPLE_SPACES = /\s+/g
var REGEX_UNSAFE_CHARS = /[{}\|\\\^~\[\]`"<>#%]/g

module.exports = function(content) {
this.cacheable && this.cacheable();

var query = loaderUtils.getOptions(this) || {};
query.encoding = query.encoding || "none";

var limit = query.limit ? parseInt(query.limit, 10) : 0;

if (limit <= 0 || content.length < limit) {
var newContent = content.toString('utf8');

var hasStyleElement = REGEX_STYLE.test(newContent)

if (query.stripdeclarations) {
newContent = newContent.replace(REGEX_DECLARATION, "");
}

var data;
if (query.encoding === "base64") {
if (typeof newContent === "string") {
newContent = new Buffer(newContent);
}
data = "data:image/svg+xml;base64," + newContent.toString("base64");
} else {
newContent = newContent.replace(REGEX_DOUBLE_QUOTE, "'");
newContent = newContent.replace(REGEX_MULTIPLE_SPACES, " ");
newContent = newContent.replace(REGEX_UNSAFE_CHARS, function(match) {
return '%'+match[0].charCodeAt(0).toString(16).toUpperCase();
});

data = 'data:image/svg+xml,' + newContent.trim();

}

if (!(query.iesafe && hasStyleElement && data.length > 4096)) {
if (query.encoding === "none" && !query.noquotes) {
data = '"'+data+'"';
}

return 'module.exports = ' + JSON.stringify(data);
}
}

var fileLoader = require('file-loader');
return fileLoader.call(this, content);
};

module.exports.raw = true;
4 changes: 3 additions & 1 deletion test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ const webpack = require('webpack')

jest.setTimeout(10000)

const svgUrlLoader = path.resolve(__dirname, '../src/loader.js')

describe('svg-url-loader', function() {

const outputDir = path.resolve(__dirname, './output'),
bundleFileName = 'bundle.js',
getBundleFile = function() {
return path.join(outputDir, bundleFileName)
}
const svgUrlLoader = path.resolve(__dirname, '../')

const globalConfig = {
context: path.resolve(__dirname, '../'),
mode: 'development',
Expand Down

0 comments on commit 3bab05d

Please sign in to comment.