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

Adds support of XML using xmldom #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ MIT license. See LICENSE for full details.
* All spec methods (open, send, abort, getRequestHeader,
getAllRequestHeaders, event methods)
* Requests to all domains
* XML parsing

## Known Issues / Missing Features ##

Expand All @@ -52,4 +53,3 @@ page](https://github.com/driverdan/node-XMLHttpRequest/issues).
* Synchronous requests freeze node while waiting for response (But that's what you want, right? Stick with async!).
* Some events are missing, such as abort
* Cookies aren't persisted between requests
* Missing XML support
37 changes: 36 additions & 1 deletion lib/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*
* @author Dan DeFelippi <[email protected]>
* @contributor David Ellis <[email protected]>
* @contributor Guy Baconniere <[email protected]>
* @contributor Arnaud Spicht <[email protected]>
* @license MIT
*/

Expand Down Expand Up @@ -442,6 +444,7 @@ exports.XMLHttpRequest = function() {
});

response.on("end", function() {
self.responseXML = html2dom(self.responseText);
if (sendFlag) {
// Discard the end event if the connection has been aborted
setState(self.DONE);
Expand Down Expand Up @@ -478,15 +481,27 @@ exports.XMLHttpRequest = function() {
// The async request the other Node process executes
var execString = "var http = require('http'), https = require('https'), fs = require('fs');"
+ "var doRequest = http" + (ssl ? "s" : "") + ".request;"
+ "var html2dom = function(html) {"
+ "var result='';"
+ "try {"
+ "var DOMParser = require('xmldom').DOMParser;"
+ "var document = new DOMParser().parseFromString(html);"
+ "result = document;"
+ "} catch(err) {"
+ "}"
+ "return result;"
+ "};"
+ "var options = " + JSON.stringify(options) + ";"
+ "var responseText = '';"
+ "var responseXML = '';"
+ "var req = doRequest(options, function(response) {"
+ "response.setEncoding('utf8');"
+ "response.on('data', function(chunk) {"
+ " responseText += chunk;"
+ "});"
+ "response.on('end', function() {"
+ "fs.writeFileSync('" + contentFile + "', JSON.stringify({err: null, data: {statusCode: response.statusCode, headers: response.headers, text: responseText}}), 'utf8');"
+ "responseXML = html2dom(responseText);"
+ "fs.writeFileSync('" + contentFile + "', JSON.stringify({err: null, data: {statusCode: response.statusCode, headers: response.headers, text: responseText, xml: responseXML}}), 'utf8');"
+ "fs.unlinkSync('" + syncFile + "');"
+ "});"
+ "response.on('error', function(error) {"
Expand Down Expand Up @@ -516,6 +531,7 @@ exports.XMLHttpRequest = function() {
response = resp.data;
self.status = resp.data.statusCode;
self.responseText = resp.data.text;
self.responseXML = resp.data.xml;
setState(self.DONE);
}
}
Expand All @@ -528,6 +544,7 @@ exports.XMLHttpRequest = function() {
this.status = 0;
this.statusText = error;
this.responseText = error.stack;
this.responseXML = error.stack;
errorFlag = true;
setState(this.DONE);
this.dispatchEvent('error');
Expand Down Expand Up @@ -617,4 +634,22 @@ exports.XMLHttpRequest = function() {
}
}
};

/**
* Parse HTML string and return a DOM
*
* @param string HTML to be parsed.
* @return string DOM representation.
*/
var html2dom = function(html) {
var result = "";
try {
var DOMParser = require('xmldom').DOMParser;
var document = new DOMParser().parseFromString(html);
result = document;
} catch(err) {
//throw "HTML_PARSE_ERR: unable to parse HTML : " + err;
}
return result;
};
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"engines": {
"node": ">=0.4.0"
},
"dependencies": {
"xmldom": ">= 0.1.19"
},
"directories": {
"lib": "./lib",
"example": "./example"
Expand Down