Skip to content

Commit

Permalink
Added "external" option support
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Dec 10, 2010
1 parent 4b27762 commit 2f1fdaf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/browser.js
Expand Up @@ -25,11 +25,18 @@ var port = 8888;
/**
* Initialize a new `Browser` with the given `html` or `server`.
*
* Options:
*
* - `external` enable fetching and evaluation of external resources [false]
*
* @param {String|http.Server} html
* @param {Object} options
* @api public
*/

var Browser = module.exports = exports = function Browser(html) {
var Browser = module.exports = exports = function Browser(html, options) {
options = options || {};
this.external = options.external;
this.history = [];
this.cookieJar = new CookieJar;
if ('string' == typeof html) {
Expand Down Expand Up @@ -59,8 +66,12 @@ Browser.prototype.__proto__ = EventEmitter.prototype;
*/

Browser.prototype.parse = function(html){
var options = {};
if (this.external) {
options.features = { FetchExternalResources: false, ProcessExternalResources: false };
}
this.source = html;
this.window = jsdom.jsdom(wrap(html)).createWindow();
this.window = jsdom.jsdom(wrap(html), null, options).createWindow();
this.jQuery = jQuery.create(this.window);
this.jQuery.browser = this.jQuery.fn.browser = this;
require('./jquery')(this, this.jQuery);
Expand Down
22 changes: 22 additions & 0 deletions test/browser.navigation.test.js
Expand Up @@ -75,6 +75,10 @@ app.get('/search/results', function(req, res){
res.send(req.query);
});

app.get('/script', function(req, res, next){
res.send('<script>document.getElementById("para").innerHTML = "<em>new</em>";</script><p id="para">old</p>');
});

app.get('/form', function(req, res){
res.send('<form id="user" method="post">'
+ '<input id="user-name" type="text" name="user[name]" />'
Expand All @@ -100,6 +104,24 @@ app.post('/form', function(req, res){
});

module.exports = {
'test external option disabled': function(done){
var browser = tobi.createBrowser(app);
browser.get('/script', function(res, $){
res.should.have.status(200);
$('p').should.have.text('old');
done();
});
},

'test external option enabled': function(done){
var browser = tobi.createBrowser(app, { external: true });
browser.get('/script', function(res, $){
res.should.have.status(200);
$('p').should.have.text('new');
done();
});
},

'test .request() invalid response': function(done){
var browser = tobi.createBrowser(app);
browser.on('error', function(err){
Expand Down

0 comments on commit 2f1fdaf

Please sign in to comment.