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

Use an anchor tag for parsing URLs instead of a regex #1046

Merged
merged 3 commits into from Dec 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/ajax.js
Expand Up @@ -184,13 +184,17 @@

$.ajax = function(options){
var settings = $.extend({}, options || {}),
deferred = $.Deferred && $.Deferred()
deferred = $.Deferred && $.Deferred(),
urlAnchor
for (key in $.ajaxSettings) if (settings[key] === undefined) settings[key] = $.ajaxSettings[key]

ajaxStart(settings)

if (!settings.crossDomain) settings.crossDomain = /^([\w-]+:)?\/\/([^\/]+)/.test(settings.url) &&
RegExp.$2 != window.location.host
if (!settings.crossDomain) {
urlAnchor = document.createElement('a')
urlAnchor.href = settings.url
settings.crossDomain = (window.location.protocol + '//' + window.location.host) !== (urlAnchor.protocol + '//' + urlAnchor.host)
}

if (!settings.url) settings.url = window.location.toString()
serializeData(settings)
Expand Down
58 changes: 56 additions & 2 deletions test/ajax.html
Expand Up @@ -832,7 +832,7 @@ <h1>Zepto Ajax unit tests</h1>
overrideMimeType: function(type) {
this.responseHeaders['content-type'] = type
},
withCredentials: false,
withCredentials: false,
send: function(data) {
this.data = data
},
Expand Down Expand Up @@ -881,7 +881,7 @@ <h1>Zepto Ajax unit tests</h1>
t.assertEqual(window.location, MockXHR.last.url)
},

testCrossDomain: function(t) {
testCrossDomainCrossOrigin: function(t) {
$.ajax({
url: 'http://example.com/foo',
beforeSend: function(xhr, settings) {
Expand All @@ -890,6 +890,60 @@ <h1>Zepto Ajax unit tests</h1>
})
},

testCrossDomainSameOrigin: function(t) {
$.ajax({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to see these as separate test cases?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in 295c021. The test names might need some attention still.

url: window.location.href,
beforeSend: function(xhr, settings) {
t.assertFalse(settings.crossDomain)
}
})
},

testCrossDomainLeadingSpace: function(t) {
$.ajax({
url: ' http://example.com/foo',
beforeSend: function(xhr, settings) {
t.assertTrue(settings.crossDomain)
}
})
},

testCrossDomainTrippleSlash: function(t) {
$.ajax({
url: 'http:///example.com/foo',
beforeSend: function(xhr, settings) {
t.assertTrue(settings.crossDomain)
}
})
},

testCrossDomainWithQuery: function(t) {
$.ajax({
url: window.location.href + "?foo=bar",
beforeSend: function(xhr, settings) {
t.assertFalse(settings.crossDomain)
}
})
},

testCrossDomainDifferentScheme: function(t) {
$.ajax({
url: "foo://" + window.location.host,
beforeSend: function(xhr, settings) {
t.assertTrue(settings.crossDomain)
}
})
},

testCrossDomainPeriodInScheme: function(t) {
$.ajax({
url: "foo.bar://example.com/foo",
beforeSend: function(xhr, settings) {
t.assertTrue(settings.crossDomain)
}
})
},

testDefaultAcceptHeader: function(t) {
$.ajax()
t.assert(MockXHR.last.headers.some(matchHeader('Accept', '*/*')))
Expand Down