diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d87b1d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/* diff --git a/lib/apricot.js b/lib/apricot.js index 9e0251c..ccf9416 100644 --- a/lib/apricot.js +++ b/lib/apricot.js @@ -4,37 +4,35 @@ var http = require('http'), url = require('url'); exports.Apricot = Apricot; -sys = require('sys'); function Apricot(content,live) { var dom = require('jsdom/lib/jsdom/level1/core').dom.level1.core; var browser = require('jsdom/lib/jsdom/browser/index').windowAugmentation(dom); - + this.raw = content; - + // TODO, content should be applied to the whole document, not the body. - - browser.document.body.innerHTML = content; + browser.document.innerHTML = content; this.document = browser.document; this.window = browser.window; this.matches = []; - + if (live) { this.live(); } - + this.__defineGetter__("toHTML", function(){ - return this.document.body.innerHTML; + return this.document.innerHTML; }); this.__defineGetter__("innerHTML", function(){ - return this.document.body.firstChild.innerHTML; + return this.document.firstChild.innerHTML; }); - + this.__defineGetter__("toDOM", function(){ - return this.document.body; - }); + return this.document; + }); } @@ -45,19 +43,19 @@ Apricot.prototype = { var sizzleSandbox = {}; var sizzle = require("./sizzle").sizzleInit(sizzleSandbox,this.document); m = sizzle(pattern); - + for (var i = 0, len = m.length; i < len; ++i) { this.matches.push(m[i]); } return this; }, - + live: function() { - var Script = process.binding('evals').Script; + var Script = require('vm').Script; console.alert = function(m) { console.log("Alerting: " + m); } - + console.console = { log: function(m) { console.log("Console: " + m); @@ -67,18 +65,18 @@ Apricot.prototype = { this.window.__defineSetter__("onload", function(e){ e.call(); }); - + try { // Setup the page var sandbox = { - document:this.document, - window:this.window, + document:this.document, + window:this.window, setTimeout:this.window.setTimeout, setInterval:this.window.setInterval, alert:console.alert, console:console.console - }; - + }; + // Add all script blocks to the scope this.find('script').each(function(script){ if (script.src) { @@ -88,7 +86,7 @@ Apricot.prototype = { fs.readFile(script.src, encoding='utf8', fnLoaderHandle); } else { - Script.runInNewContext(script.innerHTML,sandbox); + Script.runInNewContext(script.innerHTML,sandbox); } }); @@ -97,20 +95,20 @@ Apricot.prototype = { var fnName = onload.getAttribute('onload'); Script.runInNewContext(fnName,sandbox); }); - + } catch(e){ - sys.puts(sys.inspect(e)); + console.log(e); } - + }, - + each : function(fn) { for (var i = 0, len = this.matches.length; i < len; ++i) { fn.call(this,this.matches[i]); } return this; }, - + remove : function (html) { return this.html('remove', html); }, inner : function (html) { return this.html('inner', html); }, outer : function (html) { return this.html('outer', html); }, @@ -118,7 +116,7 @@ Apricot.prototype = { bottom : function (html) { return this.html('bottom', html); }, before : function (html) { return this.html('before', html); }, after : function (html) { return this.html('after', html); }, - + html : function(location, html) { this._clean(this); @@ -132,9 +130,9 @@ Apricot.prototype = { return this.each(function(el) { var parent, list, len, i = 0; - + if (location == "inner") { - + if (typeof html == 'string') { el.innerHTML = html; list = el.getElementsByTagName('SCRIPT'); @@ -161,9 +159,9 @@ Apricot.prototype = { } }); }, - + hasClass: function(className, callback) { - + return (callback === undefined && this.matches.length == 1) ? hasClass(this.matches[0], className) : this.each(function(el) { @@ -193,8 +191,8 @@ Apricot.prototype = { el.className = trim(el.className + ' ' + className); } }); - }, - + }, + // // Private instance method // @@ -217,7 +215,7 @@ Apricot.prototype = { if (n.nodeType == 3 && !ns.test(n.nodeValue)) { d.removeChild(n); } else { - n.nodeIndex = ++ni; + n.nodeIndex = ++ni; } n = nx; } @@ -283,8 +281,8 @@ Apricot.prototype = { el.addEventListener(type, fn, false); } }); - }, - + }, + } // @@ -309,10 +307,10 @@ Apricot.open = function(file, callback, live) { callback(err); } else { - try { + try { var h = live ? new Apricot(data, true) : new Apricot(data); callback(null, h); - } + } catch (error) { callback(error); } @@ -343,7 +341,7 @@ Apricot.open = function(file, callback, live) { } else { request.close(); } - + } else { fs.readFile(file, encoding='utf8', fnLoaderHandle); } diff --git a/package.json b/package.json index 18cbdbd..7f124cb 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,8 @@ } ], "dependencies": { - "jsdom": "= 0.1.2", - "htmlparser": "= 1.6.2" + "jsdom": "= 0.2.14", + "htmlparser": "= 1.7.6" }, "os": [ "linux",