Permalink
Browse files

Fix bug #62: Handle various kinds of relative URLs

This is still not a full URL parser as in http://url.spec.whatwg.org/
but should be good enough.
  • Loading branch information...
1 parent 1efce10 commit 4ba0103fe1f81cd0835fd54637b73d1ebdc62cc5 @SimonSapin committed Dec 10, 2012
Showing with 30 additions and 4 deletions.
  1. +17 −3 prefixfree.js
  2. +13 −1 prefixfree.min.js
View
@@ -24,6 +24,9 @@ var self = window.StyleFix = {
var url = link.href || link.getAttribute('data-href'),
base = url.replace(/[^\/]+$/, ''),
+ base_scheme = (/^[a-z]{3,10}:/.exec(base) || [''])[0],
+ base_domain = (/^[a-z]{3,10}:\/\/[^\/]+/.exec(base) || [''])[0],
+ base_query = /^([^?]*)\??/.exec(url)[1],
parent = link.parentNode,
xhr = new XMLHttpRequest(),
process;
@@ -43,12 +46,23 @@ var self = window.StyleFix = {
// Convert relative URLs to absolute, if needed
if(base) {
css = css.replace(/url\(\s*?((?:"|')?)(.+?)\1\s*?\)/gi, function($0, quote, url) {
- if(!/^([a-z]{3,10}:|\/|#)/i.test(url)) { // If url not absolute & not a hash
+ if(/^([a-z]{3,10}:|#)/i.test(url)) { // Absolute & or hash-relative
+ return $0;
+ }
+ else if(/^\/\//.test(url)) { // Scheme-relative
// May contain sequences like /../ and /./ but those DO work
+ return 'url("' + base_scheme + url + '")';
+ }
+ else if(/^\//.test(url)) { // Domain-relative
+ return 'url("' + base_domain + url + '")';
+ }
+ else if(/^\?/.test(url)) { // Query-relative
+ return 'url("' + base_query + url + '")';
+ }
+ else {
+ // Path-relative
return 'url("' + base + url + '")';
}
-
- return $0;
});
// behavior URLs shoudn’t be converted (Issue #19)
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 4ba0103

Please sign in to comment.