1
0
mirror of https://github.com/svg/svgo.git synced 2026-01-25 18:41:39 +03:00
Files
svgo/test/browser.js
Seth Falco f3495ff6c9 fix(removeEmptyContainers): skip if filter is applied via styles as well (#2089)
Fixes a bug where we were too eager to remove empty containers.

We already had logic to skip removing empty containers if it had the
filter attribute, which is needed to apply a filter to the whole area.
However, the filter can also be defined through CSS. We did not
properly handle this case, and treated the node as if it had no filter
at all.

This computes the styles and checks the stylesheet as well. (We also
move the logic down to avoid computing the styles eagerly.)
2024-12-22 16:01:22 +00:00

99 lines
2.4 KiB
JavaScript

import assert from 'assert';
import fs from 'node:fs/promises';
import http from 'http';
import path from 'path';
import { fileURLToPath } from 'url';
import { chromium } from 'playwright';
const PORT = 5001;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const pkgPath = path.join(__dirname, '../package.json');
const { version } = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
const fixture = `<svg xmlns="http://www.w3.org/2000/svg">
<g attr1="val1">
<g attr2="val2">
<path attr2="val3" d="..."/>
</g>
<path d="..."/>
</g>
</svg>`;
const expected = `<svg xmlns="http://www.w3.org/2000/svg">
<g attr1="val1">
<g attr2="val2">
<path attr2="val3" d="..."/>
</g>
<path d="..."/>
</g>
</svg>
`;
const content = `
<script type="module">
import { VERSION, optimize, builtinPlugins, _collections } from '/svgo.browser.js';
const result = optimize(${JSON.stringify(fixture)}, {
plugins : [],
js2svg : { pretty: true, indent: 2 }
});
globalThis.version = VERSION;
globalThis.builtinPlugins = builtinPlugins;
globalThis._collections = _collections;
globalThis.result = result.data;
</script>
`;
const server = http.createServer(async (req, res) => {
if (req.url === '/') {
res.setHeader('Content-Type', 'text/html');
res.end(content);
}
if (req.url === '/svgo.browser.js') {
res.setHeader('Content-Type', 'application/javascript');
res.end(await fs.readFile('./dist/svgo.browser.js'));
}
res.end();
});
const runTest = async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(`http://localhost:${PORT}`);
const actual = await page.evaluate(() => ({
version: globalThis.version,
builtinPlugins: globalThis.builtinPlugins,
_collections: globalThis._collections,
result: globalThis.result,
}));
assert.strictEqual(actual.version, version);
assert.notEqual(
actual.builtinPlugins,
undefined,
'builtinPlugins must be defined',
);
assert.notEqual(
actual._collections,
undefined,
'_collections must be defined',
);
assert.equal(actual.result, expected);
await browser.close();
};
server.listen(PORT, async () => {
try {
await runTest();
console.info('Tested successfully');
server.close();
} catch (error) {
server.close();
console.error(error.toString());
process.exit(1);
}
});