Repository: benhowdle89/deSVG
Branch: gh-pages
Commit: 732a3a22cbec
Files: 6
Total size: 8.8 KB
Directory structure:
gitextract_zkvoy4fk/
├── README.md
├── desvg.js
├── index.html
├── mob.css
├── package.json
└── style.css
================================================
FILE CONTENTS
================================================
================================================
FILE: README.md
================================================
deSVG
=====
### What does it do?
deSVG takes the `` tags you supply. It then grabs, using AJAX, the raw SVG you've set in the `src` attribute and replaces that `
` with the `` it downloads.
### Install
Grab the files from this repo, or install from [npm](https://www.npmjs.com/package/desvg)
### Usage
window.addEventListener('load', function(){
// 1. selector for the
tags to replace
// 2. whether to strip inline style tags from SVG paths
deSVG('.replace-svg', true);
});
If you do not wish for two network requests to be made per image, instead of using the `src` attribute, you can pass in the images path via `data-src="path/to/file"`. Note that this may cause layouts to jump and no image to be displayed whilst it is being fetched.
### Demo
Check out this [JSFiddle](http://jsfiddle.net/benhowdle89/ujxomdgc/14/).
### Source
Check out the [commented source](https://github.com/benhowdle89/deSVG/blob/gh-pages/desvg.js) which should explain it's functionality in greater detail.
================================================
FILE: desvg.js
================================================
(function() {
"use strict";
var desvg = function(selector, removeInlineCss) {
removeInlineCss = removeInlineCss || false;
var images,
imagesLength,
sortImages = {},
// load svg file
loadSvg = function (imgURL, replaceImages) {
// set up the AJAX request
var xhr = new XMLHttpRequest();
xhr.open('GET', imgURL, true);
xhr.onload = function() {
var xml,
svg,
paths,
replaceImagesLength;
// get the response in XML format
xml = xhr.responseXML;
replaceImagesLength = replaceImages.length;
// bail if no XML
if (!xml) {
return;
}
// this will be the
svg = xml.documentElement;
// get all the SVG paths
paths = svg.querySelectorAll('path');
if (removeInlineCss) {
// if `removeInlineCss` is true then remove the style attributes from the SVG paths
for (var i = 0; i < paths.length; i++) {
paths[i].removeAttribute('style');
}
}
svg.removeAttribute('xmlns:a');
while(replaceImagesLength--) {
replaceImgWithSvg(replaceImages[replaceImagesLength], svg.cloneNode(true));
}
};
xhr.send();
},
// replace the original
with the new
replaceImgWithSvg = function (img, svg) {
var imgID = img.id,
imgClasses = img.getAttribute('class');
if (imgID) {
// re-assign the ID attribute from the
svg.id = imgID;
}
if (imgClasses) {
// re-assign the class attribute from the
svg.setAttribute('class', imgClasses + ' replaced-svg');
}
img.parentNode.replaceChild(svg, img);
};
// grab all the elements from the document matching the passed in selector
images = document.querySelectorAll(selector);
imagesLength = images.length;
// sort images array by image url
while (imagesLength--) {
var _img = images[imagesLength],
_imgURL;
if (_img.getAttribute('data-src')) {
_imgURL = _img.getAttribute('data-src')
} else {
_imgURL = _img.getAttribute('src')
}
if (sortImages[_imgURL]) {
sortImages[_imgURL].push(_img);
} else {
sortImages[_imgURL] = [_img];
}
}
// loops over the matched urls
for (var key in sortImages) {
if (sortImages.hasOwnProperty(key)) {
loadSvg(key, sortImages[key]);
}
}
};
window.deSVG = desvg;
})();
================================================
FILE: index.html
================================================
deSVG takes the <img /> tags you supply. It then grabs, using AJAX, the raw SVG you've set in the "src" attribute and replaces that <img /> with the <svg /> it downloads.
<script src="path/to/desvg.js"></script>
window.addEventListener('load', function(){
// 1. selector for the <img /> tags to replace
// 2. whether to strip inline style tags from SVG paths
deSVG('.replace-svg', true);
});
Using this library will cause duplicate HTTP requests (one for initial image src download and then a subsequent one for AJAX-ing the SVG data), but development is full of trade-offs, so it's your call on the pros and cons of using this library. Sometimes it's a case of choosing between micro-optimisations vs. a sane development workflow. I trust your judgement.