Current Dev State

This commit is contained in:
Tim Lorsbach
2025-06-23 20:13:54 +02:00
parent b4f9bb277d
commit ded50edaa2
22617 changed files with 4345095 additions and 174 deletions

View File

@ -0,0 +1,91 @@
'use strict';
var assign = require('object-assign');
var copyAttributes = require('./utils/copy-attributes');
var loadXml = require('./utils/load-xml');
var removeAttributes = require('./utils/remove-attributes');
var setAttributes = require('./utils/set-attributes');
var svgToSymbol = require('./utils/svg-to-symbol');
var SELECTOR_SVG = 'svg';
var SELECTOR_DEFS = 'defs';
var TEMPLATE_SVG = '<svg><defs/></svg>';
var TEMPLATE_DOCTYPE = '<?xml version="1.0" encoding="UTF-8"?>' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ' +
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
var DEFAULT_OPTIONS = {
cleanDefs: false,
cleanSymbols: false,
inline: false,
svgAttrs: false,
symbolAttrs: false,
copyAttrs: false
};
function svgstore(options) {
var svgstoreOptions = assign({}, DEFAULT_OPTIONS, options);
// <svg>
var parent = loadXml(TEMPLATE_SVG);
var parentSvg = parent(SELECTOR_SVG);
var parentDefs = parent(SELECTOR_DEFS);
return {
element: parent,
add: function (id, file, options) {
var child = loadXml(file);
var addOptions = assign({}, svgstoreOptions, options);
// <defs>
var childDefs = child(SELECTOR_DEFS);
removeAttributes(childDefs, addOptions.cleanDefs);
parentDefs.append(childDefs.contents());
childDefs.remove();
// <symbol>
var childSvg = child(SELECTOR_SVG);
var childSymbol = svgToSymbol(id, child, addOptions);
removeAttributes(childSymbol, addOptions.cleanSymbols);
copyAttributes(childSymbol, childSvg, addOptions.copyAttrs);
setAttributes(childSymbol, addOptions.symbolAttrs);
parentSvg.append(childSymbol);
return this;
},
toString: function (options) {
// Create a clone so we don't modify the parent document.
var clone = loadXml(parent.xml());
var toStringOptions = assign({}, svgstoreOptions, options);
// <svg>
var svg = clone(SELECTOR_SVG);
setAttributes(svg, toStringOptions.svgAttrs);
// output inline
if (toStringOptions.inline) {
return clone.xml();
}
// output standalone
svg.attr('xmlns', function (val) {
return val || 'http://www.w3.org/2000/svg';
});
svg.attr('xmlns:xlink', function (val) {
return val || 'http://www.w3.org/1999/xlink';
});
return TEMPLATE_DOCTYPE + clone.xml();
}
};
}
module.exports = svgstore;

View File

@ -0,0 +1,34 @@
/**
* Utility to copy specific attributes from one node to another.
*/
'use strict';
var ALWAYS_COPY_ATTRS = [
'viewBox',
'aria-labelledby',
'role',
];
function copyAttributes(a, b, attrs) {
var attrsToCopy = ALWAYS_COPY_ATTRS.concat(attrs || []);
var copiedAttrs = Object.create(null);
attrsToCopy.forEach(function (attr) {
if (!attr || copiedAttrs[attr]) {
return;
}
copiedAttrs[attr] = true;
var bAttr = b.attr(attr);
if (bAttr != null) {
a.attr(attr, b.attr(attr));
}
});
return a;
}
module.exports = copyAttributes;

View File

@ -0,0 +1,16 @@
/**
* Utility method to create an XML document object with a jQuery-like
* interface for node manipulation.
*/
'use strict';
var cheerio = require('cheerio');
function loadXml(text) {
return cheerio.load(text, {
xmlMode: true
});
}
module.exports = loadXml;

View File

@ -0,0 +1,30 @@
/**
* Utility to remove specific attributes from all
* child nodes of a given node.
*/
'use strict';
function removeAttributes(el, attrs) {
var localAttrs = attrs;
if (localAttrs === true) {
localAttrs = ['style'];
}
if (!localAttrs || !localAttrs.length) {
return el;
}
var els = el.find('*');
els.each(function (i, el) {
localAttrs.forEach(function (attr) {
els.eq(i).removeAttr(attr);
});
});
return el;
}
module.exports = removeAttributes;

View File

@ -0,0 +1,28 @@
/**
* Utility function to set the attributes of an element. Allows values to be
* passed as functions so existing values may be manipulated or left untouched.
*/
'use strict';
function setAttributes(el, attrs) {
if (!attrs || typeof attrs !== 'object') {
return el;
}
Object.keys(attrs).forEach(function (attr) {
var value = attrs[attr];
// Handle function values directly as cherrio passes an unhelpful index
// as the first argument in the native function handler.
if (typeof value === 'function') {
value = value(el.attr(attr));
}
el.attr(attr, value);
});
return el;
}
module.exports = setAttributes;

View File

@ -0,0 +1,31 @@
/**
* Utility for cloning an <svg/> as a <symbol/> within
* the composition of svgstore output.
*/
'use strict';
var SELECTOR_SVG = 'svg';
var TEMPLATE_SYMBOL = '<symbol/>';
var ATTRIBUTE_ID = 'id';
/**
* @param {string} id The id to be applied to the symbol tag
* @param {string} child An object created by loading the content of the current file via the cheerio#load function.
* @param {object} options for parsing the svg content
* @return {object} symbol The final cheerio-aware object created by cloning the SVG contents
* @see <a href="https://github.com/cheeriojs/cheerio">The Cheerio Project</a>
*/
function svgToSymbol(id, child, options) {
var svgElem = child(SELECTOR_SVG);
// initialize a new <symbol> element
var symbol = child(TEMPLATE_SYMBOL);
symbol.attr(ATTRIBUTE_ID, id);
symbol.append(svgElem.contents());
return symbol;
}
module.exports = svgToSymbol;