10.1.68 • Published 1 year ago

@tanem/svg-injector v10.1.68

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

svg-injector

npm version build status coverage status npm downloads minzipped size

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

<div id="inject-me" data-src="icon.svg"></div>
import { SVGInjector } from '@tanem/svg-injector'

SVGInjector(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.

import { SVGInjector } from '@tanem/svg-injector'
import DOMPurify from 'dompurify'

SVGInjector(document.getElementById('inject-me'), {
  beforeEach(svg) {
    DOMPurify.sanitize(svg, {
      IN_PLACE: true,
      USE_PROFILES: { svg: true, svgFilters: true },
    })
  },
})

Live Examples

API

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

<div class="inject-me" data-src="icon-one.svg"></div>
<div class="inject-me" data-src="icon-two.svg"></div>
import { SVGInjector } from '@tanem/svg-injector'

SVGInjector(document.getElementsByClassName('inject-me'), {
  afterAll(elementsLoaded) {
    console.log(`injected ${elementsLoaded} elements`)
  },
  afterEach(err, svg) {
    if (err) {
      throw err
    }
    console.log(`injected ${svg.outerHTML}`)
  },
  beforeEach(svg) {
    svg.setAttribute('stroke', 'red')
  },
  cacheRequests: false,
  evalScripts: 'once',
  httpRequestWithCredentials: false,
  renumerateIRIElements: false,
})

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:

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

10.1.60

1 year ago

10.1.61

1 year ago

10.1.68

1 year ago

10.1.66

1 year ago

10.1.67

1 year ago

10.1.64

1 year ago

10.1.65

1 year ago

10.1.62

1 year ago

10.1.63

1 year ago

10.1.58

1 year ago

10.1.59

1 year ago

10.1.57

1 year ago

10.1.55

2 years ago

10.1.56

1 year ago

10.1.53

2 years ago

10.1.54

2 years ago

10.1.51

2 years ago

10.1.52

2 years ago

10.1.50

2 years ago

10.1.48

2 years ago

10.1.49

2 years ago

10.1.46

2 years ago

10.1.47

2 years ago

10.1.44

2 years ago

10.1.45

2 years ago

10.1.42

2 years ago

10.1.43

2 years ago

10.1.40

2 years ago

10.1.41

2 years ago

10.1.39

2 years ago

10.1.35

2 years ago

10.1.36

2 years ago

10.1.34

2 years ago

10.1.37

2 years ago

10.1.38

2 years ago

10.1.33

2 years ago

10.1.32

2 years ago

10.1.31

2 years ago

10.1.30

2 years ago

10.1.25

2 years ago

10.1.28

2 years ago

10.1.29

2 years ago

10.1.26

2 years ago

10.1.27

2 years ago

10.1.24

2 years ago

10.1.23

2 years ago

10.1.22

3 years ago

10.1.21

3 years ago

10.1.19

3 years ago

10.1.20

3 years ago

10.1.17

3 years ago

10.1.18

3 years ago

10.1.15

3 years ago

10.1.16

3 years ago

10.1.13

3 years ago

10.1.14

3 years ago

10.1.11

3 years ago

10.1.12

3 years ago

10.1.10

3 years ago

10.1.9

3 years ago

10.1.8

3 years ago

10.1.7

3 years ago

10.1.6

3 years ago

10.1.5

3 years ago

10.1.4

3 years ago

10.1.3

3 years ago

10.1.2

3 years ago

10.1.1

3 years ago

10.1.0

3 years ago

10.0.8

3 years ago

10.0.7

3 years ago

10.0.6

3 years ago

10.0.5

3 years ago

10.0.4

3 years ago

10.0.3

3 years ago

10.0.2

4 years ago

10.0.1

4 years ago

10.0.0

4 years ago

9.0.5

4 years ago

9.0.4

4 years ago

9.0.3

4 years ago

9.0.2

4 years ago

9.0.1

4 years ago

9.0.0

4 years ago

8.2.5

4 years ago

8.2.4

4 years ago

8.2.3

4 years ago

8.2.2

4 years ago

8.2.1

4 years ago

8.2.0

4 years ago

8.1.0

4 years ago

8.0.72

4 years ago

8.0.71

4 years ago

8.0.70

4 years ago

8.0.69

4 years ago

8.0.68

4 years ago

8.0.67

4 years ago

8.0.65

4 years ago

8.0.66

4 years ago

8.0.64

4 years ago

8.0.63

4 years ago

8.0.62

4 years ago

8.0.61

4 years ago

8.0.60

4 years ago

8.0.59

4 years ago

8.0.58

4 years ago

8.0.57

4 years ago

8.0.56

4 years ago

8.0.55

4 years ago

8.0.54

4 years ago

8.0.53

4 years ago

8.0.52

4 years ago

8.0.51

5 years ago

8.0.50

5 years ago

8.0.49

5 years ago

8.0.48

5 years ago

8.0.47

5 years ago

8.0.46

5 years ago

8.0.45

5 years ago

8.0.44

5 years ago

8.0.43

5 years ago

8.0.42

5 years ago

8.0.41

5 years ago

8.0.40

5 years ago

8.0.39

5 years ago

8.0.38

5 years ago

8.0.37

5 years ago

8.0.36

5 years ago

8.0.35

5 years ago

8.0.34

5 years ago

8.0.33

5 years ago

8.0.32

5 years ago

8.0.31

5 years ago

8.0.30

5 years ago

8.0.29

5 years ago

8.0.28

5 years ago

8.0.27

5 years ago

8.0.26

5 years ago

8.0.25

5 years ago

8.0.24

5 years ago

8.0.23

5 years ago

8.0.22

5 years ago

8.0.21

5 years ago

8.0.20

5 years ago

8.0.19

5 years ago

8.0.18

5 years ago

8.0.17

5 years ago

8.0.16

5 years ago

8.0.15

5 years ago

8.0.14

5 years ago

8.0.13

5 years ago

8.0.12

5 years ago

8.0.11

5 years ago

8.0.10

5 years ago

8.0.9

5 years ago

8.0.8

5 years ago

8.0.7

5 years ago

8.0.6

6 years ago

8.0.5

6 years ago

8.0.4

6 years ago

8.0.3

6 years ago

8.0.2

6 years ago

8.0.1

6 years ago

8.0.0

6 years ago

7.0.17

6 years ago

7.0.16

6 years ago

7.0.15

6 years ago

7.0.14

6 years ago

7.0.13

6 years ago

7.0.12

6 years ago

7.0.11

6 years ago

7.0.10

6 years ago

7.0.9

6 years ago

7.0.8

6 years ago

7.0.7

6 years ago

7.0.6

6 years ago

7.0.5

6 years ago

7.0.4

6 years ago

7.0.3

6 years ago

7.0.2

6 years ago

7.0.1

6 years ago

7.0.0

6 years ago

6.0.5

6 years ago

6.0.4

6 years ago

6.0.3

6 years ago

6.0.2

6 years ago

6.0.1

6 years ago

2.1.4

6 years ago

6.0.0

6 years ago

5.0.7

6 years ago

5.0.6

6 years ago

5.0.5

6 years ago

5.0.4

6 years ago

5.0.3

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

5.0.2

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.0

6 years ago

3.0.0-beta.0

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.10

6 years ago

1.1.9

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago