Skip to content

Commit

Permalink
slurp entire SVG, uses DOMParser instead of innerHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinerg committed Oct 2, 2018
1 parent a4d3948 commit 61d1c7b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
11 changes: 6 additions & 5 deletions src/components/modebar/modebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var isNumeric = require('fast-isnumeric');

var Lib = require('../../lib');
var Icons = require('../../../build/ploticon');

var Parser = new DOMParser();

/**
* UI controller for interactive plots
Expand Down Expand Up @@ -192,8 +192,6 @@ proto.createIcon = function(thisIcon) {

if(thisIcon.path) {
icon = document.createElementNS(svgNS, 'svg');
icon.setAttribute('height', '1em');
icon.setAttribute('width', (thisIcon.width / iconHeight) + 'em');
icon.setAttribute('viewBox', [0, 0, thisIcon.width, iconHeight].join(' '));

var path = document.createElementNS(svgNS, 'path');
Expand All @@ -215,10 +213,13 @@ proto.createIcon = function(thisIcon) {
}

if(thisIcon.svg) {
icon = document.createElement('div');
icon.innerHTML = '<svg height="1em" width="' + (thisIcon.width / iconHeight) + 'em" viewbox="0, 0, ' + thisIcon.width + ',' + thisIcon.height + '" xmlns="">' + thisIcon.svg + '</svg>';
var svgDoc = Parser.parseFromString(thisIcon.svg, 'application/xml');
icon = svgDoc.childNodes[0];
}

icon.setAttribute('height', '1em');
icon.setAttribute('width', '1em');

return icon;
};

Expand Down
44 changes: 31 additions & 13 deletions src/fonts/ploticon/ploticon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 6 additions & 10 deletions tasks/util/pull_font_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var fs = require('fs');
var xml2js = require('xml2js');

var parser = new xml2js.Parser();
var builder = new xml2js.Builder({ headless: true, rootName: 'g', renderOpts: {'newline': ''}});
var builder = new xml2js.Builder({ headless: true, rootName: 'svg', renderOpts: {'newline': ''}});

module.exports = function pullFontSVG(data, pathOut) {
parser.parseString(data, function(err, result) {
Expand All @@ -29,17 +29,13 @@ module.exports = function pullFontSVG(data, pathOut) {
});

// Load SVG
var svgs = result.svg.defs[0].g;
svgs.forEach(function(g) {
var name = g.$.id,
width = parseFloat(g.$['data-width']),
height = parseFloat(g.$['data-height']);
delete g.$;
var svgs = result.svg.defs[0].svg;
svgs.forEach(function(svg) {
var name = svg.$.id;
delete svg.$.id;
chars[name] = {
name: name,
width: width,
height: height,
svg: builder.buildObject(g)
svg: builder.buildObject(svg)
};
});

Expand Down

0 comments on commit 61d1c7b

Please sign in to comment.