svg-sprite-loader
Advanced tools
Comparing version 0.0.5 to 0.0.6
var Sprite = require('./sprite'); | ||
var globalSprite = new Sprite(); | ||
document.addEventListener('DOMContentLoaded', function () { | ||
if (document.body) { | ||
globalSprite.elem = globalSprite.render(document.body); | ||
}, false); | ||
} else { | ||
document.addEventListener('DOMContentLoaded', function () { | ||
globalSprite.elem = globalSprite.render(document.body); | ||
}, false); | ||
} | ||
module.exports = globalSprite; | ||
module.exports = globalSprite; |
@@ -13,2 +13,6 @@ /** | ||
var svgOpening = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink"'; | ||
var svgClosing = '</svg>'; | ||
var contentPlaceHolder = '{content}'; | ||
/** | ||
@@ -24,3 +28,4 @@ * Representation of SVG sprite | ||
Sprite.template = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink" style="'+ Sprite.styles.join(';') +'"><defs>{content}</defs></svg>'; | ||
Sprite.spriteTemplate = svgOpening + ' style="'+ Sprite.styles.join(';') +'"><defs>' + contentPlaceHolder + '</defs>' + svgClosing; | ||
Sprite.symbolTemplate = svgOpening + '>' + contentPlaceHolder + svgClosing; | ||
@@ -36,5 +41,21 @@ /** | ||
Sprite.prototype.add = function (content) { | ||
if (this.svg) { | ||
this.appendSymbol(content); | ||
} | ||
this.content.push(content); | ||
}; | ||
Sprite.prototype.wrapSVG = function (content, template) { | ||
var svgString = template.replace(contentPlaceHolder, content); | ||
return new DOMParser().parseFromString(svgString, 'image/svg+xml').documentElement; | ||
}; | ||
Sprite.prototype.appendSymbol = function (content) { | ||
var symbol = this.wrapSVG(content, Sprite.symbolTemplate).childNodes[0]; | ||
this.svg.querySelector('defs').appendChild(symbol); | ||
}; | ||
/** | ||
@@ -55,7 +76,6 @@ * @returns {String} | ||
Sprite.prototype.render = function (target, prepend) { | ||
var target = target || null; | ||
var prepend = typeof prepend === 'boolean' ? prepend : true; | ||
target = target || null; | ||
prepend = typeof prepend === 'boolean' ? prepend : true; | ||
var svgString = Sprite.template.replace('{content}', this.content.join('')); | ||
var svg = new DOMParser().parseFromString(svgString, 'image/svg+xml').documentElement; | ||
var svg = this.wrapSVG(this.content.join(''), Sprite.spriteTemplate); | ||
@@ -68,10 +88,14 @@ // Because of Firefox bug #353575 gradients and patterns don't work if they are within a symbol. | ||
if (target) { | ||
prepend | ||
? target.insertBefore(svg, target.childNodes[0]) | ||
: target.appendChild(svg); | ||
if (prepend && target.childNodes[0]) { | ||
target.insertBefore(svg, target.childNodes[0]); | ||
} else { | ||
target.appendChild(svg); | ||
} | ||
} | ||
this.svg = svg; | ||
return svg; | ||
}; | ||
module.exports = Sprite; | ||
module.exports = Sprite; |
{ | ||
"name": "svg-sprite-loader", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "SVG sprite webpack loader", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
14790
284