Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/*
78 changes: 38 additions & 40 deletions lib/apricot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}


Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -88,7 +86,7 @@ Apricot.prototype = {
fs.readFile(script.src, encoding='utf8', fnLoaderHandle);

} else {
Script.runInNewContext(script.innerHTML,sandbox);
Script.runInNewContext(script.innerHTML,sandbox);
}
});

Expand All @@ -97,28 +95,28 @@ 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); },
top : function (html) { return this.html('top', html); },
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);

Expand All @@ -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');
Expand All @@ -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) {
Expand Down Expand Up @@ -193,8 +191,8 @@ Apricot.prototype = {
el.className = trim(el.className + ' ' + className);
}
});
},
},

//
// Private instance method
//
Expand All @@ -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;
}
Expand Down Expand Up @@ -283,8 +281,8 @@ Apricot.prototype = {
el.addEventListener(type, fn, false);
}
});
},
},

}

//
Expand All @@ -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);
}
Expand Down Expand Up @@ -343,7 +341,7 @@ Apricot.open = function(file, callback, live) {
} else {
request.close();
}

} else {
fs.readFile(file, encoding='utf8', fnLoaderHandle);
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
}
],
"dependencies": {
"jsdom": "= 0.1.2",
"htmlparser": "= 1.6.2"
"jsdom": "= 0.2.14",
"htmlparser": "= 1.7.6"
},
"os": [
"linux",
Expand Down