Permalink
Browse files

Land: Isolate QuerySelector from prototype (Nao Iizuka)

  • Loading branch information...
tmpvar committed Mar 19, 2012
2 parents 3574104 + c7fb76b commit 0f255743f644342083de3284d255d51e62847c61
Showing with 40 additions and 17 deletions.
  1. +4 −4 lib/jsdom.js
  2. +21 −13 lib/jsdom/selectors/index.js
  3. +15 −0 test/jsdom/index.js
View
@@ -50,15 +50,15 @@ exports.jsdom = function (html, level, options) {
module.parent.filename;
}
- if (options.features && options.features.QuerySelector) {
- require("./jsdom/selectors/index").applyQuerySelectorPrototype(level);
- }
-
var browser = exports.browserAugmentation(level, options),
doc = (browser.HTMLDocument) ?
new browser.HTMLDocument(options) :
new browser.Document(options);
+ if (options.features && options.features.QuerySelector) {
+ require("./jsdom/selectors/index").applyQuerySelector(doc, level);
+ }
+
features.applyDocumentFeatures(doc, options.features);
if (typeof html === 'undefined' || html === null) {
@@ -1,5 +1,5 @@
var createSizzle = require("./sizzle");
-exports.applyQuerySelectorPrototype = function(dom) {
+exports.applyQuerySelector = function(doc, dom) {
var addSizzle = function(document) {
if (!document._sizzle) {
@@ -8,24 +8,32 @@ exports.applyQuerySelectorPrototype = function(dom) {
return document._sizzle;
};
- dom.Document.prototype.querySelector = function(selector) {
+ doc.querySelector = function(selector) {
return addSizzle(this)(selector, this)[0];
};
- dom.Document.prototype.querySelectorAll = function(selector) {
+ doc.querySelectorAll = function(selector) {
return new dom.NodeList(addSizzle(this)(selector, this));
};
- dom.Element.prototype.querySelector = function(selector) {
- return addSizzle(this.ownerDocument)(selector, this)[0];
- };
+ var _createElement = doc.createElement;
+ doc.createElement = function() {
+ var element = _createElement.apply(this, arguments);
- dom.Element.prototype.querySelectorAll = function(selector) {
- var el = this;
- if (!this.parentNode) {
- el = this.ownerDocument.createElement("div");
- el.appendChild(this);
- }
- return new dom.NodeList(addSizzle(this.ownerDocument)(selector, el.parentNode || el));
+ element.querySelector = function(selector) {
+ return addSizzle(this.ownerDocument)(selector, this)[0];
+ };
+
+ element.querySelectorAll = function(selector) {
+ var el = this;
+ if (!this.parentNode) {
+ el = this.ownerDocument.createElement("div");
+ el.appendChild(this);
+ }
+ return new dom.NodeList(addSizzle(this.ownerDocument)(selector, el.parentNode || el));
+ };
+
+ return element;
};
+
};
View
@@ -545,6 +545,21 @@ exports.tests = {
test.done();
},
+ turn_off_queryselector: function(test) {
+ var html = '<html><body></body></html>',
+ document = jsdom.jsdom(html, null, {features: {'QuerySelector': true}});
+ test.equal(typeof document.querySelector, 'function', 'document.querySelector function exists');
+ test.equal(typeof document.querySelectorAll, 'function', 'document.querySelectorAll function exists');
+ test.equal(typeof document.body.querySelector, 'function', 'document.body.querySelector function exists');
+ test.equal(typeof document.body.querySelectorAll, 'function', 'document.body.querySelectorAll function exists');
+ document = jsdom.jsdom(html, null);
+ test.equal(typeof document.querySelector, 'undefined', 'document.querySelector does not exist');
+ test.equal(typeof document.querySelectorAll, 'undefined', 'document.querySelectorAll does not exist');
+ test.equal(typeof document.body.querySelector, 'undefined', 'document.body.querySelector does not exist');
+ test.equal(typeof document.body.querySelectorAll, 'undefined', 'document.body.querySelectorAll does not exist');
+ test.done();
+ },
+
url_resolution: function(test) {
var html = '\
<html>\

0 comments on commit 0f25574

Please sign in to comment.