forked from libxmljs/libxmljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_parser.ts
More file actions
167 lines (133 loc) · 5.65 KB
/
html_parser.ts
File metadata and controls
167 lines (133 loc) · 5.65 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var fs = require("fs");
import libxml from "../index";
import { XMLAttribute, HTMLParseOptions, parseHtml } from "../index";
const TEST_DIR = __dirname + "/../../test";
function make_error(object: any) {
var err = new Error(object.message) as any;
err.domain = object.domain;
err.code = object.code;
err.level = object.level;
err.line = object.line;
err.column = object.column;
return err;
}
module.exports.parse = function (assert: any) {
var filename = TEST_DIR + "/fixtures/parser.html";
function attempt_parse(encoding: any) {
var str = fs.readFileSync(filename, encoding);
var doc = parseHtml(str);
assert.equal("html", doc.root()?.name());
assert.equal("Test HTML document", (doc.get("head/title") as any).text());
assert.equal("HTML content!", (doc.get("body/span") as any).text());
}
// Parse via a string
attempt_parse("utf-8");
// Parse via a Buffer
attempt_parse(null);
assert.done();
};
module.exports.parseAsync = function (assert: any) {
var filename = TEST_DIR + "/fixtures/parser.html";
function attempt_parse(encoding: any) {
var str = fs.readFileSync(filename, encoding);
let x = 0;
libxml.parseHtmlAsync(str).then((doc) => {
assert.equal(++x, 2);
assert.equal("html", doc.root()?.name());
assert.equal("Test HTML document", (doc.get("head/title") as any).text());
assert.equal("HTML content!", (doc.get("body/span") as any).text());
});
assert.equal(++x, 1);
}
// Parse via a string
attempt_parse("utf-8");
// Parse via a Buffer
attempt_parse(null);
assert.done();
};
// Although libxml defaults to a utf-8 encoding, if not specifically specified
// it will guess the encoding based on meta http-equiv tags available
// This test shows that the "guessed" encoding can be overridden
module.exports.parse_force_encoding = function (assert: any) {
var filename = TEST_DIR + "/fixtures/parser.euc_jp.html";
function attempt_parse(encoding: any, opts: HTMLParseOptions) {
var str = fs.readFileSync(filename, encoding);
var doc = libxml.parseHtml(str, opts);
assert.equal(doc.errors, 0);
assert.equal("html", doc.root()?.name());
// make sure libxml rewrite the meta charset of this document
// calling toString on the document ensure that it is converted to the
// correct internal format and the new meta tag is replaced
doc.root()?.toString();
let result = doc.find("/html/head/meta/@content")[0];
var fixedCharset = (result as XMLAttribute).value();
assert.ok(fixedCharset.indexOf(opts.encoding!.toUpperCase()) !== -1);
assert.equal("テスト", (doc.get("head/title") as any).text());
assert.equal("テスト", (doc.get("body/div") as any).text());
}
// Parse via a string
attempt_parse("utf-8", { encoding: "utf-8" });
// Parse via a Buffer
attempt_parse(null, { encoding: "utf-8" });
assert.done();
};
module.exports.recoverable_parse = function (assert: any) {
var recoverableFile = TEST_DIR + "/fixtures/warnings/amp.html";
var str = fs.readFileSync(recoverableFile, "utf8");
var recoverableErrors = [
make_error({
domain: 5,
code: 23,
message: "htmlParseEntityRef: expecting ';'\n",
level: 2,
line: 12,
column: 27,
}),
make_error({ domain: 5, code: 68, message: "htmlParseEntityRef: no name\n", level: 2, line: 12, column: 38 }),
make_error({
domain: 5,
code: 23,
message: "htmlParseEntityRef: expecting ';'\n",
level: 2,
line: 14,
column: 4,
}),
make_error({ domain: 5, code: 68, message: "htmlParseEntityRef: no name\n", level: 2, line: 15, column: 4 }),
];
var doc = libxml.parseHtml(str);
assert.equal(4, doc.errors.length);
for (var i = 0; i < recoverableErrors.length; i++) {
assert.equal(recoverableErrors[i].domain, doc.errors[i]?.domain);
assert.equal(recoverableErrors[i].code, doc.errors[i]?.code);
assert.equal(recoverableErrors[i].message, doc.errors[i]?.message);
assert.equal(recoverableErrors[i].level, doc.errors[i]?.level);
assert.equal(recoverableErrors[i].line, doc.errors[i]?.line);
}
assert.done();
};
module.exports.parseOptions = function (assert: any) {
var doc = libxml.parseHtml("<a/>", { doctype: false, implied: false }).toString()!;
assert.ok(doc.indexOf("DOCTYPE") === -1);
assert.ok(doc.indexOf("body") === -1);
assert.ok(doc.indexOf("<html>") === -1);
doc = libxml.parseHtml("<a/>", { doctype: false, implied: true }).toString()!;
assert.ok(doc.indexOf("DOCTYPE") === -1);
assert.ok(doc.indexOf("body") > -1);
assert.ok(doc.indexOf("<html>") > -1);
doc = libxml.parseHtml("<a/>", { implied: false }).toString()!;
assert.ok(doc.indexOf("DOCTYPE") > -1);
assert.ok(doc.indexOf("body") === -1);
assert.ok(doc.indexOf("<html>") === -1);
assert.done();
};
module.exports.toString = function (assert: any) {
var doc = libxml.Document();
assert.ok(doc.toString({ declaration: false }) === "");
assert.equal(doc.toString({ declaration: false, type: "html" }), "\n");
doc = libxml.parseHtml("<a></a>");
assert.ok(doc.toString().indexOf("<?xml") === -1);
assert.ok(doc.toString({ type: "xml" }).indexOf("<?xml") > -1);
assert.ok(doc.toString({ type: "xhtml" }).indexOf("<?xml") > -1);
assert.ok(doc.toString({ type: "xml", selfCloseEmpty: true })!.indexOf("<a/>") > -1);
assert.done();
};