Merge pull request #213 from paulkaplan/strip-svg-namespace

Remove "svg:" namespace from svg before importing.
This commit is contained in:
Paul Kaplan 2017-12-12 10:55:53 -05:00 committed by GitHub
commit 6a7daa83a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -82,6 +82,19 @@ class PaperCanvas extends React.Component {
} }
importSvg (svg, rotationCenterX, rotationCenterY) { importSvg (svg, rotationCenterX, rotationCenterY) {
const paperCanvas = this; const paperCanvas = this;
// Pre-process SVG to prevent parsing errors (discussion from #213)
// 1. Remove newlines and tab characters, chrome will not load urls with them.
// https://www.chromestatus.com/feature/5735596811091968
svg = svg.split(/[\n|\r|\t]/).join('');
// 2. Remove svg: namespace on elements.
svg = svg.split(/<\s*svg:/).join('<');
svg = svg.split(/<\/\s*svg:/).join('</');
// 3. Add root svg namespace if it does not exist.
const svgAttrs = svg.match(/<svg [^>]*>/);
if (svgAttrs && svgAttrs[0].indexOf('xmlns=') === -1) {
svg = svg.replace(
'<svg ', '<svg xmlns="http://www.w3.org/2000/svg" ');
}
paper.project.importSVG(svg, { paper.project.importSVG(svg, {
expandShapes: true, expandShapes: true,
onLoad: function (item) { onLoad: function (item) {