@@ -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
195196void 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
209233Napi::Object Init (Napi::Env env, Napi::Object exports)
210234{
0 commit comments