54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
// Shared QR SVG helpers for browser renderers.
|
|
|
|
(function () {
|
|
function normalizeSvgMarkup(svg) {
|
|
var markup = String(svg || '').trim();
|
|
if (!markup) {
|
|
return '';
|
|
}
|
|
|
|
return markup.replace(/^<\?xml[^>]*>\s*/i, '');
|
|
}
|
|
|
|
function stripBackground(svg) {
|
|
var markup = String(svg || '').trim();
|
|
if (!markup) {
|
|
return '';
|
|
}
|
|
|
|
return markup
|
|
.replace(/<path\b[^>]*fill="#ffffff"[^>]*d="M0 0h[^\"]*"[^>]*\s*\/>/i, '')
|
|
.replace(/<path\b[^>]*fill="#ffffff"[^>]*d="M0 0h[^\"]*"[^>]*><\/path>/i, '')
|
|
.replace(/<rect\b[^>]*fill="#ffffff"[^>]*x="0"[^>]*y="0"[^>]*width="\d+"[^>]*height="\d+"[^>]*\s*\/?>(?:<\/rect>)?/i, '');
|
|
}
|
|
|
|
function normalizePreviewSvg(svg) {
|
|
var markup = normalizeSvgMarkup(stripBackground(svg));
|
|
if (!markup || markup.charAt(0) !== '<') {
|
|
return markup;
|
|
}
|
|
|
|
return markup.replace(/^<svg\b([^>]*)>/i, function (_match, attrText) {
|
|
var attrs = String(attrText || '');
|
|
if (!/\bwidth\s*=\s*/i.test(attrs)) {
|
|
attrs += ' width="95%"';
|
|
}
|
|
if (!/\bheight\s*=\s*/i.test(attrs)) {
|
|
attrs += ' height="95%"';
|
|
}
|
|
if (!/\bpreserveAspectRatio\s*=\s*/i.test(attrs)) {
|
|
attrs += ' preserveAspectRatio="xMidYMid meet"';
|
|
}
|
|
if (!/\bstyle\s*=\s*/i.test(attrs)) {
|
|
attrs += ' style="display:block;width:95%;height:95%;"';
|
|
}
|
|
return '<svg' + attrs + '>';
|
|
});
|
|
}
|
|
|
|
window.pulseQrSvgUtils = {
|
|
normalizeSvgMarkup: normalizeSvgMarkup,
|
|
stripBackground: stripBackground,
|
|
normalizePreviewSvg: normalizePreviewSvg
|
|
};
|
|
}()); |