Gathering detailed insights and metrics for @tanem/svg-injector
Gathering detailed insights and metrics for @tanem/svg-injector
Gathering detailed insights and metrics for @tanem/svg-injector
Gathering detailed insights and metrics for @tanem/svg-injector
💉 Fast, caching, dynamic inline SVG DOM injection library.
npm install @tanem/svg-injector
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
102 Stars
2,136 Commits
14 Forks
2 Watching
7 Branches
9 Contributors
Updated on 29 Oct 2024
TypeScript (92.18%)
JavaScript (7.82%)
Cumulative downloads
Total Downloads
Last day
-5.9%
32,592
Compared to previous day
Last week
3.5%
177,611
Compared to previous week
Last month
6.8%
757,290
Compared to previous month
Last year
14%
8,082,432
Compared to previous year
3
46
A fast, caching, dynamic inline SVG DOM injection library.
There are a number of ways to use SVG on a page (object
, embed
, iframe
, img
, CSS background-image
) but to unlock the full potential of SVG, including full element-level CSS styling and evaluation of embedded JavaScript, the full SVG markup must be included directly in the DOM.
Wrangling and maintaining a bunch of inline SVG on your pages isn't anyone's idea of good time, so SVGInjector
lets you work with simple tag elements and does the heavy lifting of swapping in the SVG markup inline for you.
1<div id="inject-me" data-src="icon.svg"></div>
1import { SVGInjector } from '@tanem/svg-injector' 2 3SVGInjector(document.getElementById('inject-me'))
Be careful when injecting arbitrary third-party SVGs into the DOM, as this opens the door to XSS attacks. If you must inject third-party SVGs, it is highly recommended to sanitize the SVG before injecting. The following example uses DOMPurify to strip out attributes and tags that can execute arbitrary JavaScript. Note that this can alter the behavior of the SVG.
1import { SVGInjector } from '@tanem/svg-injector' 2import DOMPurify from 'dompurify' 3 4SVGInjector(document.getElementById('inject-me'), { 5 beforeEach(svg) { 6 DOMPurify.sanitize(svg, { 7 IN_PLACE: true, 8 USE_PROFILES: { svg: true, svgFilters: true }, 9 }) 10 }, 11})
Arguments
elements
- A single DOM element or array of elements, with src
or data-src
attributes defined, to inject.options
- Optional An object containing the optional arguments defined below. Defaults to {}
.
afterAll(elementsLoaded)
- Optional A callback which is called when all elements have been processed. elementsLoaded
is the total number of elements loaded. Defaults to () => undefined
.afterEach(err, svg)
- Optional A callback which is called when each element is processed. svg
is the newly injected SVG DOM element. Defaults to () => undefined
.beforeEach(svg)
- Optional A callback which is called just before each SVG element is added to the DOM. svg
is the SVG DOM element which is about to be injected. Defaults to () => undefined
.cacheRequests
- Optional Use request cache. Defaults to true
.evalScripts
- Optional Run any script blocks found in the SVG. One of 'always'
, 'once'
, or 'never'
. Defaults to 'never'
.httpRequestWithCredentials
- Optional Boolean that indicates whether or not cross-site Access-Control requests should be made using credentials. Defaults to false
.renumerateIRIElements
- Optional Boolean indicating if SVG IRI addressable elements should be renumerated. Defaults to true
.Example
1<div class="inject-me" data-src="icon-one.svg"></div> 2<div class="inject-me" data-src="icon-two.svg"></div>
1import { SVGInjector } from '@tanem/svg-injector' 2 3SVGInjector(document.getElementsByClassName('inject-me'), { 4 afterAll(elementsLoaded) { 5 console.log(`injected ${elementsLoaded} elements`) 6 }, 7 afterEach(err, svg) { 8 if (err) { 9 throw err 10 } 11 console.log(`injected ${svg.outerHTML}`) 12 }, 13 beforeEach(svg) { 14 svg.setAttribute('stroke', 'red') 15 }, 16 cacheRequests: false, 17 evalScripts: 'once', 18 httpRequestWithCredentials: false, 19 renumerateIRIElements: false, 20})
⚠️This library uses
Array.from()
, so if you're targeting browsers that don't support that method, you'll need to ensure an appropriate polyfill is included manually. See this issue comment for further detail.
$ npm install @tanem/svg-injector
There are also UMD builds available via unpkg:
This is a fork of a library originally developed by Waybury for use in iconic.js, part of the Iconic icon system.
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
5 existing vulnerabilities detected
Details
Reason
4 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
Found 0/29 approved changesets -- score normalized to 0
Reason
dangerous workflow patterns detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More