Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into apply-transform-shape
Browse files Browse the repository at this point in the history
  • Loading branch information
KTibow committed Jan 6, 2024
2 parents 99cc6ad + 9e01769 commit 40c1275
Show file tree
Hide file tree
Showing 434 changed files with 1,756 additions and 1,515 deletions.
9 changes: 2 additions & 7 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
{
"parserOptions": {
"ecmaVersion": 2021
"ecmaVersion": 2021,
"sourceType": "module"
},
"env": {
"node": true,
"es2021": true
},
"extends": ["eslint:recommended"],
"overrides": [
{
"files": ["rollup.config.js", "**/*.mjs"],
"parserOptions": {
"sourceType": "module"
}
},
{
"files": ["**/*.cjs"],
"parserOptions": {
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ jobs:
- 20
- 18
- 16
- 14
os:
- ubuntu-latest
- windows-latest
Expand All @@ -61,4 +60,4 @@ jobs:
- run: yarn install
- run: yarn playwright install --with-deps chromium
- run: yarn test
- run: yarn test-browser
- run: yarn test-bundles
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ test/regression-diffs
test/cli/output
coverage
.DS_Store
.vscode
*.log
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ SVGO reads the configuration from `svgo.config.js` or the `--config path/to/conf
**`svgo.config.js`**

```js
module.exports = {
export default {
multipass: false, // boolean
datauri: 'base64', // 'base64'|'enc'|'unenc'
js2svg: {
Expand Down Expand Up @@ -83,7 +83,7 @@ Instead of configuring SVGO from scratch, you can tweak the default preset to su
**`svgo.config.js`**

```js
module.exports = {
export default {
plugins: [
{
name: 'preset-default',
Expand Down Expand Up @@ -112,9 +112,9 @@ You can also specify custom plugins:
**`svgo.config.js`**

```js
const importedPlugin = require('./imported-plugin');
import importedPlugin from './imported-plugin';

module.exports = {
export default {
plugins: [
// plugin imported from another JavaScript file
importedPlugin,
Expand All @@ -140,7 +140,7 @@ SVGO provides a few low level utilities.
The core of SVGO is `optimize` function.

```js
const { optimize } = require('svgo');
import { optimize } from 'svgo';

const result = optimize(svgString, {
path: 'path-to.svg', // recommended
Expand All @@ -155,7 +155,7 @@ const optimizedSvgString = result.data;
If you write a tool on top of SVGO you may want to resolve the `svgo.config.js` file.

```js
const { loadConfig } = require('svgo');
import { loadConfig } from 'svgo';

const config = await loadConfig();
```
Expand Down
6 changes: 3 additions & 3 deletions bin/svgo → bin/svgo.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

const colors = require('picocolors');
const { program } = require('commander');
const makeProgram = require('../lib/svgo/coa');
import colors from 'picocolors';
import { program } from 'commander';
import makeProgram from '../lib/svgo/coa.js';
makeProgram(program);
program.parseAsync(process.argv).catch((error) => {
console.error(colors.red(error.stack));
Expand Down
3 changes: 3 additions & 0 deletions docs/03-plugins/convert-path-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ svgo:
straightCurves:
description: If to convert curve commands that are effectively straight lines to line commands.
default: true
convertToQ:
description: If to convert cubic beziers to quadratic beziers when they effectively are.
default: true
lineShorthands:
description: If to convert regular lines to an explicit horizontal or vertical line where possible.
default: true
Expand Down
165 changes: 109 additions & 56 deletions lib/builtin.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,111 @@
'use strict';
import presetDefault from '../plugins/preset-default.js';
import * as addAttributesToSVGElement from '../plugins/addAttributesToSVGElement.js';
import * as addClassesToSVGElement from '../plugins/addClassesToSVGElement.js';
import * as applyTransformsShapes from '../plugins/applyTransformsShapes.js';
import * as cleanupAttrs from '../plugins/cleanupAttrs.js';
import * as cleanupEnableBackground from '../plugins/cleanupEnableBackground.js';
import * as cleanupIds from '../plugins/cleanupIds.js';
import * as cleanupListOfValues from '../plugins/cleanupListOfValues.js';
import * as cleanupNumericValues from '../plugins/cleanupNumericValues.js';
import * as collapseGroups from '../plugins/collapseGroups.js';
import * as convertColors from '../plugins/convertColors.js';
import * as convertEllipseToCircle from '../plugins/convertEllipseToCircle.js';
import * as convertOneStopGradients from '../plugins/convertOneStopGradients.js';
import * as convertPathData from '../plugins/convertPathData.js';
import * as convertShapeToPath from '../plugins/convertShapeToPath.js';
import * as convertStyleToAttrs from '../plugins/convertStyleToAttrs.js';
import * as convertTransform from '../plugins/convertTransform.js';
import * as mergeStyles from '../plugins/mergeStyles.js';
import * as inlineStyles from '../plugins/inlineStyles.js';
import * as mergePaths from '../plugins/mergePaths.js';
import * as minifyStyles from '../plugins/minifyStyles.js';
import * as moveElemsAttrsToGroup from '../plugins/moveElemsAttrsToGroup.js';
import * as moveGroupAttrsToElems from '../plugins/moveGroupAttrsToElems.js';
import * as prefixIds from '../plugins/prefixIds.js';
import * as removeAttributesBySelector from '../plugins/removeAttributesBySelector.js';
import * as removeAttrs from '../plugins/removeAttrs.js';
import * as removeComments from '../plugins/removeComments.js';
import * as removeDesc from '../plugins/removeDesc.js';
import * as removeDimensions from '../plugins/removeDimensions.js';
import * as removeDoctype from '../plugins/removeDoctype.js';
import * as removeEditorsNSData from '../plugins/removeEditorsNSData.js';
import * as removeElementsByAttr from '../plugins/removeElementsByAttr.js';
import * as removeEmptyAttrs from '../plugins/removeEmptyAttrs.js';
import * as removeEmptyContainers from '../plugins/removeEmptyContainers.js';
import * as removeEmptyText from '../plugins/removeEmptyText.js';
import * as removeHiddenElems from '../plugins/removeHiddenElems.js';
import * as removeMetadata from '../plugins/removeMetadata.js';
import * as removeNonInheritableGroupAttrs from '../plugins/removeNonInheritableGroupAttrs.js';
import * as removeOffCanvasPaths from '../plugins/removeOffCanvasPaths.js';
import * as removeRasterImages from '../plugins/removeRasterImages.js';
import * as removeScriptElement from '../plugins/removeScriptElement.js';
import * as removeStyleElement from '../plugins/removeStyleElement.js';
import * as removeTitle from '../plugins/removeTitle.js';
import * as removeUnknownsAndDefaults from '../plugins/removeUnknownsAndDefaults.js';
import * as removeUnusedNS from '../plugins/removeUnusedNS.js';
import * as removeUselessDefs from '../plugins/removeUselessDefs.js';
import * as removeUselessStrokeAndFill from '../plugins/removeUselessStrokeAndFill.js';
import * as removeViewBox from '../plugins/removeViewBox.js';
import * as removeXlink from '../plugins/removeXlink.js';
import * as removeXMLNS from '../plugins/removeXMLNS.js';
import * as removeXMLProcInst from '../plugins/removeXMLProcInst.js';
import * as reusePaths from '../plugins/reusePaths.js';
import * as sortAttrs from '../plugins/sortAttrs.js';
import * as sortDefsChildren from '../plugins/sortDefsChildren.js';

exports.builtin = [
require('../plugins/preset-default.js'),
require('../plugins/addAttributesToSVGElement.js'),
require('../plugins/addClassesToSVGElement.js'),
require('../plugins/applyTransformsShapes.js'),
require('../plugins/cleanupAttrs.js'),
require('../plugins/cleanupEnableBackground.js'),
require('../plugins/cleanupIds.js'),
require('../plugins/cleanupListOfValues.js'),
require('../plugins/cleanupNumericValues.js'),
require('../plugins/collapseGroups.js'),
require('../plugins/convertColors.js'),
require('../plugins/convertEllipseToCircle.js'),
require('../plugins/convertOneStopGradients.js'),
require('../plugins/convertPathData.js'),
require('../plugins/convertShapeToPath.js'),
require('../plugins/convertStyleToAttrs.js'),
require('../plugins/convertTransform.js'),
require('../plugins/mergeStyles.js'),
require('../plugins/inlineStyles.js'),
require('../plugins/mergePaths.js'),
require('../plugins/minifyStyles.js'),
require('../plugins/moveElemsAttrsToGroup.js'),
require('../plugins/moveGroupAttrsToElems.js'),
require('../plugins/prefixIds.js'),
require('../plugins/removeAttributesBySelector.js'),
require('../plugins/removeAttrs.js'),
require('../plugins/removeComments.js'),
require('../plugins/removeDesc.js'),
require('../plugins/removeDimensions.js'),
require('../plugins/removeDoctype.js'),
require('../plugins/removeEditorsNSData.js'),
require('../plugins/removeElementsByAttr.js'),
require('../plugins/removeEmptyAttrs.js'),
require('../plugins/removeEmptyContainers.js'),
require('../plugins/removeEmptyText.js'),
require('../plugins/removeHiddenElems.js'),
require('../plugins/removeMetadata.js'),
require('../plugins/removeNonInheritableGroupAttrs.js'),
require('../plugins/removeOffCanvasPaths.js'),
require('../plugins/removeRasterImages.js'),
require('../plugins/removeScriptElement.js'),
require('../plugins/removeStyleElement.js'),
require('../plugins/removeTitle.js'),
require('../plugins/removeUnknownsAndDefaults.js'),
require('../plugins/removeUnusedNS.js'),
require('../plugins/removeUselessDefs.js'),
require('../plugins/removeUselessStrokeAndFill.js'),
require('../plugins/removeViewBox.js'),
require('../plugins/removeXlink.js'),
require('../plugins/removeXMLNS.js'),
require('../plugins/removeXMLProcInst.js'),
require('../plugins/reusePaths.js'),
require('../plugins/sortAttrs.js'),
require('../plugins/sortDefsChildren.js'),
export const builtin = [
presetDefault,
addAttributesToSVGElement,
addClassesToSVGElement,
applyTransformsShapes,
cleanupAttrs,
cleanupEnableBackground,
cleanupIds,
cleanupListOfValues,
cleanupNumericValues,
collapseGroups,
convertColors,
convertEllipseToCircle,
convertOneStopGradients,
convertPathData,
convertShapeToPath,
convertStyleToAttrs,
convertTransform,
mergeStyles,
inlineStyles,
mergePaths,
minifyStyles,
moveElemsAttrsToGroup,
moveGroupAttrsToElems,
prefixIds,
removeAttributesBySelector,
removeAttrs,
removeComments,
removeDesc,
removeDimensions,
removeDoctype,
removeEditorsNSData,
removeElementsByAttr,
removeEmptyAttrs,
removeEmptyContainers,
removeEmptyText,
removeHiddenElems,
removeMetadata,
removeNonInheritableGroupAttrs,
removeOffCanvasPaths,
removeRasterImages,
removeScriptElement,
removeStyleElement,
removeTitle,
removeUnknownsAndDefaults,
removeUnusedNS,
removeUselessDefs,
removeUselessStrokeAndFill,
removeViewBox,
removeXlink,
removeXMLNS,
removeXMLProcInst,
reusePaths,
sortAttrs,
sortDefsChildren,
];
29 changes: 13 additions & 16 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
'use strict';

/**
* @typedef {import('./types').XastNode} XastNode
* @typedef {import('./types').XastInstruction} XastInstruction
* @typedef {import('./types').XastDoctype} XastDoctype
* @typedef {import('./types').XastComment} XastComment
* @typedef {import('./types').XastRoot} XastRoot
* @typedef {import('./types').XastElement} XastElement
* @typedef {import('./types').XastCdata} XastCdata
* @typedef {import('./types').XastText} XastText
* @typedef {import('./types').XastParent} XastParent
* @typedef {import('./types').XastChild} XastChild
* @typedef {import('./types.js').XastNode} XastNode
* @typedef {import('./types.js').XastInstruction} XastInstruction
* @typedef {import('./types.js').XastDoctype} XastDoctype
* @typedef {import('./types.js').XastComment} XastComment
* @typedef {import('./types.js').XastRoot} XastRoot
* @typedef {import('./types.js').XastElement} XastElement
* @typedef {import('./types.js').XastCdata} XastCdata
* @typedef {import('./types.js').XastText} XastText
* @typedef {import('./types.js').XastParent} XastParent
* @typedef {import('./types.js').XastChild} XastChild
*/

// @ts-ignore sax will be replaced with something else later
const SAX = require('@trysound/sax');
const { textElems } = require('../plugins/_collections');
import SAX from '@trysound/sax';
import { textElems } from '../plugins/_collections.js';

class SvgoParserError extends Error {
/**
Expand Down Expand Up @@ -89,7 +87,7 @@ const config = {
*
* @type {(data: string, from?: string) => XastRoot}
*/
const parseSvg = (data, from) => {
export const parseSvg = (data, from) => {
const sax = SAX.parser(config.strict, config);
/**
* @type {XastRoot}
Expand Down Expand Up @@ -259,4 +257,3 @@ const parseSvg = (data, from) => {
sax.write(data).close();
return root;
};
exports.parseSvg = parseSvg;
21 changes: 10 additions & 11 deletions lib/path.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const { removeLeadingZero } = require('./svgo/tools');
import { removeLeadingZero, toFixed } from './svgo/tools.js';

/**
* @typedef {import('./types').PathDataItem} PathDataItem
* @typedef {import('./types').PathDataCommand} PathDataCommand
* @typedef {import('./types.js').PathDataItem} PathDataItem
* @typedef {import('./types.js').PathDataCommand} PathDataCommand
*/

// Based on https://www.w3.org/TR/SVG11/paths.html#PathDataBNF
Expand Down Expand Up @@ -137,7 +135,7 @@ const readNumber = (string, cursor) => {
/**
* @type {(string: string) => PathDataItem[]}
*/
const parsePathData = (string) => {
export const parsePathData = (string) => {
/**
* @type {PathDataItem[]}
*/
Expand Down Expand Up @@ -240,7 +238,6 @@ const parsePathData = (string) => {
}
return pathData;
};
exports.parsePathData = parsePathData;

/**
* @type {(number: number, precision?: number) => {
Expand All @@ -250,8 +247,7 @@ exports.parsePathData = parsePathData;
*/
const roundAndStringify = (number, precision) => {
if (precision != null) {
const ratio = 10 ** precision;
number = Math.round(number * ratio) / ratio;
number = toFixed(number, precision);
}

return {
Expand Down Expand Up @@ -317,7 +313,11 @@ const stringifyArgs = (command, args, precision, disableSpaceAfterFlags) => {
* @param {StringifyPathDataOptions} options
* @returns {string}
*/
const stringifyPathData = ({ pathData, precision, disableSpaceAfterFlags }) => {
export const stringifyPathData = ({
pathData,
precision,
disableSpaceAfterFlags,
}) => {
if (pathData.length === 1) {
const { command, args } = pathData[0];
return (
Expand Down Expand Up @@ -378,4 +378,3 @@ const stringifyPathData = ({ pathData, precision, disableSpaceAfterFlags }) => {

return result;
};
exports.stringifyPathData = stringifyPathData;
Loading

0 comments on commit 40c1275

Please sign in to comment.