Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed call to encodeSVGDatauri() from coa.js and added test case. #2053

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.