Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Support for Fragment Ids #27

Open
wants to merge 224 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
224 commits
Select commit Hold shift + click to select a range
a9840d8
add support for fragment-ids specifying views or symbols
Mar 28, 2015
d728414
add example for symbol and view referencing via fragment-ids
Mar 28, 2015
541df26
update version number for bower
flobacher Mar 28, 2015
925c812
changed indentation
flobacher Mar 28, 2015
5966d86
add option, to only insert visible part specified via fragment-id of …
flobacher May 1, 2015
fd16ef5
enable default fallback for sprites referenced via fragment-id
flobacher May 1, 2015
2f35d0e
extend fragment-id example by a spritesheet generated with grunt-svg-…
flobacher May 1, 2015
b37313e
if view displays a use tag, the referenced symbol is injected
flobacher May 1, 2015
fecb5a7
update fragment-id example
flobacher May 1, 2015
e80cbdd
queryselector is used to speed up finding of the svg shape inside the…
flobacher May 1, 2015
1ce9a4a
style tags will be removed if special class is found on injection-tar…
flobacher May 7, 2015
588bac2
add editor-config
flobacher May 7, 2015
a6f31d2
add dist files
flobacher May 7, 2015
01e5a0c
cleanup, add drop_console to uglify options -> remove calls to consol…
May 7, 2015
5efa1de
png fallbacks work
May 11, 2015
1131a95
png-fallbacks
May 11, 2015
2229e36
provide array with fallback classnames that contain a replacement-char
May 12, 2015
3978592
bugfix fallback classname creation
May 12, 2015
fe09f74
bugfix fallback classname creation
May 12, 2015
34cb3ba
optimise classname creation
May 12, 2015
59c2c17
remove no-svg class from html element is svg is supported
May 12, 2015
55c6657
remove fallback classes on injection
May 12, 2015
d1c81ce
do not remove classnames if not there
flobacher May 21, 2015
eb0d92e
output correct logs
May 26, 2015
d9c017c
fix remove style tag from parent, which can be a <defs> as well as th…
May 26, 2015
9e7b071
update minified version
May 26, 2015
d317caa
add default icon class -> if inect-target has this class, all styles …
May 26, 2015
e38b86d
prefix styletags upon injection
flobacher May 27, 2015
5e277e4
do not write default removeStyleClass if it is specified in the options
May 27, 2015
1a3a7b0
edit fallback classnames list
flobacher May 28, 2015
d7893d4
add option to remove all style-tags upon injection
flobacher May 28, 2015
9198950
add option to keep inline-styles for some elems (specify via class) e…
flobacher May 28, 2015
b6693a4
Copy preserveAspectRatio of elem if exists
May 28, 2015
6f4863a
add possibility to add a common spritesheetURL for all svg instances
Jun 1, 2015
7c23f6e
optional prefix for fragIdClass assigned upon injection
flobacher Jun 2, 2015
d68f27f
version bump
flobacher Jun 2, 2015
15e1754
prefix all attributes that reference an id from defs
Jun 8, 2015
2b31cb7
unify id-prefixing
flobacher Jun 9, 2015
0b67cff
cleanup logs
flobacher Jun 9, 2015
b9c3944
version bump
Jun 9, 2015
99ed486
1.2.2
Jun 10, 2015
70b802e
change the default classnames that should be removed upon injection, …
Jun 10, 2015
06e6dfd
refactor to use an oop approach
Jun 10, 2015
4be677c
cleanup the oop approach
flobacher Jun 12, 2015
543fc97
add angular suport
flobacher Jun 13, 2015
3d91982
add angular suport 2
flobacher Jun 13, 2015
f1aa85b
refactor angular support
flobacher Jun 15, 2015
1d1a757
simplify build-setup
Jun 15, 2015
73e1ed3
simplify build-setup
Jun 15, 2015
1529207
2.0.2
Jun 15, 2015
9f959a3
2.0.2
Jun 15, 2015
d660786
replace angular.value by provider
Jun 15, 2015
861ed83
change name
Jun 15, 2015
2067b0c
bug fix: find symbol by view box returned more than one symbol
flobacher Jun 18, 2015
dc90149
Updated CHANGELOG
flobacher Jun 18, 2015
6766656
bugfix
flobacher Jun 18, 2015
eb677a0
2.0.5
flobacher Jun 18, 2015
623b590
bugfix
flobacher Jun 18, 2015
f25e199
2.0.6
flobacher Jun 18, 2015
5dd714b
enable use of data-src in combination with spritesheet
Jul 9, 2015
82d7c05
2.0.7
Jul 9, 2015
35d29ae
Updated CHANGELOG
Jul 9, 2015
990f631
fix indentations
Jul 9, 2015
4203538
fix indentations
Jul 9, 2015
c08fe76
improve support for combining single svgs with a spritesheet
Jul 9, 2015
c8bd742
remove console-logs
Jul 9, 2015
37aca0a
2.0.8
Jul 9, 2015
2dfc3e6
start prefixing of style-blocks if injected svg is not a spritesheet
flobacher Jul 10, 2015
64b1a8d
prefix style-blocks if injected svg is not a spritesheet
flobacher Jul 14, 2015
2255226
2.0.9
flobacher Jul 14, 2015
72e5a02
abort injection if no sprite-- class and no data-src found
flobacher Jul 28, 2015
768c624
remove unused variable
flobacher Jul 28, 2015
d29eb5b
wrap angular directive into $observe so injection will trigger even i…
flobacher Jul 28, 2015
05f6d56
2.0.10
flobacher Jul 28, 2015
a0c06d9
Updated CHANGELOG
flobacher Jul 28, 2015
29f99e2
bugfix angular directive
flobacher Jul 28, 2015
3e86de1
2.0.11
flobacher Jul 28, 2015
e562641
Updated CHANGELOG
flobacher Jul 28, 2015
d2eb782
fixed bug to work with multiple spritemaps
hochitom Mar 1, 2016
ee04dc3
Merge pull request #1 from hochitom/hochitom-patch-1
hochitom Mar 1, 2016
0b91e02
Merge pull request #1 from hochitom/master
flobacher Mar 1, 2016
a5ee0c8
2.0.12
flobacher Mar 1, 2016
8281b74
additional iri references are matched
flobacher Mar 1, 2016
8612dea
build scripts updated
flobacher Mar 1, 2016
5164dea
2.0.13
flobacher Mar 1, 2016
e44352e
build scripts updated
flobacher Mar 1, 2016
a5bc1b3
remove deprecated version from bower
flobacher Mar 2, 2016
303c20c
update dependency
flobacher Mar 2, 2016
990d68d
rearange npm scripts for better readability
flobacher Mar 2, 2016
507212f
ignore tests and examples when installed via nom
flobacher Mar 20, 2016
e9ef3ed
cleanup logs
flobacher Mar 20, 2016
ffc0ad2
elements can also be referenced via xlink:href.. need to suffix them …
flobacher Mar 20, 2016
eccb575
fix: previous version added suffix multiple times
flobacher Mar 21, 2016
f189ab4
elements can also be referenced via xlink:href. this time it works
Mar 21, 2016
571dec2
add support for IE
Mar 21, 2016
7c25536
2.0.14
flobacher Mar 21, 2016
ef029f2
add support to set title for accessibility
Mar 21, 2016
359a256
add support for the desc element and add the role="presentation" to a…
Mar 22, 2016
9d91dc3
update gitignore
flobacher Mar 22, 2016
fc29aea
2.0.15
flobacher Mar 22, 2016
6292886
update repo url and scripts
flobacher Mar 22, 2016
5e7774e
2.0.16
flobacher Mar 22, 2016
49cceae
update repo url and scripts
flobacher Mar 22, 2016
41f86cc
update scripts
flobacher Mar 22, 2016
c39a16f
2.0.17
flobacher Mar 22, 2016
52f5fe4
2.0.18
flobacher Mar 22, 2016
da5f9e4
cleanup
flobacher Mar 22, 2016
045205b
2.0.19
flobacher Mar 22, 2016
cfa5475
update scripts
flobacher Mar 22, 2016
c508c18
2.0.20
flobacher Mar 22, 2016
4f2317e
update readme
flobacher Mar 22, 2016
33afd04
2.0.21
flobacher Mar 22, 2016
84fd93c
update ignore rules
flobacher Mar 22, 2016
8453317
2.0.22
flobacher Mar 22, 2016
66f4ede
use .trim() only if element has a class attribute
hochitom Mar 22, 2016
ec44146
IE does not support Children on SVGElement!
Mar 22, 2016
ff5578d
${npm_package_version}
Mar 22, 2016
e425f4e
avoid latedef warnings
Mar 22, 2016
216993c
2.0.23
Mar 22, 2016
d945474
make npm scripts work on windows as well
Mar 22, 2016
9480d16
codestyle
Mar 22, 2016
a721de4
make npm scripts work on windows as well
Mar 22, 2016
5b5b00a
make npm scripts work on windows as well
Mar 22, 2016
7ae93d3
add shortcut script
Mar 22, 2016
dec40de
2.0.24
Mar 22, 2016
4ce5a2b
Merge branch 'master' of https://github.com/hochitom/SVGInjector2 int…
Mar 22, 2016
b59d1d8
Merge branch 'hochitom-master'
Mar 22, 2016
94916ed
2.0.25
Mar 22, 2016
8310c96
add accessibility section
Mar 22, 2016
07217d8
codestyle
Mar 22, 2016
9487319
2.0.26
Mar 22, 2016
f8cf0fe
added support for aria-hidden attribute on svg element
dgoerdes Mar 31, 2016
018127d
Merge pull request #3 from dgoerdes/master
flobacher Mar 31, 2016
bd6213b
update/move examples
flobacher Apr 3, 2016
bc9aab5
update scripts
flobacher Apr 3, 2016
d01d011
suffix id-references in style-tags and refactor
flobacher Apr 3, 2016
24e397d
do not add role=presentation to title and desc
flobacher Apr 3, 2016
aadf829
correct those id-refs in sample sprite sheet.. this needs fixing in i…
flobacher Apr 3, 2016
2cb030f
2.0.27
flobacher Apr 3, 2016
168c99d
respect aria-hidden also on injected sag
flobacher Apr 4, 2016
f16c19a
2.0.28
flobacher Apr 4, 2016
5da3c45
reorganise folder-structure, update examples and copy all attributes …
flobacher Feb 13, 2017
cadac66
fix package-scripts
Feb 13, 2017
e6c040f
update examples
Feb 13, 2017
3cd8015
update README
Feb 13, 2017
7a39a24
Update broken examples/spritesheet link
Feb 16, 2017
8a22972
Merge pull request #7 from TheCopartnery/patch-1
flobacher Feb 17, 2017
1aac685
fix fallbacks
flobacher Feb 20, 2017
58f4acd
do not remove console.logs
flobacher Feb 20, 2017
b1ccb91
update examples
flobacher Feb 20, 2017
c5e6c81
fix package scripts
flobacher Feb 20, 2017
95bfd75
2.0.29
flobacher Feb 20, 2017
60b98b1
fix package script
flobacher Feb 20, 2017
41f49c8
fix main entry in package.json
flobacher Feb 21, 2017
759c681
2.0.30
flobacher Feb 21, 2017
febe4b2
update links
Feb 22, 2017
ed77d43
Merge pull request #10 from TheCopartnery/patch-3
flobacher Feb 22, 2017
5803a9a
add copy command to stay compatible with file structure of version < …
flobacher Feb 22, 2017
fd897d7
codestyle: streamline folder-pathes
flobacher Feb 22, 2017
eb84ea3
make sure script works on all platforms
flobacher Feb 22, 2017
dd3f7d0
add copy of file to stay compatible with file structure of version < …
flobacher Feb 22, 2017
f19994c
fix main entry in bower.json
flobacher Feb 22, 2017
0c64792
v2.0.31
flobacher Feb 22, 2017
50eaafb
always set data-src even if only src was specified
flobacher Feb 22, 2017
794d4f5
add a basic, non-spritesheet example
flobacher Feb 22, 2017
d534db0
2.0.32
flobacher Feb 22, 2017
69ecf67
update/streamline examples and read
flobacher Feb 23, 2017
caedbeb
Add simple with img tag example
Feb 24, 2017
00838d8
Update README.md
Feb 24, 2017
35a3f6a
update README.md
flobacher Feb 27, 2017
9a951f4
Merge pull request #13 from TheCopartnery/master
flobacher Feb 27, 2017
3c85037
Merge pull request #14 from TheCopartnery/patch-4
flobacher Feb 27, 2017
2658876
add options
flobacher Feb 27, 2017
147b953
add options
flobacher Feb 27, 2017
85086e0
do not change src
flobacher Feb 27, 2017
73c3432
2.0.33
flobacher Feb 27, 2017
3e72195
Check for imgUrl - inline svg’s will not have an imgUrl (src/data-src…
Mar 1, 2017
a188971
spelling correction
Mar 1, 2017
a1cb4db
Merge pull request #16 from TheCopartnery/master
flobacher Mar 4, 2017
ba2226f
remove console.logs in minified version
flobacher Mar 5, 2017
3aef12b
add inline svgs to examples
flobacher Mar 5, 2017
3c4ba0e
exit, when neither src, data-src, nor sprite sheet url are specified
flobacher Mar 5, 2017
a0459c8
getter for config
flobacher Mar 5, 2017
7eab253
codestyle
flobacher Mar 5, 2017
215638d
only execute angular-directive code if src, data-src or sprite-class …
flobacher Mar 5, 2017
1de9aa8
adapt example
flobacher Mar 5, 2017
b1a17fc
add example for interpolation support of classnames
flobacher Mar 5, 2017
dedbd9b
start adding support for interpolated data-src
flobacher Mar 5, 2017
699af49
codestyle
flobacher Mar 5, 2017
80f8b04
2.0.34
flobacher Mar 5, 2017
9c09103
fix package-script
flobacher Mar 5, 2017
6e7f25f
angular-directive: allow sprite-id class to be not the first in the c…
flobacher Mar 13, 2017
7897ca7
update example
flobacher Mar 13, 2017
9b0617f
2.0.35
flobacher Mar 13, 2017
9517d99
update version nr. on build
flobacher Mar 28, 2017
9cb552c
2.0.36
flobacher Mar 28, 2017
9f81e08
bugfix, replace element
sladiri May 12, 2017
d11d9ba
Merge pull request #22 from sladiri/patch-1
flobacher May 12, 2017
e4d6f1a
2.0.37
May 12, 2017
de4a372
add the option to provide fallback svgs for fragment ids, that might …
May 12, 2017
54fa00d
2.1.0
May 12, 2017
6221b19
add width and height also on single elements (not spritesheet-fragmen…
May 15, 2017
013bb9d
codestyle
May 15, 2017
ba82a4c
2.1.1
May 15, 2017
c934bd3
chore: update for usage with node 8
Oct 9, 2017
aafb1fe
npm 5 support. hurray =)
Oct 9, 2017
4f71cc4
add `h1` to titles to identify them easier
Oct 9, 2017
fc49c49
add angular example
Oct 9, 2017
6eab4d9
update logic for title and desc creation/adaption
Oct 9, 2017
bd18b74
update examples to test new `title`, `desc`, `aria-labelledby` and `a…
Oct 9, 2017
6de7b44
make the `update-version-comment` script work on windows as well
Oct 9, 2017
01e4044
2.1.2
Oct 9, 2017
7b60c1f
get rid of `engines` field
Oct 9, 2017
1c7d9ca
2.1.3
Oct 9, 2017
a1c5b79
replace `yarn.lock` by `package-lock.json`
Oct 9, 2017
9345475
fix onDoneCallback for empty array
pga78 Oct 25, 2021
d1de008
mind if onDoneCallback defined
pga78 Oct 25, 2021
3c6d868
Merge pull request #29 from pga78/master
flobacher Nov 30, 2021
73e5a69
2.1.4
flobacher Nov 30, 2021
324d99d
update scripts
flobacher Nov 30, 2021
6a9cda1
document scripts
flobacher Nov 30, 2021
330b01b
fix update version script
flobacher Nov 30, 2021
8c3d59a
update dependency
flobacher Nov 30, 2021
2717a72
2.1.5
flobacher Nov 30, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = false


[*]

# Change these settings to your own preference
indent_style = space
indent_size = 2

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
12 changes: 10 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
node_modules
.tmp
#dirs
.idea
.vscode

.DS_Store

node_modules


#files
npm-debug.log
10 changes: 9 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@
"node": true,
"jquery": false,
"strict": false,
"sub": true,
"globals": {
"angular":true,
"define":true,
"app": true,
"ignore": true
"ignore": true,
"SVGSymbolElement": false,
"SVGViewElement": false,
"SVGUseElement": false,
"SVGSVGElement" :false,
"Document" :false
}
}
14 changes: 14 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#dirs
.idea
.DS_Store

tests
examples
src


#files
.jshintrc
.editorconfig
bower.json
npm-debug.log
435 changes: 379 additions & 56 deletions CHANGELOG.md

Large diffs are not rendered by default.

193 changes: 134 additions & 59 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
SVGInjector
=========

A fast, caching, dynamic inline SVG DOM injection library. Developed by [Waybury](http://waybury.com/) for use in [iconic.js](https://useiconic.com/tools/iconic-js/), part of the [Iconic](https://useiconic.com/) icon system.
A fast, caching, dynamic inline SVG DOM injection library. Originally developed by [Waybury](http://waybury.com/) for use in [iconic.js](https://useiconic.com/tools/iconic-js/), part of the [Iconic](https://useiconic.com/) icon system.
Extended by Flobacher to be able to use with spritesheets and in AngularJS Applications

## Why?
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.
Linking to an external SVG on a page (via `object`, `embed`, `iframe`, `img`, CSS `background-image`) usually only allows you to display the SVG 'as is'. To unlock the full potential of SVG, including full element-level CSS styling and evaluation of embedded JavaScript, the markup of the SVG 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 `img` tag elements (or other tag of your choosing) and does the heavy lifting of swapping in the SVG markup inline for you.
Maintaining a bunch of inline SVG on your pages isn't anyone's idea of good time, so **SVGInjector** lets you specify external SVGs and embeds the contents directly into the DOM alongside your HTML.

## How?
* Any DOM element, or array of elements, passed to **SVGInjector** with an SVG file `src` or `data-src` attribute will be replaced with the full SVG markup inline. The async loaded SVG is also cached so multiple uses of an SVG only requires a single server request.
* Any DOM element or array of elements (recommended are `svg`-tags for clarity or `img`-tags if you need support for fallback pngs, but there are no technical limitations), passed to **SVGInjector** that contains a `data-src` attribute will be replaced with the full SVG markup available via this URL inline. The async loaded SVG is also cached so multiple uses of an SVG only requires a single server request.

* Any embedded JavaScript in the SVG will optionally be extracted, cached and evaluated.

Expand All @@ -20,22 +21,23 @@ Wrangling and maintaining a bunch of inline SVG on your pages isn't anyone's ide

## Install

> **SVGInjector** is compatible with:
> **SVGInjector2** is compatible with:
* [CommonJS](http://commonjs.org/) via `module.exports` for use with [Browserify](http://browserify.org/) or [Node](http://nodejs.org/)/[PhantomJS](http://phantomjs.org/)
* [AMD API](https://github.com/amdjs/amdjs-api/blob/master/AMD.md) usage with [RequireJS](http://requirejs.org/)
* Plain ol' JavaScript via creation of a global function

* AngularJS

### npm

npm install svg-injector
npm install svg-injector-2

### Bower

bower install svg-injector
bower install svg-injector-2

### Manually

Download the [dist/svg-injector.min.js](https://github.com/iconic/SVGInjector/blob/master/dist/svg-injector.min.js) file from this repository and add it to your project.
Download the [dist/svg-injector.min.js](https://github.com/flobacher/SVGInjector2/blob/master/dist/svg-injector.min.js) file from this repository and add it to your project.


## Usage
Expand All @@ -48,47 +50,92 @@ Include the **SVGInjector** script on your page.
<script src="svg-injector.min.js"></script>
```

Add some SVG `img` tags.
Add some `svg` tags.

```html
<img class="inject-me" src="image-one.svg">
<img class="inject-me" src="image-two.svg">
<svg data-src="image-one.svg" />
<svg data-src="image-two.svg" />
```

Inject 'em.

```html
<script>
// Elements to inject
var mySVGsToInject = document.querySelectorAll('img.inject-me');

// Do the injection
SVGInjector(mySVGsToInject);
// Do the injection for the selected Nodelist
new SVGInjector().inject(document.querySelectorAll('svg[data-src]'));
</script>
```

The `img` tags have now been replaced with the full SVG markup.
The `svg` tags have now been replaced with the full SVG markup.

Also see [examples/simple](https://github.com/flobacher/SVGInjector2/blob/master/examples/simple.html) for another example that illustrates how to inject `svg` elements using `data-src` with a specific classname such as `inject-me`.

### Configuration

In addition to passing elements to inject, an options object and callback function can optionally be defined.
### Full Example incl. Configuration

```js
SVGInjector(elements, options, callback);
In addition to passing elements to inject, an options object to configure the injector instance and callback functions for the injections can optionally be defined.


```html
<svg id="image-one" data-src="image-one.svg">
<svg id="image-two" data-src="image-two.svg">
```

#### `elements`
```js
// Elements to inject
var elementsToInject = document.querySelectorAll('svg[data-src]');

// Options
var injectorOptions = {
evalScripts: 'once',
pngFallback: 'assets/png'
};

var afterAllInjectionsFinishedCallback = function (totalSVGsInjected) {
// Callback after all SVGs are injected
console.log('We injected ' + totalSVGsInjected + ' SVG(s)!');
};

var perInjectionCallback = function (svg) {
// Callback after each SVG is injected
console.log('SVG injected: ' + svg);
};

A single DOM element or array of elements, with `src` or `data-src` attributes defined, to inject.
// create injector configured by options
var injector = new SVGInjector(injectorOptions);

// Trigger the injection
injector.inject(
elementsToInject,
afterAllInjectionsFinishedCallback,
perInjectionCallback
);
```
Also see [examples/everything](https://github.com/flobacher/SVGInjector2/blob/master/examples/everything.html) for more details.

#### `options`

```js
// default values as comment after the line
{
evalScripts: [always|once|never],
pngFallback: [PNG directory],
each: [function]
evalScripts: 'always'|'once'|'never', // 'always'
pngFallback: 'url to directory with fallbackpngs', // false
keepStylesClass: 'className', // ''
spriteClassName: 'className', // 'sprite'
spriteClassIdName: 'classNameInclSeperator', // 'sprite--'
removeStylesClass: 'className', // 'icon' -> this will remove all styles from the svg so it is easy to style with external css. works best with monochrome icons
removeAllStyles: true|false, // false
fallbackClassName: 'className', // 'sprite'
prefixStyleTags: true|false, // true

// spritesheet relevant options
spritesheetURL: 'urlToSpritesheet', false // only needed for classbased injection
onlyInjectVisiblePart: true|false, // true -> if the fragmentId specifies an svg-view element, only inject the part that is visible due to the specified view
svgFallbackDir: 'urlToFragmentsThatMightNotBeInSpritesheetAnymore', // false -> it can make sense to remove fragments from a spritesheet after some time.. to be sure that nothin breaks, they can be put as single files to a fallback directory and injector tries to find them there


// testing
forceFallbacks: true|false, // false
}
```

Expand All @@ -102,60 +149,88 @@ A single DOM element or array of elements, with `src` or `data-src` attributes d

* `pngFallback` - String

The directory where fallback PNGs are located for use if the browser doesn't [support SVG](http://caniuse.com/svg). This will look for a file with a `.png` extension matching the SVG filename defined in the `src` (or `data-src`).
The directory where fallback PNGs are located for use if the browser doesn't [support SVG](http://caniuse.com/svg). This will look for a file with a `.png` extension matching the SVG filename defined in the `data-src`. For this to work, use `img` instead of `svg` elements.

For additional flexibility, per-element fallbacks are also [available](#per-element-png-fallback).

* `each(svg)` - function

A function to call after each SVG is injected. Receives the newly injected SVG DOM element as a parameter.
#### `elements`

#### `callback(count)` - function
A single DOM element or array of elements, with `data-src` attributes defined, to inject.

A function to call once all the requested SVG elements have been injected. Receives a count of the total SVGs injected as a parameter.

### Full Example
#### `perInjectionCallback(svg)` - function

```html
<img id="image-one" class="inject-me" data-src="image-one.svg">
<img id="image-two" class="inject-me" data-src="image-two.svg">
```
A function to call after each SVG is injected. Receives the newly injected SVG DOM element as a parameter.

```js
// Elements to inject
var mySVGsToInject = document.querySelectorAll('img.inject-me');
#### `afterAllInjectionsFinishedCallback(count)` - function

A function to call once all the requested SVG elements have been injected. Receives a count of the total SVGs injected as a parameter.

// Options
var injectorOptions = {
evalScripts: 'once',
pngFallback: 'assets/png',
each: function (svg) {
// Callback after each SVG is injected
console.log('SVG injected: ' + svg.getAttribute('id'));
}
};

// Trigger the injection
SVGInjector(mySVGsToInject, injectorOptions, function (totalSVGsInjected) {
// Callback after all SVGs are injected
console.log('We injected ' + totalSVGsInjected + ' SVG(s)!');
});
```

### Per-element PNG fallback

Since you might be using a single SVG styled in multiple ways, you can also define per-element fallbacks by adding a `data-fallback` or `data-png` attribute to your `img` tags to define a unique PNG for each context.
Since you might be using a single SVG styled in multiple ways, you can also define per-element fallbacks by adding a `data-fallback` or `data-png` attribute to your `img` tags (remember for png fallbacks to work you need to use img-tags) to define a unique PNG for each context.

See [examples/fallbacks](https://github.com/iconic/SVGInjector/tree/master/examples/fallbacks) for more details.
See [examples/fallbacks](https://github.com/flobacher/SVGInjector2/blob/master/examples/fallbacks.html) for more details.

```html
<style>
.thumb-green {fill: #A6A93C;}
</style>
<img class="thumb-green inject-me" data-src="svg/thumb-up.svg" data-fallback="png/thumb-up-green.png">
<img class="thumb-green inject-me" data-src="svg/thumb-up.svg" data-fallback="png/thumb-up-green.png" />

```

### Spritesheets

To save http requests, you can combine your svgs to a spritesheet, where every single svg is represented
as a `<symbol>` or an `<svg>`. To inject the symbol with the id `thumb-up` from a spritesheet, on the element where you want
the injection to happen write : `data-src = "url-to-spritesheet.svg#thumb-up"`.
Besides spritesheets consisting of symbols, you can also use Spritesheets that specify `<view>` elements with
ids (it is possible to create a fallback-png spritesheet for those). The injector will try to find the symbol/svg element
via comparing its viewbox to that of the view. If no png fallback is needed, the first approach is the most
prefereable. If using nodejs build-tools like gulp or grunt, take a look at [svg-sprite](https://github.com/jkphl/svg-sprite).
See [examples/spritesheet](https://github.com/flobacher/SVGInjector2/blob/master/examples/angular-spritesheet.html) for more details.

#### Classbased fragment ids

When using spritesheets, having to type the same data-src=urltospritesheet.svg#fragmentid can become cumbersome.
Thats why there is a config options that allows to set a default url to a spritesheet. The fragment id can then be provided via
a simple class.
See [examples/fallbacks](https://github.com/flobacher/SVGInjector2/blob/master/examples/spritesheet-classbased.html) for more details.
There is an additional option to add a fallback svg in case the fragmentid cannot be found in the spritesheet.
Specify it via `data-fallback-svg = "url-to-fallback-image.svg"`

### AngularJS

SVGInjector is also available as configurable Service / Directive combination.
The svg-element-directive will automatically inject the svg. Just define the `svginjector` module as a dependency of your module.
See [examples/angular-simple](https://github.com/flobacher/SVGInjector2/blob/master/examples/angular-simple.html) for more details.

To configure the SVGInjector Service use the svgInjectorOptionsProvider available during the config-phase and use it to set
the options-object. For details
see [examples/angular-spritesheet](https://github.com/flobacher/SVGInjector2/blob/master/examples/angular-spritesheet.html).
This example also demonstrates, that of course the angular wrapper can also be used in combination with a spritesheet.

Last but not least check [examples/angular-spritesheet-classbased](https://github.com/flobacher/SVGInjector2/blob/master/examples/angular-spritesheet-classbased.html) to see the convenience the use of classbased injection offers.


#### Performance tip
add
```html
<link rel="prefetch" href="(pathToSpritesheet)"/>
```
to let the browser download the file, even before it was requested via xhr

### Accessibility
`role="img"` and `aria-labelledby` gets added to the root svg element. `aria-labelledby` points to the id of the `desc` and `title` tags inside the injected svg.
The contents of those tags comes from corresponding tags inside the element which is the injection target, if not found there, they are taken from the svg-instance that gets
injected, and if missing there as well defaults to the fragmentid or filename of the injectable. `role="presentation"` gets added to all elements
in all proper browsers (so no IE).



# Licence
The MIT License (MIT)
Expand Down
10 changes: 5 additions & 5 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "svg-injector",
"version": "1.1.2",
"homepage": "https://github.com/iconic/SVGInjector",
"name": "svg-injector-2",
"homepage": "https://github.com/flobacher/SVGInjector2",
"authors": [
"Iconic <[email protected]>"
"Iconic <[email protected]>",
"Flo Bacher <[email protected]>"
],
"description": "Fast, caching, dynamic inline SVG DOM injection library",
"main": "svg-injector.js",
"main": "dist/svg-injector.min.js",
"moduleType": [
"node",
"amd",
Expand Down
Loading