Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,34 @@ module.exports = function vueSvgLoader(svg) {
svg = svg.replace('<svg', '<svg v-on="$listeners"');
}

return `<template>${svg}</template>`;
const extracted = extractStyle(svg);
svg = extracted.svg;
const styles = extracted.styles;

return `<template>${svg}</template><style scoped>${styles.join('\n')}</style>`;
};

/**
* Extract all <style> content from SVG content.
* It also returns svg content from which all <style> elements are removed.
*/
function extractStyle(svg) {
const styleRE = /<style[^>]*>([\s\S]*?)<\/style>/ig;
let styles = [];
let match;

while (match = styleRE.exec(svg)) {
const style = match[1];
styles.push(style);

const from = match.index;
const to = from + match[0].length;
svg = svg.slice(0, from) + svg.slice(to);
styleRE.lastIndex -= match[0].length;
}

return {
svg,
styles
};
}