Last change
on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
2.0 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | const { visit } = require('../lib/xast.js');
|
---|
4 |
|
---|
5 | exports.type = 'visitor';
|
---|
6 | exports.name = 'cleanupEnableBackground';
|
---|
7 | exports.active = true;
|
---|
8 | exports.description =
|
---|
9 | 'remove or cleanup enable-background attribute when possible';
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Remove or cleanup enable-background attr which coincides with a width/height box.
|
---|
13 | *
|
---|
14 | * @see https://www.w3.org/TR/SVG11/filters.html#EnableBackgroundProperty
|
---|
15 | *
|
---|
16 | * @example
|
---|
17 | * <svg width="100" height="50" enable-background="new 0 0 100 50">
|
---|
18 | * ⬇
|
---|
19 | * <svg width="100" height="50">
|
---|
20 | *
|
---|
21 | * @author Kir Belevich
|
---|
22 | *
|
---|
23 | * @type {import('../lib/types').Plugin<void>}
|
---|
24 | */
|
---|
25 | exports.fn = (root) => {
|
---|
26 | const regEnableBackground =
|
---|
27 | /^new\s0\s0\s([-+]?\d*\.?\d+([eE][-+]?\d+)?)\s([-+]?\d*\.?\d+([eE][-+]?\d+)?)$/;
|
---|
28 |
|
---|
29 | let hasFilter = false;
|
---|
30 | visit(root, {
|
---|
31 | element: {
|
---|
32 | enter: (node) => {
|
---|
33 | if (node.name === 'filter') {
|
---|
34 | hasFilter = true;
|
---|
35 | }
|
---|
36 | },
|
---|
37 | },
|
---|
38 | });
|
---|
39 |
|
---|
40 | return {
|
---|
41 | element: {
|
---|
42 | enter: (node) => {
|
---|
43 | if (node.attributes['enable-background'] == null) {
|
---|
44 | return;
|
---|
45 | }
|
---|
46 | if (hasFilter) {
|
---|
47 | if (
|
---|
48 | (node.name === 'svg' ||
|
---|
49 | node.name === 'mask' ||
|
---|
50 | node.name === 'pattern') &&
|
---|
51 | node.attributes.width != null &&
|
---|
52 | node.attributes.height != null
|
---|
53 | ) {
|
---|
54 | const match =
|
---|
55 | node.attributes['enable-background'].match(regEnableBackground);
|
---|
56 | if (
|
---|
57 | match != null &&
|
---|
58 | node.attributes.width === match[1] &&
|
---|
59 | node.attributes.height === match[3]
|
---|
60 | ) {
|
---|
61 | if (node.name === 'svg') {
|
---|
62 | delete node.attributes['enable-background'];
|
---|
63 | } else {
|
---|
64 | node.attributes['enable-background'] = 'new';
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | } else {
|
---|
69 | //we don't need 'enable-background' if we have no filters
|
---|
70 | delete node.attributes['enable-background'];
|
---|
71 | }
|
---|
72 | },
|
---|
73 | },
|
---|
74 | };
|
---|
75 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.