-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
run.js
96 lines (71 loc) · 2.43 KB
/
run.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var browser = require("../../lib/jsdom/browser");
var dom = new browser.browserAugmentation(require("../../lib/jsdom/level1/core").dom.level1.core);
var sax = require("./sax");
var sys = require("sys");
// TODO: change this example to use pluggable parser
/**
setup innerHTML setter
*/
dom.Element.prototype.__defineSetter__('innerHTML', function(html) {
// first remove all the children
for (var i=this.childNodes.length-1; i>=0;i--)
{
this.removeChild(this.childNodes.item(i));
}
var currentElement = this, currentLevel = 0;
/**
setup sax parser
*/
parser = sax.parser(false);
parser.onerror = function (e) {
};
parser.ontext = function (t) {
var ownerDocument = currentElement.ownerDocument || currentElement;
var newText = ownerDocument.createTextNode(t);
currentElement.appendChild(newText);
};
parser.onopentag = function (node) {
var nodeName = node.name.toLowerCase(),
document = currentElement.ownerDocument || currentElement,
newElement = document.createElement(nodeName),
i = 0,
length = (node.attributes && node.attributes.length) ?
node.attributes.length :
0;
for (i in node.attributes)
{
if (node.attributes.hasOwnProperty(i)) {
newElement.setAttribute(i, node.attributes[i]);
}
}
currentElement.appendChild(newElement);
currentElement = newElement;
};
parser.onclosetag = function(node) {
currentElement = currentElement.parentNode;
}
parser.write(html).close();
});
var doc = new dom.Document("html");
var implementation = new dom.DOMImplementation(doc, {
"HTML" : "1.0",
"DisableLiveLists" : "1.0"
});
var notations = new dom.NotationNodeMap(
doc,
doc.createNotationNode("notation1","notation1File", null),
doc.createNotationNode("notation2",null, "notation2File")
);
var entities = new dom.EntityNodeMap(doc);
var doctype = new dom.DocumentType(doc, "html", entities, notations);
doc.doctype = doctype;
doc.implementation = implementation;
doc.innerHTML = '<html><head></head><body><div class="who"></div></body></html>';
var window = {
alert : function() { sys.puts(sys.inspect(arguments)); },
document : doc
};
window.Sizzle = require("../sizzle/sizzle").sizzleInit(window, doc);
var $ = require("./pure").pureInit(window, doc);
$("div").autoRender({"who":"Hello Wrrrld"});
sys.puts(doc.innerHTML);