Skip to content

Commit

Permalink
Land: jsdom.env now has a document configuation option which allows u…
Browse files Browse the repository at this point in the history
…sers to change the referer of the document.
  • Loading branch information
tmpvar committed Oct 10, 2011
2 parents 0ff9631 + ff3f250 commit c805367
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
7 changes: 6 additions & 1 deletion lib/jsdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ exports.env = exports.jsdom.env = function() {
return callback(new Error('JSDOM: a window object could not be created.'));
}

if( config.document ) {
window.document._referrer = config.document.referrer;
}

window.document.implementation.addFeature('FetchExternalResources', ['script']);
window.document.implementation.addFeature('ProcessExternalResources', ['script']);
window.document.implementation.addFeature('MutationEvents', ['1.0']);
Expand Down Expand Up @@ -296,7 +300,8 @@ exports.env.processArguments = function(args) {
'done' : true,
'scripts' : false,
'config' : false,
'url' : false // the URL for location.href if different from html
'url' : false, // the URL for location.href if different from html
'document': false // HTMLDocument properties
},
propKeys = Object.keys(props),
config = {
Expand Down
4 changes: 3 additions & 1 deletion lib/jsdom/level2/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ core.HTMLDocument = function HTMLDocument(options) {
options.contentType = 'text/html';
}
core.Document.call(this, options);
this._referrer = options.referrer || "";
this._URL = options.url || '/';
this._documentRoot = options.documentRoot || Path.dirname(this._URL);
this._queue = new ResourceQueue(options.deferClose);
Expand All @@ -326,8 +327,9 @@ core.HTMLDocument = function HTMLDocument(options) {
};

core.HTMLDocument.prototype = {
_referrer : "",
get referrer() {
return "";
return this._referrer;
},
get domain() {
return "";
Expand Down
46 changes: 26 additions & 20 deletions test/jsdom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ exports.tests = {
});
},

env_with_document_referrer : function(test) {
var html = "<html><body><p>hello world!</p></body></html>";
jsdom.env({
html : html,
document : { referrer:'https://github.com/tmpvar/jsdom' },
done: function(errors, window) {
test.equal(errors, null, 'errors should be null');
test.notEqual(window.document._referrer, null, 'window.document.referrer should not be null');
test.equal(window.document._referrer, 'https://github.com/tmpvar/jsdom', 'window.document._referrer should match the configured value');
test.done();
}
})
},

env_processArguments_invalid_args: function(test) {
test.throws(function(){ jsdom.env.processArguments(); });
test.throws(function(){ jsdom.env.processArguments({}); });
Expand All @@ -241,26 +255,18 @@ exports.tests = {
test.done();
},

env_processArguments_object_and_callback : function(test) {
var config = jsdom.env.processArguments([{
html : "",
scripts : ['path/to/some.js', 'another/path/to.js'],
url : 'http://www.example.com/'
},
function() {}
]);

test.notEqual(null, config.done, "has done");
test.notEqual(null, config.html, "has html");
test.notEqual(null, config.url, 'has url');
test.equal(2, config.scripts.length, 'has code');
test.done();
},

env_processArguments_object_and_callback: function(test) {
var config = jsdom.env.processArguments([{html: "", scripts: ['path/to/some.js', 'another/path/to.js']}, function(){}]);
test.notEqual(config.done, null, 'config.done should not be null');
test.notEqual(config.html, null, 'config.html should not be null');
var config = jsdom.env.processArguments([{
html : "",
scripts : ['path/to/some.js', 'another/path/to.js'],
url : 'http://www.example.com/',
document : {}
}, function(){}]);

test.notEqual(config.done, null, 'config.done should not be null');
test.notEqual(config.html, null, 'config.html should not be null');
test.notEqual(config.url, null, 'config.url should not be null');
test.notEqual(config.document, null, 'config.document should not be null');
test.equal(config.scripts.length, 2, 'has code');
test.done();
},
Expand Down Expand Up @@ -1052,7 +1058,7 @@ document.write("<SCR"+"IPT TYPE=\'text/javascript\' SRC=\'...\'><\/SCR"+"IPT>");
test.equal(doc.getElementById(el.id), null, 'Element must not be found after it has been removed');

test.done();
},
},

jsdom_levels: function(test) {
var level1 = jsdom.level(1);
Expand Down
21 changes: 17 additions & 4 deletions test/level2/html.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
var fs = require('fs');
var jsdom = require("../../lib/jsdom");
var fileCache = {};
var load = function(name) {
var file = __dirname + "/html/files/" + name + ".html",
contents = fileCache[file] || fs.readFileSync(file, 'utf8'),
doc = jsdom.jsdom(null, null, {url: "file://" + file }),
var load = function(name, options) {
options || (options = {});

var file = __dirname + "/html/files/" + name + ".html";

if(!options.url) {
options.url = "file://" + file;
}

var contents = fileCache[file] || fs.readFileSync(file, 'utf8'),
doc = jsdom.jsdom(null, null, options),
window = doc.createWindow();

doc.parent = window;
Expand Down Expand Up @@ -2139,6 +2146,12 @@ exports.tests = {
doc = load("document");
vreferrer = doc.referrer;
test.equal(vreferrer, "", "referrerLink");

// Test configuration of referrer value.
doc = load("document", { referrer:'http://www.example.com' });
vreferrer = doc.referrer;
test.equal(vreferrer, "http://www.example.com", "referrerLink");

test.done();
},

Expand Down

0 comments on commit c805367

Please sign in to comment.