-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse5.js
50 lines (42 loc) · 1.4 KB
/
parse5.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
module.exports = parseInto;
function parseInto(string, document, SimpleApiParser) {
var builder = new Builder(document);
var parser = new SimpleApiParser(builder);
parser.parse(string);
}
function Builder(document) {
var self = this;
this.document = document;
this.currentNode = null;
this.doctype = this.doctype.bind(this);
this.startTag = this.startTag.bind(this);
this.endTag = this.endTag.bind(this);
this.text = this.text.bind(this);
this.comment = this.comment.bind(this);
}
Builder.prototype.doctype = function (name, publicId, systemId) {
this.document.doctype = "<!doctype " + name + ">";
};
Builder.prototype.startTag = function (tagName, attributes, selfClosing) {
var node = this.document.createElement(tagName);
attributes.forEach(function (attribute) {
node.setAttribute(attribute.name, attribute.value);
});
if (this.currentNode) {
this.currentNode.appendChild(node);
} else {
this.document.documentElement = node;
}
if (!selfClosing) {
this.currentNode = node;
}
};
Builder.prototype.endTag = function () {
this.currentNode = this.currentNode.parentNode;
};
Builder.prototype.text = function (text) {
this.currentNode.appendChild(this.document.createTextNode(text));
};
Builder.prototype.comment = function (text) {
this.currentNode.appendChild(this.document.createComment(text));
};