Skip to content

Commit

Permalink
feat(removeUnknownAndDefaults): apply to xml declarations (#1872)
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco authored Dec 7, 2023
1 parent d6ff70b commit 8390add
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
5 changes: 5 additions & 0 deletions docs/03-plugins/remove-unknowns-and-defaults.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ svgo:
defaultAttrs:
description: If to remove attributes that are assigned their default value anyway.
default: true
defaultMarkupDeclarations:
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.
default: true
uselessOverrides:
description: If to remove attributes that are being if the same value is being inherited by it's parent element anyway.
default: true
Expand All @@ -29,6 +32,8 @@ svgo:

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

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`.

## Usage

<PluginUsage/>
Expand Down
2 changes: 1 addition & 1 deletion docs/03-plugins/remove-xml-proc-inst.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ An XML declaration is the line at the top of an XML file to indicate document me

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.

It can be safely removed without impacting compatibility.
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.

## Usage

Expand Down
22 changes: 14 additions & 8 deletions plugins/plugins-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type DefaultPlugins = {
cleanupIds: {
remove?: boolean;
minify?: boolean;
preserve?: Array<string>;
preservePrefixes?: Array<string>;
preserve?: string[];
preservePrefixes?: string[];
force?: boolean;
};
cleanupNumericValues: {
Expand Down Expand Up @@ -146,7 +146,7 @@ type DefaultPlugins = {
};
removeDoctype: void;
removeEditorsNSData: {
additionalNamespaces?: Array<string>;
additionalNamespaces?: string[];
};
removeEmptyAttrs: void;
removeEmptyContainers: void;
Expand Down Expand Up @@ -179,6 +179,12 @@ type DefaultPlugins = {
unknownContent?: boolean;
unknownAttrs?: boolean;
defaultAttrs?: boolean;
/**
* If to remove XML declarations that are assigned their default value. XML
* declarations are the properties in the `<?xml … ?>` block at the top of
* the document.
*/
defaultMarkupDeclarations?: boolean;
uselessOverrides?: boolean;
keepDataAttrs?: boolean;
keepAriaAttrs?: boolean;
Expand All @@ -194,7 +200,7 @@ type DefaultPlugins = {
removeViewBox: void;
removeXMLProcInst: void;
sortAttrs: {
order?: Array<string>;
order?: string[];
xmlnsOrder?: 'front' | 'alphabetical';
};
sortDefsChildren: void;
Expand Down Expand Up @@ -262,17 +268,17 @@ export type BuiltinsWithRequiredParams = {
};
addClassesToSVGElement: {
className?: string;
classNames?: Array<string>;
classNames?: string[];
};
removeAttributesBySelector: any;
removeAttrs: {
elemSeparator?: string;
preserveCurrentColor?: boolean;
attrs: string | Array<string>;
attrs: string | string[];
};
removeElementsByAttr: {
id?: string | Array<string>;
class?: string | Array<string>;
id?: string | string[];
class?: string | string[];
};
};

Expand Down
8 changes: 8 additions & 0 deletions plugins/removeUnknownsAndDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ exports.fn = (root, params) => {
unknownContent = true,
unknownAttrs = true,
defaultAttrs = true,
defaultMarkupDeclarations = true,
uselessOverrides = true,
keepDataAttrs = true,
keepAriaAttrs = true,
Expand All @@ -107,6 +108,13 @@ exports.fn = (root, params) => {
const stylesheet = collectStylesheet(root);

return {
instruction: {
enter: (node) => {
if (defaultMarkupDeclarations) {
node.value = node.value.replace(/\s*standalone\s*=\s*(["'])no\1/, '');
}
},
},
element: {
enter: (node, parentNode) => {
// skip namespaced elements
Expand Down
17 changes: 17 additions & 0 deletions test/plugins/removeUnknownsAndDefaults.16.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8390add

Please sign in to comment.