PHP 7.4.33
Preview: removeUnknownsAndDefaults.js Size: 6.07 KB
/var/www/cookieconsent.bitkit.dk/httpdocs/node_modules/svgo/plugins/removeUnknownsAndDefaults.js
'use strict';

const { visitSkip, detachNodeFromParent } = require('../lib/xast.js');
const { collectStylesheet, computeStyle } = require('../lib/style.js');
const {
  elems,
  attrsGroups,
  elemsGroups,
  attrsGroupsDefaults,
  presentationNonInheritableGroupAttrs,
} = require('./_collections');

exports.name = 'removeUnknownsAndDefaults';
exports.description =
  'removes unknown elements content and attributes, removes attrs with default values';

// resolve all groups references

/**
 * @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 (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.
 *
 * @author Kir Belevich
 *
 * @type {import('./plugins-types').Plugin<'removeUnknownsAndDefaults'>}
 */
exports.fn = (root, params) => {
  const {
    unknownContent = true,
    unknownAttrs = true,
    defaultAttrs = true,
    defaultMarkupDeclarations = true,
    uselessOverrides = true,
    keepDataAttrs = true,
    keepAriaAttrs = true,
    keepRoleAttr = false,
  } = 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
        if (node.name.includes(':')) {
          return;
        }
        // skip visiting foreignObject subtree
        if (node.name === 'foreignObject') {
          return visitSkip;
        }

        // remove unknown element's content
        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
        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?.[name] == null) {
              delete node.attributes[name];
            }
          }
          if (uselessOverrides && node.attributes.id == null) {
            const style = computedParentStyle?.[name];
            if (
              presentationNonInheritableGroupAttrs.has(name) === false &&
              style != null &&
              style.type === 'static' &&
              style.value === value
            ) {
              delete node.attributes[name];
            }
          }
        }
      },
    },
  };
};

Directory Contents

Dirs: 0 × Files: 58
Name Size Perms Modified Actions
1.87 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.71 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
10.93 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.32 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
4.80 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
5.98 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
3.77 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
2.75 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
3.79 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
3.93 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
912 B lrw-r--r-- 2025-06-16 05:45:40
Edit Download
4.56 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
35.30 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
5.73 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
4.03 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
10.92 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
12.31 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
4.11 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
2.51 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
4.19 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
3.61 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.94 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
7.07 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
7.60 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
2.98 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
2.36 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
3.90 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.23 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1013 B lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.23 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.07 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.90 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.99 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
731 B lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.52 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.26 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
12.06 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
532 B lrw-r--r-- 2025-06-16 05:45:40
Edit Download
906 B lrw-r--r-- 2025-06-16 05:45:40
Edit Download
3.84 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
746 B lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.80 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
573 B lrw-r--r-- 2025-06-16 05:45:40
Edit Download
536 B lrw-r--r-- 2025-06-16 05:45:40
Edit Download
6.07 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.77 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.70 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
4.20 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.34 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
6.04 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
578 B lrw-r--r-- 2025-06-16 05:45:40
Edit Download
579 B lrw-r--r-- 2025-06-16 05:45:40
Edit Download
5.96 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
2.55 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
1.76 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
48.70 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
20.62 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
10.64 KB lrw-r--r-- 2025-06-16 05:45:40
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).