Skip to content

Commit

Permalink
Removed call to encodeSVGDatauri() from coa.js and added test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkenny54 committed Aug 5, 2024
1 parent 9078e8c commit 2cc2eb9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
5 changes: 1 addition & 4 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import path from 'path';
import colors from 'picocolors';
import { fileURLToPath } from 'url';
import { encodeSVGDatauri, decodeSVGDatauri } from './tools.js';
import { decodeSVGDatauri } from './tools.js';
import { loadConfig, optimize } from '../svgo-node.js';
import { builtin } from '../builtin.js';

Expand Down Expand Up @@ -408,9 +408,6 @@ function processSVGData(config, info, data, output, input) {
throw error;
}
}
if (config.datauri) {
result.data = encodeSVGDatauri(result.data, config.datauri);
}
var resultFileSize = Buffer.byteLength(result.data, 'utf8'),
processingTime = Date.now() - startTime;

Expand Down
26 changes: 25 additions & 1 deletion test/coa/_index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ const svgFiles = [
const tempFolder = 'temp';
const noop = () => {};

/**
* @param {string[]} args
*/
function runProgram(args) {
const program = new Command();
svgo(program);
// prevent running process.exit
program.exitOverride(() => {});
// parser skips first two arguments
return program.parseAsync([0, 1, ...args]);
return program.parseAsync(['0', '1', ...args]);
}

describe('coa', function () {
Expand All @@ -46,6 +49,10 @@ describe('coa', function () {
global.process.exit = initialProcessExit;
}

/**
* @param {string} folderPath
* @returns {number}
*/
function calcFolderSvgWeight(folderPath) {
return fs
.readdirSync(folderPath)
Expand All @@ -62,6 +69,23 @@ describe('coa', function () {
);
}

it('should generate correct datauri', async () => {
const outfilePath = tempFolder + '/test.svg';
await runProgram([
'-i',
path.resolve(__dirname, 'testSvgDatauri') + '/test.svg',
'-o',
outfilePath,
'--datauri',
'enc',
'--quiet',
]);
const outData = fs.readFileSync(outfilePath, 'utf8');
expect(outData).toBe(
'data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Ctext%20x%3D%225%22%20y%3D%2215%22%3ETEST%3C%2Ftext%3E%3C%2Fsvg%3E',
);
});

it('should optimize folder', async () => {
const initWeight = calcFolderSvgWeight(svgFolderPath);
await runProgram([
Expand Down
3 changes: 3 additions & 0 deletions test/coa/testSvgDatauri/test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2cc2eb9

Please sign in to comment.