Giter Site home page Giter Site logo

inline-svg's Introduction

Inline SVG

Build Status Code Climate npm Bower Dependency Status

Takes an inline <img> with an SVG as its source and swaps it for an inline <svg> so you can manipulate the style of it with CSS/JS etc.

How to use

Add the Inline SVG script to your page and initialise the script. You can currently pass three options to the script: svgSelector and initClass. If these are left out the script will use the defaults.

<script src="inlineSVG.min.js"></script>
<script>
inlineSVG.init({
  svgSelector: 'img.svg', // the class attached to all images that should be inlined
  initClass: 'js-inlinesvg', // class added to <html>
}, function () {
  console.log('All SVGs inlined');
});
</script>

The callback is optional and is only fired if all the images in the selection are successfully inlined. On the other hand the initClass is applied after the first successful replacement.

The script will look for any <img> with a class that matches the svgSelector parameter and replace it with the SVG's source. Doing this enables you to manipulate the SVG with CSS and JavaScript.

<img id="logo" class="svg" src="/images/logo.svg" alt="Some awesome company" />
svg:hover path {
  fill: #c00;
}

// or
#logo:hover path {
  fill: #c00;
}

Any additional attributes (height, width, data-*, etc) will be copied to the SVG. For increased accessibility the script will also copy across the <img> alt text and add in an aria-labelledby attribute and <title> element to the SVG. If you give the <img> a longdesc attribute, a <desc> will also be added as per the W3C's guidelines on SVG accessibility.

For a live demo check out this example on CodePen. The demo is still there it's just way out of date and needs updating.

Performance

By default, the image will be requested twice, once for initial load and secondly for requesting the SVG markup. A more efficent way to set this up would be to have your image src in a data-src attribute, this will prevent the initial request of the image.

<img id="logo" class="svg" data-src="/images/logo.svg" alt="Some awesome company" />

The most harmful side effect of implementing Inline SVG this way is that if your Javascript fails or otherwise doesn't run, the image won't show at all. In the default setup, the image would still load.

Bower

If you're using Bower to manage your front-end dependencies you can include this plugin as a component. Include "inline-svg": "2.2.3" in your bower.json file and run bower install.

NPM

If you're using NPM to manage your dependencies you can include this plugin as a module. Just run npm install --save-dev inline-svg. You can then require the plugin as shown below.

var inlineSVG = require('inline-svg');

inlineSVG.init({
  svgSelector: 'img.svg', // the class attached to all images that should be inlined
  initClass: 'js-inlinesvg', // class added to <html>
}, function () {
  console.log('All SVGs inlined');
});

Changelog

  • 05/05/16: 2.2.2 – Updated documentation.
  • 05/05/16: 2.2.2 – Fixed callback error if one is not defined.
  • 16/10/15: 2.2.1 – Fix for adding the initClass to the body each time an SVG is inlined.
  • 23/09/15: 2.2.0 – Fix in package.json for main field when using as a module and added callback support to know when replacement is complete.
  • 21/09/15: 2.1.5 – Removing to code that should never have made it to release.
  • 21/09/15: 2.1.4 – Version bump.
  • 21/09/15: 2.1.3 – Version bump.
  • 26/08/15: 2.1.2 – Removed localStorage. It just doesn't work that well when SVG's change etc.
  • 31/07/15: 2.1.1 – Added localStorage support to avoid making fresh HTTP request on every page load. When the contents of the SVG is loaded it is added to localStorage and then on repeat page loads the source is grabbed from localStorage.
  • 31/07/15: 2.0.1 - Major upgrade. Added AMD support and fixed a long standing issue that would result in a warning in Google Chrome as we weren't handling the GET requests asynchronously.
  • 18/06/15: 1.2.0 – Converted to a Node.js module
  • 19/03/15: 1.0.5 – Cleaning code to comply with Code Climate
  • 16/12/14: 1.0.4 – Updated README with new CodePen demo and added an extra line regarding browser support. Changed aria-label to aria-labelledby and also added role="img" for better accessibility.
  • 15/12/14: 1.0.3 – Updated bower.json version number.
  • 15/12/14: 1.0.2 – README updates to explain browser support.
  • 15/12/14: 1.0.1 – README updates to explain Bower integration.
  • 15/12/14: 1.0.0 – First major release: registered as a Bower plugin.

inline-svg's People

Contributors

stephan-fischer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

inline-svg's Issues

Wrong handling of `longdesc`

Your code copies the content of the longdesc attribute into the desc element.

However, the longdesc attribute shall contain "A link to a more detailed description of the image. Possible values are a URL or an element id" (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes:~:text=CSS%20property%20instead.-,longdesc,-Deprecated), while "The desc element provides an accessible, long-text description of any SVG" (https://developer.mozilla.org/en-US/docs/Web/SVG/Element/desc).

Completely different kind of content. The longdesc attribute is a link, not a text.

Callback Doesn't Ever Fire

It appears as if the callback never actually fires, utilizing the following syntax:

inlineSVG.init({
      svgSelector: INLINE_SVG_OPTIONS.selector,
      initClass: INLINE_SVG_OPTIONS.initClass,
    }, () => {
      console.log('ever getting here?');
});

Thoughts on this?

Could you please tag version 2.2.3?

The README suggests specifying this version in bower.json, and, though it's been updated in your bower.json, the repository hasn't been tagged. bower install outputs:

bower inline-svg#2.2.3 ENORESTARGET No tag found that was able to satisfy 2.2.3

Additional error details:
Available versions in https://github.com/jonnyhaynes/inline-svg.git: 2.2.1, 2.2.0, 2.1.5, 2.1.4, 2.1.3, 2.1.2, 2.1.1, 2.1.0, 2.0.1, 2.0.0, 1.2.0, 1.0.5, 1.0.4, 1.0.3, 1.0.2, 1.0.1, 1.0.0

<svg> className is removed

If the <svg> tag in the image being loaded has a class, this will be overridden when the SVG is inlined:

SVG file

<svg class="someclass" xmlns="http://www.w3.org/2000/svg" width="53" height="27" viewBox="0 0 53 27">
    <circle cx="26.5" cy="13.5" r="8" fill="#FFF" fill-rule="evenodd" />
</svg>

Expected outcome

<svg class="someclass svg inlined-svg" xmlns="http://www.w3.org/2000/svg" width="53" height="27" viewBox="0 0 53 27">
    <circle cx="26.5" cy="13.5" r="8" fill="#FFF" fill-rule="evenodd" />
</svg>

Actual outcome

<svg class="svg inlined-svg" xmlns="http://www.w3.org/2000/svg" width="53" height="27" viewBox="0 0 53 27">
    <circle cx="26.5" cy="13.5" r="8" fill="#FFF" fill-rule="evenodd" />
</svg>

I know as a workaround I can define the class in the <img> tag and it'll be retained, but this seems redundant/inconvenient.

Cannot read property 'removeAttribute' of undefined

I'm having some errors while using this library. It seems to be working anyways so far.

Cannot read property 'removeAttribute' of undefined
Line 121

inlinedSVG = result.getElementsByTagName('svg')[0];

// Remove some of the attributes that aren't needed
inlinedSVG.removeAttribute('xmlns:a');

There should be a check if( inlinedSVG ) actually exists.

Support for SVGs stored on AWS

Hello!
I am trying to find a solution to allow inline insertion of SVG graphics which are stored on a AWS S3 process. I am able to get inline-svg working but if my img tag refers to the AWS URL then the SVG is not inserted inline, its left as an image tag (the svg is visible, just not inline). Is there any way to get inline-svg.js to work with files stored on AWS?

Accessibility

The older jQuery code had the ability to add in an aria-label and title attribute to the resulting SVG, the new non-jQuery version doesn't currently add in the title attribute as per the W3C spec.

Uncaught ReferenceError: callback is not defined

If I use your plugin like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="/node_modules/inline-svg/dist/inlineSVG.min.js"></script>
  <script>
    inlineSVG.init({
      svgSelector: 'img.svg', // the class attached to all images that should be inlined
      initClass: 'js-inlinesvg', // class added to <html>
      storeResults: false // should the results be stored in localStorage
    }, callback);
  </script> 

I receive:

Uncaught ReferenceError: callback is not defined

Why? If I remove the callback it works. Is this really needed?

SVG style issue

If we are convert two svg images having same classes. One having embedded style tag then other svg also using the same styles that are used in first svg image.

Aria labelledby not pointing to correct value

aria-labelledby should point to an ID, but instead it is pointing to the title tag. This leads to a broken aria reference.

Additionally, aria-labelledby shouldn't be added if no alt attribute is present. Can consider adding role="presentation" and aria-hidden="true" in cases where no alt is present. As this tells screen readers to ignore it, similar to when an empty alt is present on img tags

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.