Skip to content

Commit

Permalink
handle leading and trailing whitespace and comments in SVG files
Browse files Browse the repository at this point in the history
  • Loading branch information
dasboe committed Aug 16, 2018
1 parent 4af0508 commit 19f68ee
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 463 deletions.
9 changes: 5 additions & 4 deletions dist/svg-inject.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* SVGInject - Version 1.0.0-rc.5
* SVGInject - Version 1.0.0-rc.7
* A tiny, intuitive, robust, caching solution for injecting SVG files inline into the DOM.
*
* https://github.com/iconfu/svg-inject
Expand Down Expand Up @@ -69,7 +69,7 @@
var status = req.status;
if (status == 200) {
// readyState is done, request status ok
callback(req.responseXML, req.responseText);
callback(req.responseXML, req.responseText.trim());
} else if (status >= 400) {
errorCallback();
} else if (status == 0) {
Expand Down Expand Up @@ -169,7 +169,7 @@

// inject svg by replacing the img element with the svg element in the DOM
function inject(img, svg, svgString, absUrl, options) {
svg = svg || buildSvg(svgString, absUrl);
svg = svg || buildSvg(svgString, absUrl);

if (svg) {
var parentNode = img.parentNode;
Expand Down Expand Up @@ -239,7 +239,8 @@
} catch (e) {
return NULL;
}
var svg = DIV_ELEMENT.removeChild(DIV_ELEMENT.firstChild);
var svg = DIV_ELEMENT.firstElementChild;
DIV_ELEMENT.innerHTML = '';

if (isSVGElem(svg)) {
svg.setAttribute('data-inject-url', absUrl);
Expand Down
2 changes: 1 addition & 1 deletion dist/svg-inject.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions examples/svg-inject.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* SVGInject - Version 1.0.0-rc.5
* SVGInject - Version 1.0.0-rc.7
* A tiny, intuitive, robust, caching solution for injecting SVG files inline into the DOM.
*
* https://github.com/iconfu/svg-inject
Expand Down Expand Up @@ -69,7 +69,7 @@
var status = req.status;
if (status == 200) {
// readyState is done, request status ok
callback(req.responseXML, req.responseText);
callback(req.responseXML, req.responseText.trim());
} else if (status >= 400) {
errorCallback();
} else if (status == 0) {
Expand Down Expand Up @@ -169,7 +169,7 @@

// inject svg by replacing the img element with the svg element in the DOM
function inject(img, svg, svgString, absUrl, options) {
svg = svg || buildSvg(svgString, absUrl);
svg = svg || buildSvg(svgString, absUrl);

if (svg) {
var parentNode = img.parentNode;
Expand Down Expand Up @@ -239,7 +239,8 @@
} catch (e) {
return NULL;
}
var svg = DIV_ELEMENT.removeChild(DIV_ELEMENT.firstChild);
var svg = DIV_ELEMENT.firstElementChild;
DIV_ELEMENT.innerHTML = '';

if (isSVGElem(svg)) {
svg.setAttribute('data-inject-url', absUrl);
Expand Down
2 changes: 1 addition & 1 deletion examples/svg-inject.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ gulp.task('default', function (cb) {
pump([
gulp.src('src/svg-inject.js'),
gulp.dest('dist/'),
gulp.dest('test/js/'),
gulp.dest('examples/'),
uglify({
output: {
Expand All @@ -19,6 +18,7 @@ gulp.task('default', function (cb) {
}),
rename({ extname: '.min.js' }),
gulp.dest('examples/'),
gulp.dest('test/js/'),
gulp.dest('dist/')
], cb);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iconfu/svg-inject",
"version": "1.0.0-rc.6",
"version": "1.0.0-rc.7",
"description": "simple bulletproof svg injector",
"main": "dist/svg-inject.js",
"repository": {
Expand Down
9 changes: 5 additions & 4 deletions src/svg-inject.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* SVGInject - Version 1.0.0-rc.6
* SVGInject - Version 1.0.0-rc.7
* A tiny, intuitive, robust, caching solution for injecting SVG files inline into the DOM.
*
* https://github.com/iconfu/svg-inject
Expand Down Expand Up @@ -69,7 +69,7 @@
var status = req.status;
if (status == 200) {
// readyState is done, request status ok
callback(req.responseXML, req.responseText);
callback(req.responseXML, req.responseText.trim());
} else if (status >= 400) {
errorCallback();
} else if (status == 0) {
Expand Down Expand Up @@ -169,7 +169,7 @@

// inject svg by replacing the img element with the svg element in the DOM
function inject(img, svg, svgString, absUrl, options) {
svg = svg || buildSvg(svgString, absUrl);
svg = svg || buildSvg(svgString, absUrl);

if (svg) {
var parentNode = img.parentNode;
Expand Down Expand Up @@ -239,7 +239,8 @@
} catch (e) {
return NULL;
}
var svg = DIV_ELEMENT.removeChild(DIV_ELEMENT.firstChild);
var svg = DIV_ELEMENT.firstElementChild;
DIV_ELEMENT.innerHTML = '';

if (isSVGElem(svg)) {
svg.setAttribute('data-inject-url', absUrl);
Expand Down
7 changes: 6 additions & 1 deletion test/imgs/test3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 19f68ee

Please sign in to comment.