Skip to content

Commit

Permalink
feat: add path() to text (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
marudor authored Oct 9, 2020
1 parent 0bf57d6 commit 7b0af7a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/xml_text.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ namespace libxmljs {

Nan::Persistent<FunctionTemplate> XmlText::constructor_template;

Local<Value> XmlText::get_path() {
Nan::EscapableHandleScope scope;
xmlChar *path = xmlGetNodePath(xml_obj);
const char *return_path = path ? reinterpret_cast<char *>(path) : "";
int str_len = xmlStrlen((const xmlChar *)return_path);
Local<String> js_obj =
Nan::New<String>(return_path, str_len).ToLocalChecked();
xmlFree(path);
return scope.Escape(js_obj);
}

// doc, name, content
NAN_METHOD(XmlText::New) {
NAN_CONSTRUCTOR_CHECK(Text)
Expand Down Expand Up @@ -153,6 +164,14 @@ NAN_METHOD(XmlText::Replace) {
return info.GetReturnValue().Set(info[0]);
}

NAN_METHOD(XmlText::Path) {
Nan::HandleScope scope;
XmlText *text = Nan::ObjectWrap::Unwrap<XmlText>(info.Holder());
assert(text);

return info.GetReturnValue().Set(text->get_path());
}

void XmlText::set_content(const char *content) {
xmlChar *encoded =
xmlEncodeSpecialChars(xml_obj->doc, (const xmlChar *)content);
Expand Down Expand Up @@ -265,6 +284,8 @@ void XmlText::Initialize(Local<Object> target) {

Nan::SetPrototypeMethod(tmpl, "text", XmlText::Text);

Nan::SetPrototypeMethod(tmpl, "path", XmlText::Path);

Nan::SetPrototypeMethod(tmpl, "replace", XmlText::Replace);

Nan::SetPrototypeMethod(tmpl, "addPrevSibling", XmlText::AddPrevSibling);
Expand Down
2 changes: 2 additions & 0 deletions src/xml_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class XmlText : public XmlNode {
static NAN_METHOD(New);
static NAN_METHOD(Text);
static NAN_METHOD(Replace);
static NAN_METHOD(Path);

static NAN_METHOD(NextElement);
static NAN_METHOD(PrevElement);
Expand All @@ -30,6 +31,7 @@ class XmlText : public XmlNode {
v8::Local<v8::Value> get_next_element();
v8::Local<v8::Value> get_prev_element();
v8::Local<v8::Value> get_content();
v8::Local<v8::Value> get_path();
void set_content(const char *content);
void replace_text(const char *content);
void replace_element(xmlNode *element);
Expand Down
9 changes: 9 additions & 0 deletions test/xml_parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ describe('xml parser', () => {
expect(err.code).toBe(errorControl.code);
});

it('text path', () => {
const xml = '<?xml version="1.0" encoding="utf-8"?><Name>Test</Name>';
const doc = libxml.parseXmlString(xml);
const text = doc.get('/Name').childNodes()[0];

expect(text.type()).toEqual('text');
expect(text.path()).toEqual('/Name/text()');
});

it('parse_options', () => {
function test_parser_option(input, options, expected) {
let output = libxml.parseXml(input, options).toString();
Expand Down

0 comments on commit 7b0af7a

Please sign in to comment.