Skip to content

Commit

Permalink
Added XML entity resolution (closes svg-sprite#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkphl committed May 14, 2016
1 parent da02456 commit 1b8eec6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/svg-sprite/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,36 @@ SVGShape.prototype.setSVG = function(svg) {
SVGShape.prototype._initSVG = function() {

// Basic check for basic SVG file structure
if (!this.svg.current.match(/<svg(?:\s+[a-z0-9-]+=("|').*?\1)*\s*(\/)|(>[^]*<\/svg)>/i)) {
var svgStart = this.svg.current.match(/<svg(?:\s+[a-z0-9-\:]+=("|').*?\1)*\s*(?:(\/)|(>[^]*<\/svg))>/i);
if (!svgStart) {
var e = new Error('Invalid SVG file');
e.name = 'ArgumentError';
e.errno = 1429395394;
throw e;
}


// Resolve XML entities
var entityRegExp = /<!ENTITY\s+([^\s]+)\s+("|')(.+)(?:\2)>/i;
var entityStart = 0;
var entities = 0;
var entityMap = {};
var entity;
do {
entity = entityRegExp.exec(this.svg.current.substr(entityStart));
if (entity) {
++entities;
entityStart += entity.index + entity[0].length;
entityMap[entity[1]] = entity[3];
}
} while(entity);
if (entities) {
var svg = this.svg.current.substr(svgStart.index);
for (entity in entityMap) {
svg = svg.replace('&' + entity + ';', entityMap[entity]);
}
this.svg.current = this.svg.current.substr(0, svgStart.index) + svg;
}

// Parse the XML
this.dom = new DOMParser({
locator : {},
Expand Down
12 changes: 12 additions & 0 deletions test/fixture/svg/special/with-entity.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1b8eec6

Please sign in to comment.