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

163 IE11 bug when absolute URL is used #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
40 changes: 36 additions & 4 deletions nette.ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ if (typeof $ !== 'function') {
return console.error('nette.ajax.js: jQuery is missing, load it please');
}

// Inspired by https://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript
var getLocation = function(href) {
var k = ['protocol', 'hostname', 'host', 'pathname', 'port', 'search', 'hash', 'href'];
var a = document.createElement('a');

a.href = href;

// IE doesn't populate all link properties when setting .href with a relative URL,
// however .href will return an absolute URL which then can be used on itself
// to populate these additional fields.
if (a.host === '') {
a.href = a.href;
}

for (var r = {}, i = 0; i < k.length; i++) {
r[k[i]] = a[k[i]];
}

// IE doesn't return the leading / in pathname
if (r.pathname === '') {
r.pathname = '/';
} else if (r.pathname[0] !== '/') {
r.pathname = '/' + r.pathname;
}

r.toString = function() { return a.href; };

return r;
};

var nette = function () {
var inner = {
self: this,
Expand Down Expand Up @@ -335,8 +365,10 @@ $.nette.ext('validation', {
// Check if URL is absolute
if (/(?:^[a-z][a-z0-9+.-]*:|\/\/)/.test(urlToValidate)) {
// Parse absolute URL
var parsedUrl = new URL(urlToValidate);
if (/:|^#/.test(parsedUrl['pathname'] + parsedUrl['search'] + parsedUrl['hash'])) return false;
var parsedUrl = getLocation(urlToValidate);
if (location.pathname === parsedUrl.pathname && location.search === parsedUrl.search && parsedUrl.hash) {
return false;
}
} else {
if (/:|^#/.test(urlToValidate)) return false;
}
Expand Down Expand Up @@ -400,7 +432,7 @@ $.nette.ext('forms', {
data[name + '.y'] = dataOffset[1];
}
}

// https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects#Sending_files_using_a_FormData_object
var formMethod = analyze.form.attr('method');
if (formMethod && formMethod.toLowerCase() === 'post' && 'FormData' in window) {
Expand All @@ -414,7 +446,7 @@ $.nette.ext('forms', {
formData.append(i, originalData[i]);
}
}

// remove empty file inputs as these causes Safari 11 to stall
// https://stackoverflow.com/questions/49672992/ajax-request-fails-when-sending-formdata-including-empty-file-input-in-safari
if (formData.entries && navigator.userAgent.match(/version\/11(\.[0-9]*)? safari/i)) {
Expand Down