svg-inline-loader
Advanced tools
Comparing version 0.2.2 to 0.2.3
# Changelog | ||
## 0.2.3 | ||
* Tag removal is fixed (`<defs />`, `<style />`, `<title />`, ...) and test | ||
added | ||
## 0.2.2 | ||
* Added unittest | ||
## 0.2.1 | ||
@@ -4,0 +13,0 @@ |
46
index.js
@@ -20,7 +20,2 @@ var simpleHTMLTokenizer = require('simple-html-tokenizer'); | ||
// Non-displayed elements | ||
[/<title>.*<\/title>/gi, ""], | ||
[/<desc>.*<\/desc>/gi, ""], | ||
[/<defs>.*<\/defs>/gi, ""], | ||
// SVG XML -> HTML5 | ||
@@ -32,2 +27,13 @@ [/\<([A-Za-z]+)([^\>]*)\/\>/g, "<$1$2></$1>"], // convert self-closing XML SVG nodes to explicitly closed HTML5 SVG nodes | ||
var removingTags = [ | ||
'title', | ||
'desc', | ||
'defs', | ||
'style' | ||
]; | ||
function isRemovingTag (tag) { | ||
return removingTags.indexOf(tag.tagName) > -1; | ||
} | ||
function getExtractedSVG (svgStr) { | ||
@@ -49,11 +55,27 @@ // Clean-up XML crusts like comments and doctype, etc. | ||
// FIXME: Due to limtation of parser, we need to implement our | ||
// very own little state machine to express tree structure | ||
var removingTag = null; | ||
// If the token is <svg> start-tag, then remove width and height attributes. | ||
tokens.forEach(function(tag) { | ||
if (isSVGToken(tag)) { | ||
tag.attributes = tag.attributes.filter(hasNoWidthHeight); | ||
return generate(tokens.map(function(tag) { | ||
if (removingTag == null) { | ||
// Reached start tag of a removing tag | ||
if (isRemovingTag(tag)) { | ||
removingTag = tag.tagName; | ||
tag = null; | ||
// Other stuffs that needs to be modified | ||
} else if (isSVGToken(tag)) { | ||
tag.attributes = tag.attributes.filter(hasNoWidthHeight); | ||
} | ||
} else { | ||
// Reached end tag of a removing tag | ||
if (tag.tagName === removingTag && tag.type === 'EndTag') { | ||
removingTag = null; | ||
} | ||
tag = null; | ||
} | ||
}); | ||
// Finally, assemble tokens | ||
return generate(tokens); | ||
return tag; | ||
}) | ||
.filter(function (nonNull) { return nonNull; })); | ||
} | ||
@@ -60,0 +82,0 @@ |
{ | ||
"name": "svg-inline-loader", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "Cleans up and inlines your SVG files into Webpack module.", | ||
@@ -35,4 +35,5 @@ "main": "index.js", | ||
"karma-webpack": "^1.5.1", | ||
"node-libs-browser": "^0.5.2" | ||
"node-libs-browser": "^0.5.2", | ||
"raw-loader": "^0.5.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5350
8
5
78