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
]
};
```
- 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
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)
`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.
- 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.
- 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
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
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
- 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
- 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
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.
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.
"full" plugins prevents from possible optimisation. We need to migrate
all plugins to "perItem" type and later implement visitor plugin api to
allow state.
- 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.
Ref https://github.com/svg/svgo/issues/777
Currently a lot of optimisations are attributes specific and may be
broken because of inline or shared styles.
In this diff I'm trying to solve the problem with getComputedStyle
analog.
`computeStyle` collects attributes, shared css rules, inline styles
and inherited styles and checks whether they can be statically optimised
or left as deoptimisation.
Ref https://github.com/svg/svgo/issues/32
Added `stringifyPathData` utility to produce small as possible path by
- matching leading moveto and line to commands (m -> l, M -> L)
- combining moveto and lineto commands
- avoiding space before decimal numbers if possible
Ref https://github.com/svg/svgo/issues/1385
With disabled convertStyleToAttrs applyTransform fails to transform
inline styles. They are considered as deoptimisation for now.
Future style manager should fix the problem.