CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
parse.js68 linesDownload Raw Back to svg
1import { wrapSVGContent } from "./defs.js";2import { getSVGViewBox } from "./viewbox.js";3/**4* Extract attributes and content from SVG5*/6function parseSVGContent(content) {7	const match = content.trim().match(/(?:<(?:\?xml|!DOCTYPE)[^>]+>\s*)*<svg([^>]+)>([\s\S]+)<\/svg[^>]*>/);8	if (!match) return;9	const body = match[2].trim();10	const attribsList = match[1].match(/[\w:-]+="[^"]*"/g);11	const attribs = Object.create(null);12	attribsList?.forEach((row) => {13		const match = row.match(/([\w:-]+)="([^"]*)"/);14		if (match) attribs[match[1]] = match[2];15	});16	return {17		attribs,18		body19	};20}21function build(data) {22	const attribs = data.attribs;23	const viewBox = getSVGViewBox(attribs["viewBox"] ?? "");24	if (!viewBox) return;25	const groupAttributes = [];26	for (const key in attribs) if (key === "style" || key.startsWith("fill") || key.startsWith("stroke")) groupAttributes.push(`${key}="${attribs[key]}"`);27	let body = data.body;28	if (groupAttributes.length) body = wrapSVGContent(body, "<g " + groupAttributes.join(" ") + ">", "</g>");29	return {30		width: attribs.width,31		height: attribs.height,32		viewBox,33		body34	};35}36/**37* Convert parsed SVG to IconifyIconBuildResult38*/39function buildParsedSVG(data) {40	const result = build(data);41	if (result) return {42		attributes: {43			width: result.width,44			height: result.height,45			viewBox: result.viewBox.join(" ")46		},47		viewBox: result.viewBox,48		body: result.body49	};50}51/**52* Convert parsed SVG to IconifyIcon53*/54function convertParsedSVG(data) {55	const result = build(data);56	if (result) {57		const viewBox = result.viewBox;58		return {59			left: viewBox[0],60			top: viewBox[1],61			width: viewBox[2],62			height: viewBox[3],63			body: result.body64		};65	}66}67export { buildParsedSVG, convertParsedSVG, parseSVGContent };68 
basant307/AI_Governance_Project · CoolFace