Skip to content

Commit 8390add

Browse files
authored
feat(removeUnknownAndDefaults): apply to xml declarations (#1872)
1 parent d6ff70b commit 8390add

5 files changed

+45
-9
lines changed

docs/03-plugins/remove-unknowns-and-defaults.mdx

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ svgo:
1313
defaultAttrs:
1414
description: If to remove attributes that are assigned their default value anyway.
1515
default: true
16+
defaultMarkupDeclarations:
17+
description: If to remove XML declarations that are assigned their default value. XML declarations are the properties in the <code>&lt;?xml … ?&gt;</code> block at the top of the document.
18+
default: true
1619
uselessOverrides:
1720
description: If to remove attributes that are being if the same value is being inherited by it's parent element anyway.
1821
default: true
@@ -29,6 +32,8 @@ svgo:
2932

3033
Removes unknown elements and attributes, as well as attributes that are set to their default value.
3134

35+
This can also remove defaults from the XML declaration if present in the document, namely [`standalone`](https://www.w3.org/TR/REC-xml/#sec-rmd) if it's set to `no`.
36+
3237
## Usage
3338

3439
<PluginUsage/>

docs/03-plugins/remove-xml-proc-inst.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ An XML declaration is the line at the top of an XML file to indicate document me
1717

1818
The XML declaration is optional in [XML 1.0](https://www.w3.org/TR/REC-xml/#sec-prolog-dtd), but mandatory in the [XML 1.1](https://www.w3.org/TR/2006/REC-xml11-20060816/#sec-prolog-dtd). If the XML declaration is omitted, the document is assumed to follow the XML 1.0 specifications, which won't impact SVG documents.
1919

20-
It can be safely removed without impacting compatibility.
20+
It can be safely removed without impacting compatibility with SVG clients. However, some tools may fail to detect the MIME-type as `image/svg+xml` if this is removed.
2121

2222
## Usage
2323

plugins/plugins-types.d.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type DefaultPlugins = {
1414
cleanupIds: {
1515
remove?: boolean;
1616
minify?: boolean;
17-
preserve?: Array<string>;
18-
preservePrefixes?: Array<string>;
17+
preserve?: string[];
18+
preservePrefixes?: string[];
1919
force?: boolean;
2020
};
2121
cleanupNumericValues: {
@@ -146,7 +146,7 @@ type DefaultPlugins = {
146146
};
147147
removeDoctype: void;
148148
removeEditorsNSData: {
149-
additionalNamespaces?: Array<string>;
149+
additionalNamespaces?: string[];
150150
};
151151
removeEmptyAttrs: void;
152152
removeEmptyContainers: void;
@@ -179,6 +179,12 @@ type DefaultPlugins = {
179179
unknownContent?: boolean;
180180
unknownAttrs?: boolean;
181181
defaultAttrs?: boolean;
182+
/**
183+
* If to remove XML declarations that are assigned their default value. XML
184+
* declarations are the properties in the `<?xml … ?>` block at the top of
185+
* the document.
186+
*/
187+
defaultMarkupDeclarations?: boolean;
182188
uselessOverrides?: boolean;
183189
keepDataAttrs?: boolean;
184190
keepAriaAttrs?: boolean;
@@ -194,7 +200,7 @@ type DefaultPlugins = {
194200
removeViewBox: void;
195201
removeXMLProcInst: void;
196202
sortAttrs: {
197-
order?: Array<string>;
203+
order?: string[];
198204
xmlnsOrder?: 'front' | 'alphabetical';
199205
};
200206
sortDefsChildren: void;
@@ -262,17 +268,17 @@ export type BuiltinsWithRequiredParams = {
262268
};
263269
addClassesToSVGElement: {
264270
className?: string;
265-
classNames?: Array<string>;
271+
classNames?: string[];
266272
};
267273
removeAttributesBySelector: any;
268274
removeAttrs: {
269275
elemSeparator?: string;
270276
preserveCurrentColor?: boolean;
271-
attrs: string | Array<string>;
277+
attrs: string | string[];
272278
};
273279
removeElementsByAttr: {
274-
id?: string | Array<string>;
275-
class?: string | Array<string>;
280+
id?: string | string[];
281+
class?: string | string[];
276282
};
277283
};
278284

plugins/removeUnknownsAndDefaults.js

+8
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ exports.fn = (root, params) => {
9999
unknownContent = true,
100100
unknownAttrs = true,
101101
defaultAttrs = true,
102+
defaultMarkupDeclarations = true,
102103
uselessOverrides = true,
103104
keepDataAttrs = true,
104105
keepAriaAttrs = true,
@@ -107,6 +108,13 @@ exports.fn = (root, params) => {
107108
const stylesheet = collectStylesheet(root);
108109

109110
return {
111+
instruction: {
112+
enter: (node) => {
113+
if (defaultMarkupDeclarations) {
114+
node.value = node.value.replace(/\s*standalone\s*=\s*(["'])no\1/, '');
115+
}
116+
},
117+
},
110118
element: {
111119
enter: (node, parentNode) => {
112120
// skip namespaced elements
Loading

0 commit comments

Comments
 (0)