Skip to content

Commit 87f2818

Browse files
committed
Bindings and spec tests for HTML parsing
1 parent 2fb3dea commit 87f2818

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Test HTML document</title>
5+
</head>
6+
<body>
7+
<h1>Hello World</h1>
8+
<BR><BR><span>HTML content!</span>
9+
</body>
10+
</html>

spec/spec_html_parser.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
process.mixin(require('./helpers'));
2+
3+
describe('Parsing', function() {
4+
var filename = path.dirname(__filename)+'/fixtures/html_parser_test.html';
5+
6+
it('can be done from HTML', function() {
7+
var str = posix.cat(filename).wait();
8+
9+
var doc = libxml.parseHTML(str);
10+
assertEqual('html', doc.root().name());
11+
assertEqual('Test HTML document', doc.root().get('head').get('title').text());
12+
assertEqual('HTML content!', doc.root().get('body').get('span').text());
13+
});
14+
});

src/parser.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ ParseString(const v8::Arguments& args) {
2525
return LIBXMLJS_GET_MAYBE_BUILD(Document, xmlDoc, doc);
2626
}
2727

28+
v8::Handle<v8::Value>
29+
ParseHTML(const v8::Arguments& args) {
30+
v8::HandleScope scope;
31+
32+
if (!args[0]->IsString())
33+
return v8::ThrowException(v8::Exception::Error(
34+
v8::String::New("Must supply parseHTML with a string")));
35+
36+
v8::String::Utf8Value str(args[0]->ToString());
37+
xmlResetLastError();
38+
xmlDoc *doc = htmlReadMemory(*str, str.length(), NULL, NULL, 0);
39+
if (doc == NULL) {
40+
xmlFreeDoc(doc);
41+
return v8::Null();
42+
}
43+
44+
return LIBXMLJS_GET_MAYBE_BUILD(Document, xmlDoc, doc);
45+
}
46+
2847
v8::Handle<v8::Value>
2948
ParseFile(const v8::Arguments& args) {
3049
v8::HandleScope scope;
@@ -48,6 +67,7 @@ void
4867
Parser::Initialize(v8::Handle<v8::Object> target) {
4968
v8::HandleScope scope;
5069

70+
LIBXMLJS_SET_METHOD(target, "parseHTML", ParseHTML);
5171
LIBXMLJS_SET_METHOD(target, "parseString", ParseString);
5272
LIBXMLJS_SET_METHOD(target, "parseFile", ParseFile);
5373

0 commit comments

Comments
 (0)