💉 Fast, caching, dynamic inline SVG DOM injection library.
Installations
npm install @tanem/svg-injector
Releases
Unable to fetch releases
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
20.9.0
NPM Version
10.2.3
Statistics
102 Stars
2,136 Commits
14 Forks
2 Watching
7 Branches
9 Contributors
Updated on 29 Oct 2024
Languages
TypeScript (92.18%)
JavaScript (7.82%)
Total Downloads
Cumulative downloads
Total Downloads
30,980,558
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
Dev Dependencies
46
svg-injector
A fast, caching, dynamic inline SVG DOM injection library.
Background
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.
Basic Usage
1<div id="inject-me" data-src="icon.svg"></div>
1import { SVGInjector } from '@tanem/svg-injector' 2 3SVGInjector(document.getElementById('inject-me'))
Avoiding XSS
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})
Live Examples
- Basic Usage: Source | Sandbox
- API Usage: Source | Sandbox
- MooTools: Source | Sandbox
- UMD Build (Development): Source | Sandbox
- UMD Build (Production): Source | Sandbox
API
Arguments
elements
- A single DOM element or array of elements, withsrc
ordata-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 totrue
.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 tofalse
.renumerateIRIElements
- Optional Boolean indicating if SVG IRI addressable elements should be renumerated. Defaults totrue
.
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})
Installation
⚠️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:
- https://unpkg.com/@tanem/svg-injector/dist/svg-injector.umd.development.js
- https://unpkg.com/@tanem/svg-injector/dist/svg-injector.umd.production.js
Credit
This is a fork of a library originally developed by Waybury for use in iconic.js, part of the Iconic icon system.
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
5 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-7hpj-7hhx-2fgx
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
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
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/tanem/svg-injector/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/tanem/svg-injector/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/tanem/svg-injector/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/tanem/svg-injector/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:35: update your workflow using https://app.stepsecurity.io/secureworkflow/tanem/svg-injector/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:41: update your workflow using https://app.stepsecurity.io/secureworkflow/tanem/svg-injector/ci.yml/master?enable=pin
- Info: 0 out of 3 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 3 third-party GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
Found 0/29 approved changesets -- score normalized to 0
Reason
dangerous workflow patterns detected
Details
- Warn: untrusted code checkout '${{ github.event.pull_request.head.sha }}': .github/workflows/ci.yml:29
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 29 are checked with a SAST tool
Score
2.3
/10
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