Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | exports.type = 'visitor';
|
---|
| 4 | exports.name = 'removeViewBox';
|
---|
| 5 | exports.active = true;
|
---|
| 6 | exports.description = 'removes viewBox attribute when possible';
|
---|
| 7 |
|
---|
| 8 | const viewBoxElems = ['svg', 'pattern', 'symbol'];
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | * Remove viewBox attr which coincides with a width/height box.
|
---|
| 12 | *
|
---|
| 13 | * @see https://www.w3.org/TR/SVG11/coords.html#ViewBoxAttribute
|
---|
| 14 | *
|
---|
| 15 | * @example
|
---|
| 16 | * <svg width="100" height="50" viewBox="0 0 100 50">
|
---|
| 17 | * ⬇
|
---|
| 18 | * <svg width="100" height="50">
|
---|
| 19 | *
|
---|
| 20 | * @author Kir Belevich
|
---|
| 21 | *
|
---|
| 22 | * @type {import('../lib/types').Plugin<void>}
|
---|
| 23 | */
|
---|
| 24 | exports.fn = () => {
|
---|
| 25 | return {
|
---|
| 26 | element: {
|
---|
| 27 | enter: (node, parentNode) => {
|
---|
| 28 | if (
|
---|
| 29 | viewBoxElems.includes(node.name) &&
|
---|
| 30 | node.attributes.viewBox != null &&
|
---|
| 31 | node.attributes.width != null &&
|
---|
| 32 | node.attributes.height != null
|
---|
| 33 | ) {
|
---|
| 34 | // TODO remove width/height for such case instead
|
---|
| 35 | if (node.name === 'svg' && parentNode.type !== 'root') {
|
---|
| 36 | return;
|
---|
| 37 | }
|
---|
| 38 | const nums = node.attributes.viewBox.split(/[ ,]+/g);
|
---|
| 39 | if (
|
---|
| 40 | nums[0] === '0' &&
|
---|
| 41 | nums[1] === '0' &&
|
---|
| 42 | node.attributes.width.replace(/px$/, '') === nums[2] && // could use parseFloat too
|
---|
| 43 | node.attributes.height.replace(/px$/, '') === nums[3]
|
---|
| 44 | ) {
|
---|
| 45 | delete node.attributes.viewBox;
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | },
|
---|
| 49 | },
|
---|
| 50 | };
|
---|
| 51 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.