1
0
mirror of https://github.com/svg/svgo.git synced 2025-09-14 09:49:20 +03:00
Commit Graph

411 Commits

Author SHA1 Message Date
Bogdan Chadkin
bb3f1a99ef [new plugin] reimplement inlineDefs
The code is taken from https://github.com/svg/svgo/pull/976, refactored
with new api, covered types and simplified.

Plugin has no dependencies so can be used without changing.

```
const inlineDefs = require('./inlineDefs.js');

module.exports = {
  plugins: [
    'preset-default',
    inlineDefs
  ]
};
```
2022-02-21 19:12:53 +03:00
Bogdan Chadkin
068db0e894 Refactor applyTransforms (#1637)
- applyTransforms is prepared to be a separate plugin, in v3 I will
  remove it from convertPathData
- migrated to tsdoc
- removed optimisation with stroke-width inside id (still not idea how
  it worked)
- added deoptimisation when id is on element to fix redefining in `<use>`

Note: review with hidden whitespaces
2022-01-19 20:41:10 +03:00
Bogdan Chadkin
700f203169 Fix newline in tests 2021-12-25 15:03:29 +03:00
Bogdan Chadkin
72b972261d Refactor inlineStyles (#1601)
This is a big one

- got rid from another closestByName usage
- delegated removing empty defs elements to removeEmptyContainers plugin
- got rid from all css-tools usages (most inlineStyles code was there
  for some reason)
- combined a few loops
- fixed useMqs option (I would remove it in v3 for simplicity as it
  seems nobody use it)
2021-10-30 01:31:52 +03:00
Bogdan Chadkin
4b4391fbe3 Refactor basic cli tests (#1595)
Moved some tests to cli.test.js and got rid from mock-stdin dependency.
2021-10-15 12:34:24 +03:00
Bogdan Chadkin
4377ea38c4 Refactor removeEmptyAttrs (#1594)
- migrated to visitor plugin api
- covered with tsdoc
2021-10-07 14:07:06 +03:00
Ilya Skriblovsky
6235264cf3 Fix prefixIds plugin to properly handle url()s in style="..." (#1592)
`prefixIds` plugin currently breaks url()-links inside `style` attributes:

```javascript
optimize(
  `<g style="fill:url(#brush-id);stroke:url(#pen-id)"/>`,
  { plugins: ['prefixIds'] }
).data
```
will generate `<g style=""/>`. Seems like `prefixIds` assumes that attribute's whole value might be `url()`, but this is not the case for the `style` attribute.

This fix solves the issue by preserving all attribute's content other than #id inside url(). It also adds some more tests for the `prefixIds` plugin.
2021-09-30 14:40:40 +03:00
Bogdan Chadkin
203db9aaec Replace strip-ansi with --no-color flag (#1588)
nanocolors checks for --color and --no-color flags to force or disable
output coloring. Which can be used instead of strip-ansi package which
btw introduced vulnerability recently.
2021-09-30 14:39:12 +03:00
Bogdan Chadkin
543346ca97 Refactor moveElemsAttrsToGroup (#1574)
- migrated to visitor plugin api
- covered with tsdoc
- added more test cases
- restructured and simplified code
2021-09-23 21:52:46 +03:00
Bogdan Chadkin
9ebff13725 Refactor mergeStyles (#1575)
- covered with tsdoc
- replace another closestByName usage with visitSkip symbol to skip
  subtree instead of skipping element by ancestor

Better review with hidden whitespaces as most code just got bigger indent.
2021-09-23 21:48:54 +03:00
Samuel Vaillant
8af10de8d4 fix(plugin): removeAttrs: warn without attrs (#1582) 2021-09-23 21:48:14 +03:00
Bogdan Chadkin
6e23b9cf56 Cover svg parser with tsdoc (#1584)
Moved to lib/parser.js. The code will be slightly simpler
when JSAPI will be removed in v3.
2021-09-23 21:45:22 +03:00
Bogdan Chadkin
7111c52f96 Support es modules (#1583)
Ref https://github.com/svg/svgo/issues/1579

In config of course. Projects with type:module can now use modules to
export config

```js
export default {
  plugins: []
}
```

Also added support for resolving svgo.config.mjs and svgo.config.cjs.

Moved loadConfig tests to svgo-node tests.

mjs test is skipped for now in node 10, just don't use modules there
2021-09-23 21:44:55 +03:00
Bogdan Chadkin
1c551a87c0 Refactor sortAttrs plugin (#1564)
- covered with tsdoc
- migrated to visitor plugin api
- slightly simplified (hope so) logic by avoiding loop over order array
  in every compare function call
- rewrote tests
2021-09-13 16:16:56 +03:00
Bogdan Chadkin
3d22a5b23d Refactor prefixIds (#1561)
Ref https://github.com/svg/svgo/issues/1499

- migrated to visitor plugin api
- covered with tsdoc
- made the plugin idempotent as requested a few times
  Now even manually running svgo a few times will not duplicate
  prefix in ids and classes
- run each plugin test twice to see which plugin need to run many times
  ideally idempotent plugins will allow to get rid of multipass option in v3
2021-09-13 16:16:38 +03:00
Bogdan Chadkin
77102ed096 Add better parser errors (#1553)
Old SVGO errors were not very helpful. Packages like cssnano
(postcss-svgo) had to deal with a lot of issues which are hard to debug
with old errors.

```
Error: Error in parsing SVG: Unquoted attribute value
Line: 1
Column: 29
Char: 6
File: input.svg
```

New errors are more informative and may solve many struggles

```
Error: SvgoParserError: input.svg:2:29: Unquoted attribute value

  1 | <svg viewBox="0 0 120 120">
> 2 |   <circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
    |                             ^
  3 | </svg>
  4 |
```
2021-09-12 01:09:10 +03:00
Bogdan Chadkin
93143f32c1 Refactor removeUselessStrokeAndFill (#1549)
The logic is a little messy. Will be better when we drop node 12 support
and use optional chaining.

- migrated to visitor plugin api
- covered with types
- get rid from patching params as plugin state
- replaced many node.computedAttr() with style manager
- enabled and fixed removeNone param test (was merged as muted back in 2017)
- added ability to return null and not run visitor in plugins
2021-09-05 17:34:13 +03:00
Bogdan Chadkin
fab4b255c7 Add xmlns:xlink in reusePaths plugin (#1555)
Ref https://github.com/svg/svgo/issues/1200
2021-09-03 20:57:01 +03:00
Bogdan Chadkin
1e5236d813 Refactor reusePaths plugin (#1551)
- migrated to visitor plugin api; combination of enter and exit helped
  to fit into single traverse
- got rid from the only node.clone() usage in the project so no need to
  reimplement it
- the logic is a bit simplified
2021-09-01 15:47:04 +03:00
Bogdan Chadkin
5ad2d4a5f6 Refactor removeOffCanvasPaths plugin (#1545)
- refactored with visitor plugin api
- covered with types
- replaced parentNode traverse with visitSkip symbol
- replaced regex path parser with parsePathData
- get rid from global state which lead test case to invalid state
2021-08-27 17:15:18 +03:00
Bogdan Chadkin
ac8edbaf41 Fix applying transform to arc with zero radius
Ref https://github.com/svg/svgo/issues/1500
2021-08-27 01:03:11 +03:00
Bogdan Chadkin
cce036f285 Refactor convertColors (#1543)
- refactored with visitor plugin api
- covered with types
2021-08-25 20:52:21 +03:00
Bogdan Chadkin
539237ce87 Normalize path data structure (#1538)
Use modern path data structure in all plugins.
Will be easier to cover with types.
2021-08-24 00:01:18 +03:00
Bogdan Chadkin
98c023bdb0 Drop node.class usages (#1533)
We are gonna remove everything outside of xast.
Here's dropped class prop.
2021-08-21 19:42:32 +03:00
Bogdan Chadkin
07f8d606e0 Implement preset-default plugin (#1513)
I saw complaints about `extendDefaultPlugins` api

- it cannot be used when svgo is installed globally
- it requires svgo to be installed when using svgo-loader or svgo-jsx
- it prevents using serializable config formats like json

In this diff I introduced the new plugin which is a bundle of all
default plugins.

```js
module.exports = {
  plugins: [
    'preset_default',
    // or
    {
      name: 'preset_default',
      floatPrecision: 4,
      overrides: {
        convertPathData: {
          applyTransforms: false
        }
      }
    }
  ]
}
```
2021-08-13 19:07:08 +03:00
Bogdan Chadkin
9e578b515a Reset current cursor on Z
Ref https://github.com/svg/svgo/issues/1510
2021-08-13 18:39:55 +03:00
Bogdan Chadkin
c3695ae533 Migrate to jest (#1520)
Mocha doesn't have a lot of features provided by jest.
There is a great assertion library out of the box.
And the most cool feature is inline snapshots.
Mocha also hides errors which makes debugging a nightmare sometimes.
2021-08-12 18:06:10 +03:00
Bogdan Chadkin
06110b4fc0 Convert mergePaths to visitor 2021-03-29 03:08:15 +03:00
Bogdan Chadkin
368a67b70f Convert removeHiddenElems to visitor 2021-03-29 02:34:34 +03:00
Bogdan Chadkin
17aaf3617f Cleanup collapseGroups tests 2021-03-28 20:23:39 +03:00
Bogdan Chadkin
27bef1a954 Add "visitor" plugins support (#1454)
Visitor is a simple pattern which helps to avoid many type checks
and provide both "perItem" and "perItemReverse" functionality without
fragmentation.

The most important case is an ability to define state which in many
plugins specified either on module level or by polluting `params`.

In this diff I added visit and detachFromParent utilities and refactored
new mergeStyles plugin with it.

Also fixed bug when cdata content is merged into "text" node which is
not always valid.
2021-03-28 11:20:17 +03:00
strarsis
19c77d2398 Add mergeStyles plugin (#1381) 2021-03-27 16:59:56 +03:00
Bogdan Chadkin
d89d36eace Split regression extracter and runner (#1451)
A lot of new sources of regression tests may come and it's important to
make debug simpler.

Now regression-extract.js downloads and write svg files into
test/regression-fixtures. regression.js run each svg in this folder.
Mismatched svg diff is written into test/regression-diffs.
2021-03-25 00:12:13 +03:00
Bogdan Chadkin
eb934b40f6 Serve svg for regressions and cache w3c test suite 2021-03-23 00:29:29 +03:00
Bogdan Chadkin
7389bcddbf Override default floatPrecision with global
Ref https://github.com/svg/svgo/issues/1426

Previously extendDefaultPlugins incorrectly overrided global
floatPrecision.
2021-03-22 19:36:05 +03:00
Bogdan Chadkin
3d4adb6b04 Simplify number rendering and fix -0 in path
Ref https://github.com/svg/svgo/issues/1422
2021-03-22 14:38:06 +03:00
Bogdan Chadkin
447f82ca6b Convert addAttributesToSVGElement to item plugin (#1448)
"full" plugins prevents from possible optimisation. We need to migrate
all plugins to "perItem" type and later implement visitor plugin api to
allow state.
2021-03-22 01:24:41 +03:00
XhmikosR
13a0ad056b Specify --ignore-path for ESLint (#1443)
Otherwise, all files are included
2021-03-21 17:57:55 +03:00
XhmikosR
bc5c4ea29c Add a test for removeXMLNS plugin (#1444) 2021-03-21 17:30:36 +03:00
XhmikosR
3390df1186 removeOffCanvasPaths: Add one more test (#1445) 2021-03-21 13:30:20 +03:00
XhmikosR
f44612e7a2 addClassesToSVGElement: add one more test (#1441) 2021-03-21 10:26:57 +03:00
XhmikosR
21d24006fd Remove the executable flag from files. (#1439) 2021-03-20 20:46:26 +03:00
XhmikosR
b903de9554 test/config/_index.js: fix find usage (#1438) 2021-03-20 19:51:33 +03:00
Bogdan Chadkin
61657433e1 Refactor apply transforms
- handle each command separately
- handle both relative and absolute commands
- moved into _applyTransforms.js to convert eventually into plugin
- apply transforms before converting into relative

These changes makes code independent and easy to work with.
2021-03-15 21:24:23 +03:00
Bogdan Chadkin
5314c12c99 Convert element children to xast
Ref https://github.com/syntax-tree/xast

Renamed content to children to match xast spec.
2021-03-11 12:29:13 +03:00
Bogdan Chadkin
3e1bfd528d Convert attributes to xast with proxy fallback
There is a lot of attributes manipulation which is hard to remove at
once. In this diff I added `attributes` object and wrapped it as proxy
for `attrs` field.
2021-03-10 23:11:09 +03:00
Bogdan Chadkin
e82a672bbf Prepare root and element nodes for xast
Ref https://github.com/syntax-tree/xast

- added type: root | element
- renamed elem to name
- replaced "elem" property checks with check for correct type
2021-03-10 18:49:26 +03:00
Bogdan Chadkin
c50decb438 Convert cdata and text nodes to xast
Ref https://github.com/syntax-tree/xast

More consistent naming and distinction by type looks a bit cleaner.
2021-03-10 16:16:11 +03:00
Bogdan Chadkin
8622a5f31b Convert to xast doctype, instruction and comment nodes
Here we add [xast](https://github.com/syntax-tree/xast) support
to three basic nodes: doctype, instruction and comment

Some tests are rewritten instead of checking each field to `.include`
assertion which is able to match shape of object.
2021-03-10 14:10:40 +03:00
Bogdan Chadkin
d1d6e5efe8 Remove prefix/local support in elements and attributes (#1413)
These parts of element and attribute name are easy to extract.
Now we can easily replace attrs with xast attributes object.
2021-03-10 13:26:15 +03:00