iconic / svginjector Goto Github PK
View Code? Open in Web Editor NEWFast, caching, dynamic inline SVG DOM injection library
License: MIT License
Fast, caching, dynamic inline SVG DOM injection library
License: MIT License
This happens because of the ID reassignment: elements have their IDs changed (e.g. <marker id="Arrow1Lend">
becomes <marker id="Arrow1Lend-0">
) but the style declarations are not updated to match (e.g. <path style="marker-start:url(#Arrow1Lend)">
will remain unchanged).
You can see below a simple file that exhibits this problem:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
width="128"
height="128"
id="svg8338">
<defs
id="defs8340">
<marker
refX="0"
refY="0"
orient="auto"
id="Arrow1Lend"
style="overflow:visible">
<path
d="M 0,0 5,-5 -12.5,0 5,5 0,0 z"
transform="matrix(-0.8,0,0,-0.8,-10,0)"
id="path3795"
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;marker-start:none" />
</marker>
<marker
refX="0"
refY="0"
orient="auto"
id="DotL"
style="overflow:visible">
<path
d="m -2.5,-1 c 0,2.76 -2.24,5 -5,5 -2.76,0 -5,-2.24 -5,-5 0,-2.76 2.24,-5 5,-5 2.76,0 5,2.24 5,5 z"
transform="matrix(0.8,0,0,0.8,5.92,0.8)"
id="path3854"
style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt;marker-start:none;marker-end:none" />
</marker>
</defs>
<metadata
id="metadata8343">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
transform="translate(0,-924.3622)"
id="layer1">
<path
d="m 14.285713,989.50504 99.999997,0"
id="path8346"
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;marker-start:url(#DotL);marker-end:url(#Arrow1Lend)" />
</g>
</svg>
The index of the injected svg seems to be inserted after id's, which is breaking url references
eg:
fill: url(#green)
image.svg:
<defs>
<linearGradient id="green">
...
</linearGradient>
</defs>
But when injected:
<defs>
<linearGradient id="green-0">
...
</linearGradient>
</defs>
Hi,
Hi, this excellent plugin works almost perfectly in my project, I'm only having problems to use SVGInjector in IE9.
My SVG files are hosted in Amazon S3 and the IE9 console displays this error:
SCRIPT5: Access is denied. (line 173)
The problem is that IE8 and IE9 don't support standard XMLHttpRequest (line 110).
I think that the problem can be solved using XDomainRequest for IE8 and IE9, based on this article: http://www.leggetter.co.uk/2010/03/12/making-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html
Thank you very much!
There is a consistent, noticeable delay in the appearance of icons loaded with the injector. If it can't be fixed, I (and others who find this to be a bad user experience) wouldn't use SVGs. Can you provide guidance in the docs on how to remove this delay? Perhaps there is a way to include the SVGs in the initial page load, or initially fill the space with the icon font, and then replace it seamlessly with the ajax SVG? Thanks!
Hey guys,
Could you please publish this to npm? you already have the package.json setup, so it should be no problem.
😄
it would be good, if the injector could transclude the title attribute to the SVG, if possible (tho, some more work could be involved to properly implement that)
currently, the title just remains on SVG, but according to the SVG 1.1 specs, it would be better to use a <title>
or <desc>
element
WDYT?
Hello,
Like this exemple : http://jsfiddle.net/24DNn/5/
It's possible to stack a lot of svg inside an other svg, like a sprite.
Nevertheless, SVGInjector doesn't work if we add a target "#IDofMySvg" at the end of the URL.
exemple: " img class="inject-svg" src="/images/sprite.svg#logo" "
My project have more than 150 svg and I really need to use Stack.
Have you an idea ?
Thanks !
Is there anything similar to this to pre-process the html and replace the img tags with embedded svg?
I want to apply another jQuery plugin to all the inlined SVGs using the final callback rather than the 'each' callback, but at the point when the callback is called, the <svg>
tags are not yet actually present in the DOM (or in other words the SVGs are not yet inlined).
This is what my callback looks like:
And this is the DOM at the moment the breakpoint occurs - note these are <img>
tags:
How do you dynamically change the direction of iconic with the svginjector? Since the svginjector removes the <img>
element what are you supposed to do to update the direction of the svg? Do you have to completely remove that node from dom and try to re-render it or?
I´m having some troubles with IE11, some svg's appear all black, while others don´t.
The main difference is that the one that don´t appear have id's and are more complex.
The simple one only have one class. They were made in illustrator, and they load well without the svg injector.
Here goes a bit of the svg code:
svg version="1.1" id="formacao_profissional" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 360 235" xml:space="preserve">
<defs>
<style>
.formacao_profissional .st0{fill:#AFD0EF;}
.formacao_profissional .st1{fill:#FFFFFF;stroke:#009BDE;stroke-width:0.5;stroke-miterlimit:10;}
.formacao_profissional .st2{fill:#B5D5F1;}
#arm-down{transition: all .2s ease-in-out;}
#arm-up{opacity:0; fill:#003349; transition: all .2s ease-in-out;}
.formacao_profissional .st4{fill:#FFFFFF;}
.formacao_profissional .st5{fill:#FFFFFF;stroke:#5EC6F2;stroke-width:0.25;stroke-miterlimit:10;}
.formacao_profissional .st6{fill:#00091A;}
.formacao_profissional .st7{fill:#003349;}
.formacao_profissional .st8{fill:#009ADD;}
.formacao_profissional .st9{fill:#0D6B95;}
#formacao_profissional:hover #arm-down { opacity:0 }
#formacao_profissional:hover #arm-up { opacity:1 }
</style>
</defs>
<g class="formacao_profissional">
<rect id="chao" x="-0.004" y="35" class="st2" width="360" height="200"/>
(...)
Any ideas?
Might it be interesting to add SVGInjector as a bower package?
If so, I'm willing to spend some work on this.
I just noticed, that when I use this super nice tool, the first IDs in the SVG is not changed. For example I have this SVG:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128" viewBox="0 0 128 128" data-inject-url="http://us.localhost.com:3000/assets/application/home/us-chat-in-development.svg"><title>Ribbon - Remove Chat from US Platform</title><defs><circle id="a" cx="64" cy="64" r="64"></circle></defs><g fill="none" fill-rule="evenodd"><mask id="b--inject-1" fill="#fff"><use xlink:href="#a"></use></mask><use fill="#D7DEE2" xlink:href="#a"></use><circle fill="#82C0D2" mask="url(#b--inject-1)" cx="64" cy="64" r="54"></circle><g mask="url(#b--inject-1)"><g transform="translate(36.44 23)"><path d="M2.157 48.825a1.94 1.94 0 0 1-.02-2.795l29.24-29.243a12.967 12.967 0 0 1 1.037-8.816c2.767-5.519 8.922-8.235 14.719-6.934l-6.058 6.485a1.984 1.984 0 0 0 .109 2.809l6.945 6.397c.811.747 2.08.698 2.828-.103l5.962-6.38a12.958 12.958 0 0 1-.87 9.408c-2.92 5.824-9.615 8.53-15.678 6.679L11.595 55.1a2.054 2.054 0 0 1-2.853.038l-6.585-6.313z" fill="#555"></path><path d="M1.437 48.11a1.94 1.94 0 0 1-.02-2.795l29.24-29.243a12.967 12.967 0 0 1 1.037-8.816C34.46 1.737 40.616-.98 46.412.32l-6.057 6.485a1.984 1.984 0 0 0 .108 2.81l6.945 6.396c.812.748 2.08.699 2.829-.102l5.961-6.381a12.958 12.958 0 0 1-.87 9.408c-2.919 5.825-9.614 8.53-15.678 6.68L10.874 54.384a2.054 2.054 0 0 1-2.852.038L1.437 48.11z" fill="#D7DEE2"></path><circle fill="#D7DEE2" cx="36.284" cy="16.223" r="2.5"></circle><circle fill="#D7DEE2" cx="41.517" cy="20.685" r="2.5"></circle></g><g fill-rule="nonzero"><path fill="#FFF" d="M39.297 39.09l9.745-9.745 33.6 33.599-9.746 9.745z"></path><path fill="#F6D7AA" opacity=".94" d="M35.51 25.762l13.397 3.613-9.703 9.69z"></path><path fill="#555" d="M70.774 70.414l9.745-9.745 4.22 4.22-9.746 9.745z"></path><path d="M74.983 74.592l9.758-9.634 4.852 4.817c.661.657.661 1.697 0 2.3l-7.388 7.335c-.661.657-1.709.657-2.315 0l-4.907-4.818z" fill="#D7DEE2"></path><path d="M36.061 25.543l3.473 1.314-2.866 2.846-1.268-3.448c-.22-.438.22-.876.661-.712z" fill="#555"></path></g></g><path d="M.183 84.805C-.303 83.808.196 83 1.305 83H126.45c1.106 0 1.77.874 1.486 1.941l-5.086 19.118c-.285 1.072-1.416 1.941-2.517 1.941H12.521c-1.105 0-2.393-.805-2.88-1.805L.183 84.805z" fill="#D7DEE2" mask="url(#b--inject-1)"></path><path d="M17 96.894v-7.772h1.27v7.772H17zm9.887 0H25.33L21.508 90.7h-.043l.027.346c.05.659.074 1.261.074 1.807v4.04h-1.153v-7.772h1.542l3.81 6.161h.033a36.05 36.05 0 0 1-.043-.89 30.766 30.766 0 0 1-.032-1.199v-4.072h1.164v7.772zm11.382-3.96c0 1.282-.356 2.263-1.069 2.942-.712.678-1.738 1.018-3.077 1.018h-2.174v-7.772h2.402c1.237 0 2.2.333 2.887 1 .687.666 1.03 1.603 1.03 2.812zm-1.34.042c0-1.86-.87-2.79-2.61-2.79h-1.1v5.64h.904c1.87 0 2.806-.95 2.806-2.85zm7.496 3.918h-4.396v-7.772h4.396v1.074h-3.126v2.116h2.929v1.063h-2.929v2.44h3.126v1.079zm6.198-7.772h1.318l-2.721 7.772h-1.34l-2.71-7.772h1.307l1.621 4.832c.085.23.176.527.271.89.096.364.158.634.186.811.046-.27.117-.581.213-.935.096-.355.174-.617.234-.787l1.621-4.81zm6.831 7.772h-4.396v-7.772h4.396v1.074h-3.126v2.116h2.93v1.063h-2.93v2.44h3.126v1.079zm1.77 0v-7.772h1.27v6.682h3.291v1.09h-4.56zm12.673-3.897c0 1.265-.316 2.249-.949 2.95C70.316 96.65 69.42 97 68.261 97c-1.173 0-2.074-.348-2.703-1.045-.629-.696-.943-1.685-.943-2.968s.316-2.268.949-2.956c.632-.687 1.535-1.031 2.708-1.031 1.155 0 2.048.35 2.679 1.047.63.698.946 1.682.946 2.95zm-5.932 0c0 .957.193 1.683.58 2.177.386.494.958.742 1.716.742.755 0 1.325-.245 1.71-.734.384-.489.576-1.217.576-2.185 0-.953-.19-1.676-.571-2.168-.381-.493-.95-.74-1.704-.74-.762 0-1.337.247-1.725.74-.388.492-.582 1.215-.582 2.168zm12.838-1.525c0 .811-.266 1.433-.798 1.866-.531.432-1.288.648-2.27.648h-.808v2.908h-1.27v-7.772h2.238c.97 0 1.698.199 2.182.596.484.397.726.981.726 1.754zm-3.876 1.446h.676c.652 0 1.13-.114 1.435-.34.305-.227.457-.582.457-1.064 0-.446-.137-.78-.41-.999-.272-.22-.698-.33-1.275-.33h-.883v2.733zm8.878 3.976l-2.254-6.49h-.042c.06.963.09 1.867.09 2.71v3.78h-1.154v-7.772h1.792l2.158 6.182h.032l2.222-6.182h1.796v7.772h-1.222V93.05c0-.386.01-.889.029-1.51.02-.62.036-.995.05-1.126h-.042l-2.334 6.48h-1.121zm11.174 0h-4.396v-7.772h4.396v1.074h-3.126v2.116h2.929v1.063h-2.929v2.44h3.126v1.079zm8.245 0h-1.558L97.844 90.7h-.042l.026.346c.05.659.075 1.261.075 1.807v4.04h-1.154v-7.772h1.542l3.81 6.161h.033a36.044 36.044 0 0 1-.043-.89 30.765 30.765 0 0 1-.032-1.199v-4.072h1.165v7.772zm4.826 0h-1.27v-6.682h-2.286v-1.09h5.842v1.09h-2.286v6.682z" fill="#333" mask="url(#b--inject-1)"></path></g></svg>
Here You can see that the id="a"
in the circle tag is not touched at all, but the following IDs in the mask tags are changed. I have another example, where all the IDs stay the same:
<?xml version="1.0" encoding="UTF-8"?> <svg width="24px" height="24px" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"> <g id="StarNoGradient" fill-rule="nonzero"> <polygon id="Combined-Shape-Copy" fill="#E3B500" points="12 0 12 19.9 4.584 24 6 15.317 0 9.167 8.292 7.9"></polygon> <polygon id="Combined-Shape-Copy-2" fill="#999999" points="12 0 15.708 7.9 24 9.167 18 15.317 19.416 24 12 19.9"></polygon> </g> </g> </svg>
I am wondering now, if its a tag related problem or if I just miss something here?
https://github.com/flobacher/SVGInjector2
Regularly updated and directly installable via bower/npm.
Since you do async loading of svg content, any code attempting to peek inside is almost guaranteed to fail. My solution to this (good enough for my purposes) is to fire an event when the last item in the queue has been processed and wrap the user code inside that event's listener:
var
. . .
processRequestQueue,
event = new CustomEvent("svg-injector-done", {}),
. . .
processRequestQueue = function (url) {
var i, len;
// Make these calls async so we avoid blocking the page/renderer
function asyncClone(index) {
setTimeout(function () {
requestQueue[url][index](cloneSvg(svgCache[url]));
// Tell the user we're done
if (index === len - 1) {
document.dispatchEvent(event);
}
}, 0);
}
for (i = 0, len = requestQueue[url].length; i < len; i += 1) {
asyncClone(i);
}
};
// Elsewhere:
document.addEventListener('svg-injector-done', function () {
// Work with that SVG
});
Can you see any problems with this? Any alternative solutions?
I can't immediately think of a good way to run SVG-facing code for each image in the queue. Fire an event with a payload that somehow identifies the image?
Hello everyone!
I included my SVG element like that:
<img id="thumb-icon" class="inject-me" src="/images/svg/innovate.svg" title="Preview" alt="Preview">
it works good in chrome but only "Preview" message in safari.
Safari's console said me that:
Unable to load SVG file: /images/svg/innovate.svg
How I can manage that problem?
Some idea?
hey again ;-)
i'm starting to use the iconic set with my current angular project ad i'd like to "preload" few icons and put them into the cache.
currently, it's not available and i need to load the icons the "normal" way.
it would be great to have something like
/**
* parses data and adds it to the cache on success
* @param {String} url url to cache it at
* @param {String|Node} data SVG (data or string) to parse
* @return {Boolean} true on success,
* false on failure
*/
function preload( url, data ) {
// ...
}
that would eventually parse the data and return Boolean result of the "preloading".
Would you implement that, or do you prefer PR?
this method would improve loading speed of my icons a bunch, since i could just inline the SVGs and "preload" them before injecting the icons :-)
what do you think?
SVGInjector is fantastic, I'm using it in the documentation for my software project.
In said documentation, I have ~30 very similar SVGs (examples of colored console output). Each SVG contains its own <style>
element so that they remain portable. The problem is that when all of them are embedded, the browser is inundated with ~30 copies of the same style rules.
Removing the <style>
elements afterward, with the each
callback, is too late to mitigate any performance impact they might have.
If I could define a callback that pruned the <style>
elements before they were appended, I could eliminate this problem. Alternatively, perhaps all I need is an option like evalScripts
, but for styles (perhaps includeStyles
?).
Hi @protodave and other contributors.
First off, amazing work! This lib has been very helpful for developing without needing to manage inline SVG in source code.
It seems there are 11 pull requests open, with the oldest one dating back to 2014. Is this lib no longer being maintained?
I want to submit a PR to allow attributes like onClick
, but am afraid that the PR will just sit like the others open.
"Is anybody out there listening?"
I have an SVG in an image tag like this which gets injected fine:
<img class="inject-me" src="assets/images/logo.svg" />
However, when I look at the network traffic (in Chrome Network tab), I can see that the browser downloads it (svg+xml) and then svg-injector.js downloads it again (xhr).
How can I stop each of the SVGs getting downloaded twice? Is there a way of injecting the SVGs without using tags as this seems to be cause of the first unnecessary download.
Many thanks
Hi,
I insert this in the <head>
tag but I get the title error:
<script src="svg-injector.min.js"></script>
<script>
// Elements to inject
var mySVGsToInject = document.querySelectorAll('.iconic-sprite');
// Do the injection
SVGInjector(mySVGsToInject);
</script>
Thanks for your help.
Hey,
I've the problem that he did not found the svg on the server but it's there
GET http://xxx/wp-content/themes/XXX/assets/images/svg/icons/icon_26_26.svg 404 (Not Found)
Local it runs all fine but remote I've no chance that it works.
Any Idea?
In our app we are using <span class="icon icon-name"></span>
with an icon font. I was thinking of trying to switch over to using SVGs and thus came here.
For our current structure is it possible to let it remain as it is, but then I would map the class names to their corresponding SVGs and replace them using SVGInjector?
Great work on this btw.
Cheers
From the Iconic forums:
https://useiconic.com/community/#!/tools:inject-safety-iconicjs-v0
We currently using iconic with our angularjs app.
In order to support dynamically added icons, we turned off the auto-inject selector, and have a directive to inject the iconic icon.
We originally had the auto-inject selector enabled together with our directive, but found a race condition where the auto-injection would still be running when the directive would start ‘injecting’; this would fail in the replaceChild call within iconic.min.js as the icon had already been processed.
We are finding another race condition, with a similar problem, where the directive might fire off multiple times and inject the same icon twice on the same element, failing with the same ‘replaceChild’ error.
Can you look at adding a check before injecting an icon to make sure it still needs injecting?
Note: This issue is in SVGInjector, on which iconic.js is built.
using SVGinjector 1.1.3 on Amref.nl and I receive a javascript error on Safari 9.0.2
[Error] TypeError: sourceSvg.cloneNode is not a function. (In 'sourceSvg.cloneNode(true)', 'sourceSvg.cloneNode' is undefined)
cloneSvg
(anonieme functie)
This results in not showing both img nor svg on Safari.
I have no issue on Chrome nor Firefox.
(this issue looks a lot like #25 but I can't see any solution in it)
I am using your extention for our web site. It is very cool.
Webp format is verr effective and small on size. But safari is not supporting it.
I solve this problem for static images by providing alternative sources to my image:
<picture class="img-fluid rounded-circle" alt="Hasan Behlül Bozal">
<source srcset="img/alternative.webp" type="image/webp">
<img class="img-fluid rounded-circle" src="img/alternative.jpg">
</picture>
How can I apply this solution with SVGInjector.
To see the problem you can go to https://robotic.mobi with safari browser.
// Callback after each SVG is injected
console.log('SVG injected: ' + svg.getAttribute('id'));
See: flobacher#9
When using SVG clipPath
elements, which are referenced and used via the url(#clip-path-id)
syntax, adding the <base>
tag to the page changes the meaning/location of that relative identifier fragment so the clipPaths can't be found (causing the SVG to not render as expected).
Using the <base>
tag can also cause issues with SVG gradients, masks, markers, etc… which are also referenced by relative fragment id.
References:
https://useiconic.com/community#!/tools:icons-not-displaying-correc
http://stackoverflow.com/questions/24673855/svg-clip-path-working-locally-but-not-online
As media type 'application/ecmascript'
is obsolete (new 'text/javascript'
) line: 336 should be updated:
if (!scriptType || scriptType === 'application/ecmascript' || scriptType === 'text/javascript' || scriptType === 'application/javascript') {
Source: https://www.iana.org/assignments/media-types/application/ecmascript
There is a flicker between when SVGInejctor runs and removes the img
tag and before the svg
appears. I think the img
should only be removed once the svg
is ready so the cutover is transparent. Or maybe this is an option the dev can opt into.
When injecting an icon from iconic that has a data-direction it resorts back to the default direction of the icon.
<img data-src="/build/img/logo.svg?v=0.1.81" data-fallback="/build/img/logo.png?v=0.1.81" class="svg" alt="Logo">
Doesn't work in Safari, this snippet throws an error
var cloneSvg = function (sourceSvg) {
return sourceSvg.cloneNode(true);
};
wouldn't it make sense to generate one SVG with <symbol>
elements representing every requested SVG that could be referenced via <use>
?
that would save a lot of repetition in the output HTML.
example:
<img class="inject-me" data-src="thumb-up.svg">
<img class="inject-me" data-src="thumb-up.svg">
<img class="inject-me" data-src="thumb-up.svg">
<img class="inject-me" data-src="thumb-up.svg">
<img class="inject-me" data-src="thumb-up.svg">
current solution:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="8" height="8" data-icon="thumb-up" viewBox="0 0 8 8" class=" iconic-injected-svg inject-me" data-src="thumb-up.svg">
<path d="M4.438 0c-.19.021-.34.149-.438.344-.13.26-1.101 2.185-1.281 2.375-.19.18-.439.281-.719.281v4.001h3.5c.21 0 .389-.133.469-.313 0 0 1.031-2.908 1.031-3.188 0-.28-.22-.5-.5-.5h-1.5c-.28 0-.5-.25-.5-.5s.389-1.574.469-1.844c.08-.27-.053-.545-.313-.625l-.219-.031zm-4.438 3v4h1v-4h-1z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="8" height="8" data-icon="thumb-up" viewBox="0 0 8 8" class=" iconic-injected-svg inject-me" data-src="thumb-up.svg">
<path d="M4.438 0c-.19.021-.34.149-.438.344-.13.26-1.101 2.185-1.281 2.375-.19.18-.439.281-.719.281v4.001h3.5c.21 0 .389-.133.469-.313 0 0 1.031-2.908 1.031-3.188 0-.28-.22-.5-.5-.5h-1.5c-.28 0-.5-.25-.5-.5s.389-1.574.469-1.844c.08-.27-.053-.545-.313-.625l-.219-.031zm-4.438 3v4h1v-4h-1z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="8" height="8" data-icon="thumb-up" viewBox="0 0 8 8" class=" iconic-injected-svg inject-me" data-src="thumb-up.svg">
<path d="M4.438 0c-.19.021-.34.149-.438.344-.13.26-1.101 2.185-1.281 2.375-.19.18-.439.281-.719.281v4.001h3.5c.21 0 .389-.133.469-.313 0 0 1.031-2.908 1.031-3.188 0-.28-.22-.5-.5-.5h-1.5c-.28 0-.5-.25-.5-.5s.389-1.574.469-1.844c.08-.27-.053-.545-.313-.625l-.219-.031zm-4.438 3v4h1v-4h-1z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="8" height="8" data-icon="thumb-up" viewBox="0 0 8 8" class=" iconic-injected-svg inject-me" data-src="thumb-up.svg">
<path d="M4.438 0c-.19.021-.34.149-.438.344-.13.26-1.101 2.185-1.281 2.375-.19.18-.439.281-.719.281v4.001h3.5c.21 0 .389-.133.469-.313 0 0 1.031-2.908 1.031-3.188 0-.28-.22-.5-.5-.5h-1.5c-.28 0-.5-.25-.5-.5s.389-1.574.469-1.844c.08-.27-.053-.545-.313-.625l-.219-.031zm-4.438 3v4h1v-4h-1z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="8" height="8" data-icon="thumb-up" viewBox="0 0 8 8" class=" iconic-injected-svg inject-me" data-src="thumb-up.svg">
<path d="M4.438 0c-.19.021-.34.149-.438.344-.13.26-1.101 2.185-1.281 2.375-.19.18-.439.281-.719.281v4.001h3.5c.21 0 .389-.133.469-.313 0 0 1.031-2.908 1.031-3.188 0-.28-.22-.5-.5-.5h-1.5c-.28 0-.5-.25-.5-.5s.389-1.574.469-1.844c.08-.27-.053-.545-.313-.625l-.219-.031zm-4.438 3v4h1v-4h-1z"></path>
</svg>
proposed solution:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="iconic-symbols">
<symbol viewBox="0 0 8 8" id="thumb-up_svg">
<path d="M4.438 0c-.19.021-.34.149-.438.344-.13.26-1.101 2.185-1.281 2.375-.19.18-.439.281-.719.281v4.001h3.5c.21 0 .389-.133.469-.313 0 0 1.031-2.908 1.031-3.188 0-.28-.22-.5-.5-.5h-1.5c-.28 0-.5-.25-.5-.5s.389-1.574.469-1.844c.08-.27-.053-.545-.313-.625l-.219-.031zm-4.438 3v4h1v-4h-1z"></path>
</symbol>
</svg>
<!--- ... --->
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="8" height="8" class=" iconic-injected-svg inject-me" data-src="thumb-up.svg">
<use xlink:href="#thumb-up_svg" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="8" height="8" class=" iconic-injected-svg inject-me" data-src="thumb-up.svg">
<use xlink:href="#thumb-up_svg" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="8" height="8" class=" iconic-injected-svg inject-me" data-src="thumb-up.svg">
<use xlink:href="#thumb-up_svg" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="8" height="8" class=" iconic-injected-svg inject-me" data-src="thumb-up.svg">
<use xlink:href="#thumb-up_svg" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="8" height="8" class=" iconic-injected-svg inject-me" data-src="thumb-up.svg">
<use xlink:href="#thumb-up_svg" />
</svg>
probably the holder could be hidden or even put into <head>
(need to research that), but IMO the benefits are obvious ;-)
what do you think?
I couldn't figure out why but injecting the attached vector doesn't show the vector on the page while it does exist in the DOM. I'm guessing it has to do with the gradient fill references...
jellyfish-anatomy.svg.zip
I'm using SVG Injector and it works perfectly on IE 10 +, but on IE 9 SVGs are not loading. I inspected the source and I realized SVG Injector is using a base64 image. According to this, base64 is not supported on IE 9. So, I assume the only way to use SVG Injector on IE 9 is through fallbacks?
SVGinjector should have an option to auto detect the location of the fallback png files. It could look for them in the location of the svg files (replace .svg with .png).
Hi,
I downloaded a template with this plugin included but it doesn't work on Chrome (but works on Firefox...). I get the name of the class displayed instead of the SVG image. When I look in Chrome's code inspector (F12) I can see that every src attribute to the SVG image is replaced by "(unknown)" (instead of the path).
Anybody knows how to fix this issue ? Thanks
Currently, the fallback mechanism is "global"; you provide a directory path and the Injector will swap all targeted .svg
for .png
. This has the drawback of requiring you to provide a new SVG for each PNG you need to fit the context :
Say you have a blue pictogram, they are named
foo.svg
andfoo.png
.If you customize the colours of the SVG for a certain context, the basic PNG might no longer be appropriate/useable. In one location you've added a red stroke to your SVG, in another it's twice as large, or in another location you got a blue background and you colour your SVG white.
You'd necessarily need to create a new PNG (
foo-stroked.png
,foo-white.png
,foo-large.png
) and a corresponding SVG (foo-stroked.svg
,foo-white.svg
,foo-large.svg
) for each new context.
This kind of defeats the purpose of a single SVG being flexible. Even if you just clone your blue-filled XML with a new filename.
What I've done on my side is tweak the injectElement()
function to check if the current element has a data-fallback
attribute. If not rely on the pngFallback
:
var injectElement = function (el, evalScripts, pngFallback, callback) {
// Grab the src or data-src attribute
var imgUrl = el.getAttribute('data-src') || el.getAttribute('src');
// We can only inject SVG
if (!(/svg$/i).test(imgUrl)) {
callback('Attempted to inject a file with a non-svg extension: ' + imgUrl);
return;
}
// If we don't have SVG support try to fall back to png
if (!hasSvgSupport) {
var backupUrl = el.getAttribute('data-fallback') || el.getAttribute('data-png');
if (!backupUrl && pngFallback) {
backupUrl = pngFallback + '/' + imgUrl.split('/').pop().replace('.svg', '.png');
}
if (backupUrl) {
el.setAttribute('src', backupUrl);
callback(null);
}
else {
callback('This browser does not support SVG and no PNG fallback was defined.');
}
return;
}
// ...
And the HTML:
<img class="inject-me" data-src="foo.svg" data-fallback="foo-qux.png">
This way I can always use the same SVG but provide a unique PNG for each context.
What do you think? Is there something I'm missing or might not have noticed? Is this a viable idea?
If it isn't implemented, I can just fork this repo and apply the change for myself and whoever else might need this feature.
Fantastic library, by the way. Really digging it!
Hey guys, I am using the iconic.js library and I can isolate that it's the SVGInjector line that is causing me troubles:
Line 377 in 8fefa15
The context of my issue is, I have an angular application that uses iconic to handle loading in the svgs. We are running into issues because of some weird intermediate states that the parentNode is null for certain elements, like when we are binding to the element before it gets into the dom like on a directive template replacement.
That said, I am wondering if making the check for whether or not the image is available to be replaced (via parentNode.replaceChild) is a condition worthy of being added as a prerequisite for processing similar to the perquisites of it being a .svg file type?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.