Skip to content

Commit

Permalink
fix SVG gradient rendering for iOS WebViews (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored and James committed Oct 21, 2019
1 parent 7fe62e2 commit c6796c0
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
### Changelog

#### 4.5.3

##### Fixes

* Further fixes to UI gradient rendering in cases where iro.js is used in a page where a `<base>` tag is also present.

#### 4.5.2

##### Fixes
Expand Down
17 changes: 9 additions & 8 deletions dist/iro.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/iro.es.js.map

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions dist/iro.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/iro.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/iro.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/iro.min.js.map

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions docs/.vuepress/theme/js/iro.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jaames/iro",
"version": "4.5.2",
"version": "4.5.3",
"description": "An HSV color picker widget for JavaScript, with a modern SVG-based user interface",
"module": "dist/iro.es.js",
"main": "dist/iro.js",
Expand Down
13 changes: 7 additions & 6 deletions src/util/svg.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Keep track of html <base> elements for resolveUrl
// getElementsByTagName returns a live HTMLCollection, which stays in sync with the DOM tree
// So it only needs to be called once
const bases = document.getElementsByTagName('base');

/**
* @desc Resolve an SVG URL
* This is required to work around how Safari handles gradient URLS under certain conditions
Expand All @@ -7,9 +12,7 @@
* https://stackoverflow.com/questions/19742805/angular-and-svg-filters/19753427#19753427
* https://github.com/jaames/iro.js/issues/18
* https://github.com/jaames/iro.js/issues/45
* There's also a secondary issue with using absolute SVG gradient URLs in Ionic, as the
* Ionic Webview plugin changes location.protocol to "ionic://" which breaks URL resolution
* https://github.com/jaames/iro.js/issues/45#issuecomment-542949642
* https://github.com/jaames/iro.js/pull/89
* @param {String} url resource url (should be an id selector e.g "#example")
* @returns {String} resolved url
*/
Expand All @@ -19,9 +22,7 @@ export function resolveUrl(url) {
const ua = window.navigator.userAgent;
const isSafari = /^((?!chrome|android).)*safari/i.test(ua);
const isIos = /iPhone|iPod|iPad/i.test(ua);
// Sniff protocol string to check if iro.js is running in an Ionic webview
const isIonic = /ionic/i.test(location.protocol);
return ((isSafari || isIos) && (!isIonic)) ? `${location.protocol}//${location.host}${location.pathname}${location.search}${url}` : url;
return ((isSafari || isIos) && (bases.length > 0)) ? `${location.protocol}//${location.host}${location.pathname}${location.search}${url}` : url;
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/util.svg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('resolveUrl', () => {

test('resolveUrl correctly resolves full URL when a Safari userAgent is present', () => {
Object.defineProperty(window.navigator, 'userAgent', {value: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15'});
document.head.innerHTML = '<base href="/" />';
expect(resolveUrl('#test')).toBe('http://localhost/#test');
window.history.pushState({}, '', '/example');
expect(resolveUrl('#test')).toBe('http://localhost/example#test');
Expand Down

0 comments on commit c6796c0

Please sign in to comment.