Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed ajax

  • Loading branch information...
commit d84f1d5b0da8c8b035b22ca52b1663943633b869 1 parent 5f735ec
@smith authored
View
315 src/ajax.js
@@ -1,315 +0,0 @@
-var Ajax = {
- getTransport: function() {
- return Try.these(
- function() {return new XMLHttpRequest()},
- function() {return new ActiveXObject('Msxml2.XMLHTTP')},
- function() {return new ActiveXObject('Microsoft.XMLHTTP')}
- ) || false;
- },
-
- activeRequestCount: 0
-};
-
-Ajax.Responders = {
- responders: [],
-
- _each: function(iterator) {
- this.responders._each(iterator);
- },
-
- register: function(responder) {
- if (!this.include(responder))
- this.responders.push(responder);
- },
-
- unregister: function(responder) {
- this.responders = this.responders.without(responder);
- },
-
- dispatch: function(callback, request, transport, json) {
- this.each(function(responder) {
- if (Object.isFunction(responder[callback])) {
- try {
- responder[callback].apply(responder, [request, transport, json]);
- } catch (e) { }
- }
- });
- }
-};
-
-Object.extend(Ajax.Responders, Enumerable);
-
-Ajax.Responders.register({
- onCreate: function() { Ajax.activeRequestCount++ },
- onComplete: function() { Ajax.activeRequestCount-- }
-});
-
-Ajax.Base = Class.create({
- initialize: function(options) {
- this.options = {
- method: 'post',
- asynchronous: true,
- contentType: 'application/x-www-form-urlencoded',
- encoding: 'UTF-8',
- parameters: '',
- evalJSON: true,
- evalJS: true
- };
- Object.extend(this.options, options || { });
-
- this.options.method = this.options.method.toLowerCase();
-
- if (Object.isString(this.options.parameters))
- this.options.parameters = this.options.parameters.toQueryParams();
- else if (Object.isHash(this.options.parameters))
- this.options.parameters = this.options.parameters.toObject();
- }
-});
-
-Ajax.Request = Class.create(Ajax.Base, {
- _complete: false,
-
- initialize: function($super, url, options) {
- $super(options);
- this.transport = Ajax.getTransport();
- this.request(url);
- },
-
- request: function(url) {
- this.url = url;
- this.method = this.options.method;
- var params = Object.clone(this.options.parameters);
-
- if (!['get', 'post'].include(this.method)) {
- // simulate other verbs over post
- params['_method'] = this.method;
- this.method = 'post';
- }
-
- this.parameters = params;
-
- if (params = Object.toQueryString(params)) {
- // when GET, append parameters to URL
- if (this.method == 'get')
- this.url += (this.url.include('?') ? '&' : '?') + params;
- else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent))
- params += '&_=';
- }
-
- try {
- var response = new Ajax.Response(this);
- if (this.options.onCreate) this.options.onCreate(response);
- Ajax.Responders.dispatch('onCreate', this, response);
-
- this.transport.open(this.method.toUpperCase(), this.url,
- this.options.asynchronous);
-
- if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1);
-
- this.transport.onreadystatechange = this.onStateChange.bind(this);
- this.setRequestHeaders();
-
- this.body = this.method == 'post' ? (this.options.postBody || params) : null;
- this.transport.send(this.body);
-
- /* Force Firefox to handle ready state 4 for synchronous requests */
- if (!this.options.asynchronous && this.transport.overrideMimeType)
- this.onStateChange();
-
- }
- catch (e) {
- this.dispatchException(e);
- }
- },
-
- onStateChange: function() {
- var readyState = this.transport.readyState;
- if (readyState > 1 && !((readyState == 4) && this._complete))
- this.respondToReadyState(this.transport.readyState);
- },
-
- setRequestHeaders: function() {
- var headers = {
- 'X-Requested-With': 'XMLHttpRequest',
- 'X-Prototype-Version': Prototype.Version,
- 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
- };
-
- if (this.method == 'post') {
- headers['Content-type'] = this.options.contentType +
- (this.options.encoding ? '; charset=' + this.options.encoding : '');
-
- /* Force "Connection: close" for older Mozilla browsers to work
- * around a bug where XMLHttpRequest sends an incorrect
- * Content-length header. See Mozilla Bugzilla #246651.
- */
- if (this.transport.overrideMimeType &&
- (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005)
- headers['Connection'] = 'close';
- }
-
- // user-defined headers
- if (typeof this.options.requestHeaders == 'object') {
- var extras = this.options.requestHeaders;
-
- if (Object.isFunction(extras.push))
- for (var i = 0, length = extras.length; i < length; i += 2)
- headers[extras[i]] = extras[i+1];
- else
- $H(extras).each(function(pair) { headers[pair.key] = pair.value });
- }
-
- for (var name in headers)
- this.transport.setRequestHeader(name, headers[name]);
- },
-
- success: function() {
- var status = this.getStatus();
- return !status || (status >= 200 && status < 300);
- },
-
- getStatus: function() {
- try {
- return this.transport.status || 0;
- } catch (e) { return 0 }
- },
-
- respondToReadyState: function(readyState) {
- var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this);
-
- if (state == 'Complete') {
- try {
- this._complete = true;
- (this.options['on' + response.status]
- || this.options['on' + (this.success() ? 'Success' : 'Failure')]
- || Prototype.emptyFunction)(response, response.headerJSON);
- } catch (e) {
- this.dispatchException(e);
- }
-
- var contentType = response.getHeader('Content-type');
- if (this.options.evalJS == 'force'
- || (this.options.evalJS && this.isSameOrigin() && contentType
- && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i)))
- this.evalResponse();
- }
-
- try {
- (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON);
- Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON);
- } catch (e) {
- this.dispatchException(e);
- }
-
- if (state == 'Complete') {
- // avoid memory leak in MSIE: clean up
- this.transport.onreadystatechange = Prototype.emptyFunction;
- }
- },
-
- isSameOrigin: function() {
- var m = this.url.match(/^\s*https?:\/\/[^\/]*/);
- return !m || (m[0] == '#{protocol}//#{domain}#{port}'.interpolate({
- protocol: location.protocol,
- domain: document.domain,
- port: location.port ? ':' + location.port : ''
- }));
- },
-
- getHeader: function(name) {
- try {
- return this.transport.getResponseHeader(name) || null;
- } catch (e) { return null }
- },
-
- evalResponse: function() {
- try {
- return eval((this.transport.responseText || '').unfilterJSON());
- } catch (e) {
- this.dispatchException(e);
- }
- },
-
- dispatchException: function(exception) {
- (this.options.onException || Prototype.emptyFunction)(this, exception);
- Ajax.Responders.dispatch('onException', this, exception);
- }
-});
-
-Ajax.Request.Events =
- ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
-
-Ajax.Response = Class.create({
- initialize: function(request){
- this.request = request;
- var transport = this.transport = request.transport,
- readyState = this.readyState = transport.readyState;
-
- if((readyState > 2 && !Prototype.Browser.IE) || readyState == 4) {
- this.status = this.getStatus();
- this.statusText = this.getStatusText();
- this.responseText = String.interpret(transport.responseText);
- this.headerJSON = this._getHeaderJSON();
- }
-
- if(readyState == 4) {
- var xml = transport.responseXML;
- this.responseXML = Object.isUndefined(xml) ? null : xml;
- this.responseJSON = this._getResponseJSON();
- }
- },
-
- status: 0,
- statusText: '',
-
- getStatus: Ajax.Request.prototype.getStatus,
-
- getStatusText: function() {
- try {
- return this.transport.statusText || '';
- } catch (e) { return '' }
- },
-
- getHeader: Ajax.Request.prototype.getHeader,
-
- getAllHeaders: function() {
- try {
- return this.getAllResponseHeaders();
- } catch (e) { return null }
- },
-
- getResponseHeader: function(name) {
- return this.transport.getResponseHeader(name);
- },
-
- getAllResponseHeaders: function() {
- return this.transport.getAllResponseHeaders();
- },
-
- _getHeaderJSON: function() {
- var json = this.getHeader('X-JSON');
- if (!json) return null;
- json = decodeURIComponent(escape(json));
- try {
- return json.evalJSON(this.request.options.sanitizeJSON ||
- !this.request.isSameOrigin());
- } catch (e) {
- this.request.dispatchException(e);
- }
- },
-
- _getResponseJSON: function() {
- var options = this.request.options;
- if (!options.evalJSON || (options.evalJSON != 'force' &&
- !(this.getHeader('Content-type') || '').include('application/json')) ||
- this.responseText.blank())
- return null;
- try {
- return this.responseText.evalJSON(options.sanitizeJSON ||
- !this.request.isSameOrigin());
- } catch (e) {
- this.request.dispatchException(e);
- }
- }
-});
-
View
1  src/prototype.js
@@ -14,4 +14,3 @@ var Prototype = {
<%= include 'enumerable.js', 'array.js', 'number.js', 'hash.js', 'range.js' %>
-<%= include 'ajax.js' %>
View
325 test/unit/ajax_test.js
@@ -1,325 +0,0 @@
-var extendDefault = function(options) {
- return Object.extend({
- asynchronous: false,
- method: 'get',
- onException: function(e) { throw e }
- }, options);
-};
-
-var testResults = new Test.Unit.Runner({
- setup: function() {
- $('content').update('');
- $('content2').update('');
- },
-
- teardown: function() {
- // hack to cleanup responders
- Ajax.Responders.responders = [Ajax.Responders.responders[0]];
- },
-
- testSynchronousRequest: function() {
- this.assertEqual("", $("content").innerHTML);
-
- this.assertEqual(0, Ajax.activeRequestCount);
- new Ajax.Request("../fixtures/hello.js", {
- asynchronous: false,
- method: 'GET',
- evalJS: 'force'
- });
- this.assertEqual(0, Ajax.activeRequestCount);
-
- var h2 = $("content").firstChild;
- this.assertEqual("Hello world!", h2.innerHTML);
- },
-
- testAsynchronousRequest: function() {
- this.assertEqual("", $("content").innerHTML);
-
- new Ajax.Request("../fixtures/hello.js", {
- asynchronous: true,
- method: 'get',
- evalJS: 'force'
- });
- this.wait(1000, function() {
- var h2 = $("content").firstChild;
- this.assertEqual("Hello world!", h2.innerHTML);
- });
- },
-
- testResponders: function(){
- // check for internal responder
- this.assertEqual(1, Ajax.Responders.responders.length);
-
- var dummyResponder = {
- onComplete: function(req) { /* dummy */ }
- };
-
- Ajax.Responders.register(dummyResponder);
- this.assertEqual(2, Ajax.Responders.responders.length);
-
- // don't add twice
- Ajax.Responders.register(dummyResponder);
- this.assertEqual(2, Ajax.Responders.responders.length);
-
- Ajax.Responders.unregister(dummyResponder);
- this.assertEqual(1, Ajax.Responders.responders.length);
-
- var responder = {
- onCreate: function(req){ responderCounter++ },
- onLoading: function(req){ responderCounter++ },
- onComplete: function(req){ responderCounter++ }
- };
- Ajax.Responders.register(responder);
-
- this.assertEqual(0, responderCounter);
- this.assertEqual(0, Ajax.activeRequestCount);
- new Ajax.Request("../fixtures/content.html", { method:'get', parameters:"pet=monkey" });
- this.assertEqual(1, responderCounter);
- this.assertEqual(1, Ajax.activeRequestCount);
-
- this.wait(1000,function() {
- this.assertEqual(3, responderCounter);
- this.assertEqual(0, Ajax.activeRequestCount);
- });
- },
-
- testEvalResponseShouldBeCalledBeforeOnComplete: function() {
- if (this.isRunningFromRake) {
- this.assertEqual("", $("content").innerHTML);
-
- this.assertEqual(0, Ajax.activeRequestCount);
- new Ajax.Request("../fixtures/hello.js", extendDefault({
- onComplete: function(response) { this.assertNotEqual("", $("content").innerHTML) }.bind(this)
- }));
- this.assertEqual(0, Ajax.activeRequestCount);
-
- var h2 = $("content").firstChild;
- this.assertEqual("Hello world!", h2.innerHTML);
- } else {
- this.info(message);
- }
- },
-
- testContentTypeSetForSimulatedVerbs: function() {
- if (this.isRunningFromRake) {
- new Ajax.Request('/inspect', extendDefault({
- method: 'put',
- contentType: 'application/bogus',
- onComplete: function(response) {
- this.assertEqual('application/bogus; charset=UTF-8', response.responseJSON.headers['content-type']);
- }.bind(this)
- }));
- } else {
- this.info(message);
- }
- },
-
- testOnCreateCallback: function() {
- new Ajax.Request("../fixtures/content.html", extendDefault({
- onCreate: function(transport) { this.assertEqual(0, transport.readyState) }.bind(this),
- onComplete: function(transport) { this.assertNotEqual(0, transport.readyState) }.bind(this)
- }));
- },
-
- testEvalJS: function() {
- if (this.isRunningFromRake) {
-
- $('content').update();
- new Ajax.Request("/response", extendDefault({
- parameters: Fixtures.js,
- onComplete: function(transport) {
- var h2 = $("content").firstChild;
- this.assertEqual("Hello world!", h2.innerHTML);
- }.bind(this)
- }));
-
- $('content').update();
- new Ajax.Request("/response", extendDefault({
- evalJS: false,
- parameters: Fixtures.js,
- onComplete: function(transport) {
- this.assertEqual("", $("content").innerHTML);
- }.bind(this)
- }));
- } else {
- this.info(message);
- }
-
- $('content').update();
- new Ajax.Request("../fixtures/hello.js", extendDefault({
- evalJS: 'force',
- onComplete: function(transport) {
- var h2 = $("content").firstChild;
- this.assertEqual("Hello world!", h2.innerHTML);
- }.bind(this)
- }));
- },
-
- testCallbacks: function() {
- var options = extendDefault({
- onCreate: function(transport) { this.assertInstanceOf(Ajax.Response, transport) }.bind(this)
- });
-
- Ajax.Request.Events.each(function(state){
- options['on' + state] = options.onCreate;
- });
-
- new Ajax.Request("../fixtures/content.html", options);
- },
-
- testResponseText: function() {
- new Ajax.Request("../fixtures/empty.html", extendDefault({
- onComplete: function(transport) { this.assertEqual('', transport.responseText) }.bind(this)
- }));
-
- new Ajax.Request("../fixtures/content.html", extendDefault({
- onComplete: function(transport) { this.assertEqual(sentence, transport.responseText.toLowerCase()) }.bind(this)
- }));
- },
-
- testResponseXML: function() {
- if (this.isRunningFromRake) {
- new Ajax.Request("/response", extendDefault({
- parameters: Fixtures.xml,
- onComplete: function(transport) {
- this.assertEqual('foo', transport.responseXML.getElementsByTagName('name')[0].getAttribute('attr'))
- }.bind(this)
- }));
- } else {
- this.info(message);
- }
- },
-
- testResponseJSON: function() {
- if (this.isRunningFromRake) {
- new Ajax.Request("/response", extendDefault({
- parameters: Fixtures.json,
- onComplete: function(transport) { this.assertEqual(123, transport.responseJSON.test) }.bind(this)
- }));
-
- new Ajax.Request("/response", extendDefault({
- parameters: {
- 'Content-Length': 0,
- 'Content-Type': 'application/json'
- },
- onComplete: function(transport) { this.assertNull(transport.responseJSON) }.bind(this)
- }));
-
- new Ajax.Request("/response", extendDefault({
- evalJSON: false,
- parameters: Fixtures.json,
- onComplete: function(transport) { this.assertNull(transport.responseJSON) }.bind(this)
- }));
-
- new Ajax.Request("/response", extendDefault({
- parameters: Fixtures.jsonWithoutContentType,
- onComplete: function(transport) { this.assertNull(transport.responseJSON) }.bind(this)
- }));
-
- new Ajax.Request("/response", extendDefault({
- sanitizeJSON: true,
- parameters: Fixtures.invalidJson,
- onException: function(request, error) {
- this.assert(error.message.include('Badly formed JSON string'));
- this.assertInstanceOf(Ajax.Request, request);
- }.bind(this)
- }));
- } else {
- this.info(message);
- }
-
- new Ajax.Request("../fixtures/data.json", extendDefault({
- evalJSON: 'force',
- onComplete: function(transport) { this.assertEqual(123, transport.responseJSON.test) }.bind(this)
- }));
- },
-
- testHeaderJSON: function() {
- if (this.isRunningFromRake) {
- new Ajax.Request("/response", extendDefault({
- parameters: Fixtures.headerJson,
- onComplete: function(transport, json) {
- this.assertEqual('hello #éà', transport.headerJSON.test);
- this.assertEqual('hello #éà', json.test);
- }.bind(this)
- }));
-
- new Ajax.Request("/response", extendDefault({
- onComplete: function(transport, json) {
- this.assertNull(transport.headerJSON)
- this.assertNull(json)
- }.bind(this)
- }));
- } else {
- this.info(message);
- }
- },
-
- testGetHeader: function() {
- if (this.isRunningFromRake) {
- new Ajax.Request("/response", extendDefault({
- parameters: { 'X-TEST': 'some value' },
- onComplete: function(transport) {
- this.assertEqual('some value', transport.getHeader('X-Test'));
- this.assertNull(transport.getHeader('X-Inexistant'));
- }.bind(this)
- }));
- } else {
- this.info(message);
- }
- },
-
- testParametersCanBeHash: function() {
- if (this.isRunningFromRake) {
- new Ajax.Request("/response", extendDefault({
- parameters: $H({ "one": "two", "three": "four" }),
- onComplete: function(transport) {
- this.assertEqual("two", transport.getHeader("one"));
- this.assertEqual("four", transport.getHeader("three"));
- this.assertNull(transport.getHeader("toObject"));
- }.bind(this)
- }));
- } else {
- this.info(message);
- }
- },
-
- testIsSameOriginMethod: function() {
- var isSameOrigin = Ajax.Request.prototype.isSameOrigin;
- this.assert(isSameOrigin.call({ url: '/foo/bar.html' }), '/foo/bar.html');
- this.assert(isSameOrigin.call({ url: window.location.toString() }), window.location);
- this.assert(!isSameOrigin.call({ url: 'http://example.com' }), 'http://example.com');
-
- if (this.isRunningFromRake) {
- Ajax.Request.prototype.isSameOrigin = function() {
- return false
- };
-
- $("content").update('same origin policy');
- new Ajax.Request("/response", extendDefault({
- parameters: Fixtures.js,
- onComplete: function(transport) {
- this.assertEqual("same origin policy", $("content").innerHTML);
- }.bind(this)
- }));
-
- new Ajax.Request("/response", extendDefault({
- parameters: Fixtures.invalidJson,
- onException: function(request, error) {
- this.assert(error.message.include('Badly formed JSON string'));
- }.bind(this)
- }));
-
- new Ajax.Request("/response", extendDefault({
- parameters: { 'X-JSON': '{});window.attacked = true;({}' },
- onException: function(request, error) {
- this.assert(error.message.include('Badly formed JSON string'));
- }.bind(this)
- }));
-
- Ajax.Request.prototype.isSameOrigin = isSameOrigin;
- } else {
- this.info(message);
- }
- }
-});
View
29 test/unit/asp/ajax_test.asp
@@ -1,29 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-<head>
- <title>Prototype Unit test file | Ajax</title>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <script src="../../../dist/prototype.js" type="text/javascript" runat="server" language="javascript"></script>
- <script src="../../lib/assets/unittest.js" type="text/javascript" runat="server" language="javascript"></script>
- <link rel="stylesheet" href="../../lib/assets/test.css" type="text/css" />
- <script src="../fixtures/ajax.js" type="text/javascript" charset="utf-8" runat="server" language="javascript"></script>
- <script src="../ajax_test.js" type="text/javascript" runat="server" language="javascript"></script>
-</head>
-<body>
-<h1>Prototype Unit test file</h1>
-<h2>Ajax</h2>
-
-<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify -->
-
-<!-- Log output start -->
-<div id="testlog"><%= testResults.toHTML() %></div>
-<!-- Log output end -->
-
-<!-- HTML Fixtures start -->
-<div id="content"></div>
-<div id="content2" style="color:red"></div>
-
-<!-- HTML Fixtures end -->
-</body>
-</html>
Please sign in to comment.
Something went wrong with that request. Please try again.