UNPKG

@sutton-signwriting/sgnw-components

Version:

a javascript package of web components for use with the SignWriting script.

34 lines (31 loc) 1.14 kB
/*! * The Sutton SignWriting Web Components */ 'use strict'; const addSvgStyle = function (svg, style) { if (!svg) return svg; return svg.replace(/<svg\b([^>]*)>/i, (match, attrs) => { const styleMatch = attrs.match(/\sstyle=(["'])(.*?)\1/i); if (styleMatch) { return match.replace(styleMatch[0], ` style=${styleMatch[1]}${style};${styleMatch[2]}${styleMatch[1]}`); } return `<svg${attrs} style="${style}">`; }); }; const addSvgClassStyle = function (svg, className, style) { if (!svg) return svg; return svg.replace(/<[^/!][^>]*\bclass=(["'])([^"']*)\1[^>]*>/gi, (match, _quote, classes) => { if (!classes.split(/\s+/).includes(className)) { return match; } const styleMatch = match.match(/\sstyle=(["'])(.*?)\1/i); if (styleMatch) { return match.replace(styleMatch[0], ` style=${styleMatch[1]}${styleMatch[2]};${style}${styleMatch[1]}`); } return match.replace(/>$/, ` style="${style}">`); }); }; exports.addSvgClassStyle = addSvgClassStyle; exports.addSvgStyle = addSvgStyle;