Skip to content

Commit

Permalink
[mozilla#220] Popcorn.xhr support needed for parsing data-timeline-so…
Browse files Browse the repository at this point in the history
…urces
  • Loading branch information
rwaldron committed Dec 15, 2010
1 parent 12bad18 commit 4599b6e
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 0 deletions.
56 changes: 56 additions & 0 deletions popcorn.js
Expand Up @@ -542,6 +542,62 @@
return plugin;
};


var setup = {
url: '',
data: '',
dataType: '',
success: Popcorn.nop,
type: 'GET',
async: true,
xhr: function() {
return new XMLHttpRequest();
}
};

Popcorn.xhr = function ( options ) {

var settings = Popcorn.extend( {}, setup, options );

settings.ajax = settings.xhr();

if ( settings.ajax ) {

settings.ajax.open( settings.type, settings.url, settings.async );
settings.ajax.send( null );

return Popcorn.xhr.httpData( settings );
}
};


Popcorn.xhr.httpData = function ( settings ) {

var data, json = null,

onreadystatechange = settings.ajax.onreadystatechange = function() {

if ( settings.ajax.readyState == 4 ) {

try {
json = JSON.parse(settings.ajax.responseText);
} catch(e) {
//suppress
};

data = {
xml: settings.ajax.responseXML,
text: settings.ajax.responseText,
json: json
};

settings.success.call( settings.ajax, data );

}
};
return data;
};


global.Popcorn = Popcorn;

Expand Down
1 change: 1 addition & 0 deletions test/data/json.js
@@ -0,0 +1 @@
{ "data": {"lang": "en", "length": 25} }
1 change: 1 addition & 0 deletions test/data/test.js
@@ -0,0 +1 @@
{ "data": {"lang": "en", "length": 25} }
1 change: 1 addition & 0 deletions test/data/test.txt
@@ -0,0 +1 @@
This is a text test
11 changes: 11 additions & 0 deletions test/data/test.xml
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<dashboard>
<locations class="foo">
<location for="bar">
<infowindowtab>
<tab title="Location"><![CDATA[blabla]]></tab>
<tab title="Users"><![CDATA[blublu]]></tab>
</infowindowtab>
</location>
</locations>
</dashboard>
112 changes: 112 additions & 0 deletions test/popcorn.unit.js
Expand Up @@ -662,8 +662,120 @@ test("Protected Names", function () {
};
});
});
Popcorn.xhr.httpData

module("Popcorn XHR");
test("Basic", function () {

expect(2);

equals( typeof Popcorn.xhr, "function" , "Popcorn.xhr is a provided utility function");
equals( typeof Popcorn.xhr.httpData, "function" , "Popcorn.xhr.httpData is a provided utility function");


});

test("Parsing text", function () {

var expects = 2,
count = 0;

function plus() {
if ( ++count === expects ) {
start();
}
}

expect(expects);

stop();

Popcorn.xhr({
url: 'data/test.txt',
success: function( data ) {

ok(data, "xhr returns data");
plus();

equals( data.text, "This is a text test", "test.txt returns the string 'This is a text test'");
plus();

}
});
});

test("Parsing JSON", function () {

var expects = 2,
count = 0;

function plus() {
if ( ++count === expects ) {
start();
}
}

expect(expects);

stop();


var testObj = { "data": {"lang": "en", "length": 25} };

Popcorn.xhr({
url: 'data/test.js',
success: function( data ) {

ok(data, "xhr returns data");
plus();


ok( QUnit.equiv(data.json, testObj) , "data.json returns an object of data");
plus();

}
});

});

test("Parsing XML", function () {

var expects = 2,
count = 0;

function plus() {
if ( ++count === expects ) {
start();
}
}

expect(expects);

stop();


Popcorn.xhr({
url: 'data/test.xml',
success: function( data ) {

ok(data, "xhr returns data");
plus();

var parser = new DOMParser(),
xml = parser.parseFromString('<?xml version="1.0" encoding="UTF-8"?><dashboard><locations class="foo"><location for="bar"><infowindowtab> <tab title="Location"><![CDATA[blabla]]></tab> <tab title="Users"><![CDATA[blublu]]></tab> </infowindowtab> </location> </locations> </dashboard>',"text/xml");


equals( data.xml.toString(), xml.toString(), "data.xml returns a document of xml");
plus();

}
});

});



module("Popcorn Test Runner End");
test("Last Check", function () {

// ALWAYS RUN LAST
Expand Down

0 comments on commit 4599b6e

Please sign in to comment.