Git Product home page Git Product logo

polyfill-svg-uri's Introduction

polyfillSvgUri

npm GitHub issues dependencies license

Polyfill for plain SVG as Data URI scheme.

The plain SVG without encoding (e.g. Base64, URL encoding, etc.) that is written in the Data URI scheme is easy to read, easy to edit and small size.
That Data URI scheme is written in for example, background-image, list-style-image, cursor CSS properties, src, data attributes of <img>, <input>, <iframe>, <object> elements, etc..

div {
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="96" height="96"><path d="M10,10L32,90L90,32z" fill="lightgreen"/></svg>') center no-repeat;
}

But IE ignores it, and some browsers consider # as the hash.
This polyfill solves those problems. It passes encoded SVG to IE, and it escapes #.

Usage

<script src="polyfill-svg-uri.min.js"></script>

Supported Places

The plain SVGs as Data URI scheme are enabled in:

CSS styles in stylesheets

For example: <link> and <style> tags

CSS styles in inline-style

For example:

<div style="background-image: url('data:image/svg+xml;utf8,<svg>...')">

src and data attributes of <img>, <input>, <iframe>, <object> tags, etc.

For example:

<img src="data:image/svg+xml;utf8,<svg>...">

CSS properties of elment.style

For example:

document.getElementById('media-1').style.backgroundImage =
  'url(\'data:image/svg+xml;utf8,<svg>...\')';

elment.style.cssText property

For example:

document.getElementById('media-1').style.cssText =
  'background-image: url(\'data:image/svg+xml;utf8,<svg>...\');';

elment.style.setProperty method

For example:

document.getElementById('media-1').style.setProperty(
  'background-image', 'url(\'data:image/svg+xml;utf8,<svg>...\')');

New CSS rules

For example:

var styleSheet = document.styleSheets[0],
  selector = '#media-1',
  cssText = 'background-image: url(\'data:image/svg+xml;utf8,<svg>...\');';
if (styleSheet.insertRule) {
  styleSheet.insertRule(selector + '{' + cssText + '}', 0);
} else if (styleSheet.addRule) {
  styleSheet.addRule(selector, cssText);
}

Existing CSS rules

For example:

document.styleSheets[0].cssRules[0].style.backgroundImage =
  'url(\'data:image/svg+xml;utf8,<svg>...\')';

elment.src and elment.data properties

For example:

document.getElementById('media-1').src =
  'data:image/svg+xml;utf8,<svg>...';

polyfill-svg-uri's People

Contributors

anseki avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

polyfill-svg-uri's Issues

Deprecation warning in Google Chrome

polyfill-svg-uri.min.js:1 [Deprecation] 'webkitURL' is deprecated. Please use 'URL' instead.

polyfill-svg-uri.min.js:1 [Deprecation] 'window.webkitStorageInfo' is deprecated. Please use 'navigator.webkitTemporaryStorage' or 'navigator.webkitPersistentStorage' instead.

Please help.
Thanks in Advance

DOMException: Failed to read the 'cssRules'

@anseki Hello. I had faced with some problem:

DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules

Error in parseRule function.

I get this error in case when I have a link for external resources (like google fonts).
This problem related to browser CORS policy. I've fixed this problem locally with using of try/catch block, but I not sure that this fix is the better option.

Would you make updates, please?
Or I can create PR with my fix :)

reduce deprecated warnings

Hi,

Sorry to bug you again, but do you know if there is a good way to disable deprecated warnings?

image

I think they occur because they exist as property and are accessed.

Could we fix this by first checking if a property exists without the webkit prefix and if so, skip it?
Then if used on an old browser it would still process the prefixed one, and on a newer browser, it would process the non-deprecated one.

It only won't work for cases where you ignore the deprecated warning for a case where the property contains an SVG. Not sure if those exist, in my case, they don't I think but don't know about all the cases.

Or is this something that should be fixed outside of the polyfill by overriding the console.warm behavior and ignoring them?

Thanks,

Erik

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.