schnee effeckt und fehler Korektur
This commit is contained in:
308
node_modules/svgo/plugins/removeUnknownsAndDefaults.js
generated
vendored
308
node_modules/svgo/plugins/removeUnknownsAndDefaults.js
generated
vendored
@@ -1,142 +1,218 @@
|
||||
'use strict';
|
||||
|
||||
exports.type = 'perItem';
|
||||
const { visitSkip, detachNodeFromParent } = require('../lib/xast.js');
|
||||
const { collectStylesheet, computeStyle } = require('../lib/style.js');
|
||||
const {
|
||||
elems,
|
||||
attrsGroups,
|
||||
elemsGroups,
|
||||
attrsGroupsDefaults,
|
||||
presentationNonInheritableGroupAttrs,
|
||||
} = require('./_collections');
|
||||
|
||||
exports.type = 'visitor';
|
||||
exports.name = 'removeUnknownsAndDefaults';
|
||||
exports.active = true;
|
||||
exports.description =
|
||||
'removes unknown elements content and attributes, removes attrs with default values';
|
||||
|
||||
exports.description = 'removes unknown elements content and attributes, removes attrs with default values';
|
||||
// resolve all groups references
|
||||
|
||||
exports.params = {
|
||||
unknownContent: true,
|
||||
unknownAttrs: true,
|
||||
defaultAttrs: true,
|
||||
uselessOverrides: true,
|
||||
keepDataAttrs: true
|
||||
};
|
||||
|
||||
var collections = require('./_collections'),
|
||||
elems = collections.elems,
|
||||
attrsGroups = collections.attrsGroups,
|
||||
elemsGroups = collections.elemsGroups,
|
||||
attrsGroupsDefaults = collections.attrsGroupsDefaults,
|
||||
attrsInheritable = collections.inheritableAttrs;
|
||||
|
||||
// collect and extend all references
|
||||
for (var elem in elems) {
|
||||
elem = elems[elem];
|
||||
|
||||
if (elem.attrsGroups) {
|
||||
elem.attrs = elem.attrs || [];
|
||||
|
||||
elem.attrsGroups.forEach(function(attrsGroupName) {
|
||||
elem.attrs = elem.attrs.concat(attrsGroups[attrsGroupName]);
|
||||
|
||||
var groupDefaults = attrsGroupsDefaults[attrsGroupName];
|
||||
|
||||
if (groupDefaults) {
|
||||
elem.defaults = elem.defaults || {};
|
||||
|
||||
for (var attrName in groupDefaults) {
|
||||
elem.defaults[attrName] = groupDefaults[attrName];
|
||||
}
|
||||
}
|
||||
});
|
||||
/**
|
||||
* @type {Map<string, Set<string>>}
|
||||
*/
|
||||
const allowedChildrenPerElement = new Map();
|
||||
/**
|
||||
* @type {Map<string, Set<string>>}
|
||||
*/
|
||||
const allowedAttributesPerElement = new Map();
|
||||
/**
|
||||
* @type {Map<string, Map<string, string>>}
|
||||
*/
|
||||
const attributesDefaultsPerElement = new Map();
|
||||
|
||||
for (const [name, config] of Object.entries(elems)) {
|
||||
/**
|
||||
* @type {Set<string>}
|
||||
*/
|
||||
const allowedChildren = new Set();
|
||||
if (config.content) {
|
||||
for (const elementName of config.content) {
|
||||
allowedChildren.add(elementName);
|
||||
}
|
||||
|
||||
if (elem.contentGroups) {
|
||||
elem.content = elem.content || [];
|
||||
|
||||
elem.contentGroups.forEach(function(contentGroupName) {
|
||||
elem.content = elem.content.concat(elemsGroups[contentGroupName]);
|
||||
});
|
||||
}
|
||||
if (config.contentGroups) {
|
||||
for (const contentGroupName of config.contentGroups) {
|
||||
const elemsGroup = elemsGroups[contentGroupName];
|
||||
if (elemsGroup) {
|
||||
for (const elementName of elemsGroup) {
|
||||
allowedChildren.add(elementName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @type {Set<string>}
|
||||
*/
|
||||
const allowedAttributes = new Set();
|
||||
if (config.attrs) {
|
||||
for (const attrName of config.attrs) {
|
||||
allowedAttributes.add(attrName);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @type {Map<string, string>}
|
||||
*/
|
||||
const attributesDefaults = new Map();
|
||||
if (config.defaults) {
|
||||
for (const [attrName, defaultValue] of Object.entries(config.defaults)) {
|
||||
attributesDefaults.set(attrName, defaultValue);
|
||||
}
|
||||
}
|
||||
for (const attrsGroupName of config.attrsGroups) {
|
||||
const attrsGroup = attrsGroups[attrsGroupName];
|
||||
if (attrsGroup) {
|
||||
for (const attrName of attrsGroup) {
|
||||
allowedAttributes.add(attrName);
|
||||
}
|
||||
}
|
||||
const groupDefaults = attrsGroupsDefaults[attrsGroupName];
|
||||
if (groupDefaults) {
|
||||
for (const [attrName, defaultValue] of Object.entries(groupDefaults)) {
|
||||
attributesDefaults.set(attrName, defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
allowedChildrenPerElement.set(name, allowedChildren);
|
||||
allowedAttributesPerElement.set(name, allowedAttributes);
|
||||
attributesDefaultsPerElement.set(name, attributesDefaults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove unknown elements content and attributes,
|
||||
* remove attributes with default values.
|
||||
*
|
||||
* @param {Object} item current iteration item
|
||||
* @param {Object} params plugin params
|
||||
* @return {Boolean} if false, item will be filtered out
|
||||
*
|
||||
* @author Kir Belevich
|
||||
*
|
||||
* @type {import('../lib/types').Plugin<{
|
||||
* unknownContent?: boolean,
|
||||
* unknownAttrs?: boolean,
|
||||
* defaultAttrs?: boolean,
|
||||
* uselessOverrides?: boolean,
|
||||
* keepDataAttrs?: boolean,
|
||||
* keepAriaAttrs?: boolean,
|
||||
* keepRoleAttr?: boolean,
|
||||
* }>}
|
||||
*/
|
||||
exports.fn = function(item, params) {
|
||||
exports.fn = (root, params) => {
|
||||
const {
|
||||
unknownContent = true,
|
||||
unknownAttrs = true,
|
||||
defaultAttrs = true,
|
||||
uselessOverrides = true,
|
||||
keepDataAttrs = true,
|
||||
keepAriaAttrs = true,
|
||||
keepRoleAttr = false,
|
||||
} = params;
|
||||
const stylesheet = collectStylesheet(root);
|
||||
|
||||
// elems w/o namespace prefix
|
||||
if (item.isElem() && !item.prefix) {
|
||||
|
||||
var elem = item.elem;
|
||||
return {
|
||||
element: {
|
||||
enter: (node, parentNode) => {
|
||||
// skip namespaced elements
|
||||
if (node.name.includes(':')) {
|
||||
return;
|
||||
}
|
||||
// skip visiting foreignObject subtree
|
||||
if (node.name === 'foreignObject') {
|
||||
return visitSkip;
|
||||
}
|
||||
|
||||
// remove unknown element's content
|
||||
if (
|
||||
params.unknownContent &&
|
||||
!item.isEmpty() &&
|
||||
elems[elem] && // make sure we know of this element before checking its children
|
||||
elem !== 'foreignObject' // Don't check foreignObject
|
||||
) {
|
||||
item.content.forEach(function(content, i) {
|
||||
if (
|
||||
content.isElem() &&
|
||||
!content.prefix &&
|
||||
(
|
||||
(
|
||||
elems[elem].content && // Do we have a record of its permitted content?
|
||||
elems[elem].content.indexOf(content.elem) === -1
|
||||
) ||
|
||||
(
|
||||
!elems[elem].content && // we dont know about its permitted content
|
||||
!elems[content.elem] // check that we know about the element at all
|
||||
)
|
||||
)
|
||||
) {
|
||||
item.content.splice(i, 1);
|
||||
}
|
||||
});
|
||||
if (unknownContent && parentNode.type === 'element') {
|
||||
const allowedChildren = allowedChildrenPerElement.get(
|
||||
parentNode.name
|
||||
);
|
||||
if (allowedChildren == null || allowedChildren.size === 0) {
|
||||
// remove unknown elements
|
||||
if (allowedChildrenPerElement.get(node.name) == null) {
|
||||
detachNodeFromParent(node, parentNode);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// remove not allowed children
|
||||
if (allowedChildren.has(node.name) === false) {
|
||||
detachNodeFromParent(node, parentNode);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const allowedAttributes = allowedAttributesPerElement.get(node.name);
|
||||
const attributesDefaults = attributesDefaultsPerElement.get(node.name);
|
||||
const computedParentStyle =
|
||||
parentNode.type === 'element'
|
||||
? computeStyle(stylesheet, parentNode)
|
||||
: null;
|
||||
|
||||
// remove element's unknown attrs and attrs with default values
|
||||
if (elems[elem] && elems[elem].attrs) {
|
||||
|
||||
item.eachAttr(function(attr) {
|
||||
|
||||
if (
|
||||
attr.name !== 'xmlns' &&
|
||||
(attr.prefix === 'xml' || !attr.prefix) &&
|
||||
(!params.keepDataAttrs || attr.name.indexOf('data-') != 0)
|
||||
) {
|
||||
if (
|
||||
// unknown attrs
|
||||
(
|
||||
params.unknownAttrs &&
|
||||
elems[elem].attrs.indexOf(attr.name) === -1
|
||||
) ||
|
||||
// attrs with default values
|
||||
(
|
||||
params.defaultAttrs &&
|
||||
elems[elem].defaults &&
|
||||
elems[elem].defaults[attr.name] === attr.value && (
|
||||
attrsInheritable.indexOf(attr.name) < 0 ||
|
||||
!item.parentNode.computedAttr(attr.name)
|
||||
)
|
||||
) ||
|
||||
// useless overrides
|
||||
(
|
||||
params.uselessOverrides &&
|
||||
attrsInheritable.indexOf(attr.name) > -1 &&
|
||||
item.parentNode.computedAttr(attr.name, attr.value)
|
||||
)
|
||||
) {
|
||||
item.removeAttr(attr.name);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
for (const [name, value] of Object.entries(node.attributes)) {
|
||||
if (keepDataAttrs && name.startsWith('data-')) {
|
||||
continue;
|
||||
}
|
||||
if (keepAriaAttrs && name.startsWith('aria-')) {
|
||||
continue;
|
||||
}
|
||||
if (keepRoleAttr && name === 'role') {
|
||||
continue;
|
||||
}
|
||||
// skip xmlns attribute
|
||||
if (name === 'xmlns') {
|
||||
continue;
|
||||
}
|
||||
// skip namespaced attributes except xml:* and xlink:*
|
||||
if (name.includes(':')) {
|
||||
const [prefix] = name.split(':');
|
||||
if (prefix !== 'xml' && prefix !== 'xlink') {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
unknownAttrs &&
|
||||
allowedAttributes &&
|
||||
allowedAttributes.has(name) === false
|
||||
) {
|
||||
delete node.attributes[name];
|
||||
}
|
||||
if (
|
||||
defaultAttrs &&
|
||||
node.attributes.id == null &&
|
||||
attributesDefaults &&
|
||||
attributesDefaults.get(name) === value
|
||||
) {
|
||||
// keep defaults if parent has own or inherited style
|
||||
if (
|
||||
computedParentStyle == null ||
|
||||
computedParentStyle[name] == null
|
||||
) {
|
||||
delete node.attributes[name];
|
||||
}
|
||||
}
|
||||
if (uselessOverrides && node.attributes.id == null) {
|
||||
const style =
|
||||
computedParentStyle == null ? null : computedParentStyle[name];
|
||||
if (
|
||||
presentationNonInheritableGroupAttrs.includes(name) === false &&
|
||||
style != null &&
|
||||
style.type === 'static' &&
|
||||
style.value === value
|
||||
) {
|
||||
delete node.attributes[name];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user