Skip to content

Commit a722eb8

Browse files
committed
imp: readding freeDtds function
1 parent b717927 commit a722eb8

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

libxml.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Napi::Object Libxml::Init(Napi::Env env, Napi::Object exports)
1616
InstanceMethod("loadXmlFromString", &Libxml::loadXmlFromString),
1717
InstanceMethod("loadDtds", &Libxml::loadDtds),
1818
InstanceMethod("getDtd", &Libxml::getDtd),
19-
InstanceMethod("freeXml", &Libxml::freeXml)
19+
InstanceMethod("freeXml", &Libxml::freeXml),
20+
InstanceMethod("freeDtds", &Libxml::freeDtds)
2021
});
2122

2223
// Create a peristent reference to the class constructor. This will allow
@@ -193,7 +194,8 @@ Napi::Value Libxml::getDtd(const Napi::CallbackInfo& info) {
193194
}
194195

195196
void Libxml::freeXml(const Napi::CallbackInfo& info) {
196-
Napi::HandleScope scope(info.Env());
197+
Napi::Env env = info.Env();
198+
Napi::HandleScope scope(env);
197199
// Delete Javascript property
198200
this->Value().Delete("wellformedErrors");
199201
// If doc is already null, just do nothing and only available on manual mod
@@ -205,6 +207,28 @@ void Libxml::freeXml(const Napi::CallbackInfo& info) {
205207
this->docPtr = nullptr;
206208
}
207209

210+
Napi::Value Libxml::freeDtds(const Napi::CallbackInfo& info) {
211+
Napi::Env env = info.Env();
212+
Napi::HandleScope scope(env);
213+
// Delete Javascript property
214+
this->Value().Delete("dtdsLoadedErrors");
215+
this->Value().Delete("validationDtdErrors");
216+
// If dtds is already empty, just stop here
217+
if(this->dtdsPaths.empty()){
218+
return env.Undefined();
219+
}
220+
for (vector<xmlDtdPtr>::iterator dtd = this->dtdsPaths.begin(); dtd != this->dtdsPaths.end() ; ++dtd){
221+
if(*dtd != nullptr){
222+
// Force clear memory DTD loaded
223+
xmlFreeDtd(*dtd);
224+
*dtd = nullptr;
225+
}
226+
}
227+
// clear the vector of dtds
228+
this->dtdsPaths.clear();
229+
return env.Undefined();
230+
}
231+
208232
// Initialize native add-on
209233
Napi::Object Init(Napi::Env env, Napi::Object exports)
210234
{

libxml.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Libxml : public Napi::ObjectWrap<Libxml>
4848
// static Napi::Value xpathSelect(const Napi::CallbackInfo& info);
4949
Napi::Value getDtd(const Napi::CallbackInfo& info);
5050
void freeXml(const Napi::CallbackInfo& info);
51-
// static Napi::Value freeDtds(const Napi::CallbackInfo& info);
51+
Napi::Value freeDtds(const Napi::CallbackInfo& info);
5252
// static Napi::Value freeSchemas(const Napi::CallbackInfo& info);
5353
// static Napi::Value clearAll(const Napi::CallbackInfo& info);
5454
};

test/libxml-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Node-Libxml', function () {
1212
expect(libxml.dtdsLoadedErrors).to.be.a('array');
1313
expect(libxml.dtdsLoadedErrors).to.include('test/dtd/mydoctype-not-existing.dtd');
1414
libxml.freeXml();
15-
// libxml.freeDtds();
15+
libxml.freeDtds();
1616
});
1717
// Wellformed & valid
1818
// it('Should return wellformed & valid on a wellformed & valid xml', function () {

0 commit comments

Comments
 (0)