@@ -14,6 +14,7 @@ Napi::Object Libxml::Init(Napi::Env env, Napi::Object exports)
1414 Napi::Function func = DefineClass (env, " Libxml" , {
1515 InstanceMethod (" loadXml" , &Libxml::loadXml),
1616 InstanceMethod (" loadXmlFromString" , &Libxml::loadXmlFromString),
17+ InstanceMethod (" loadDtds" , &Libxml::loadDtds),
1718 InstanceMethod (" getDtd" , &Libxml::getDtd),
1819 InstanceMethod (" freeXml" , &Libxml::freeXml)
1920 });
@@ -118,6 +119,47 @@ Napi::Value Libxml::loadXmlFromString(const Napi::CallbackInfo& info) {
118119 return Napi::Boolean::New (env, true );
119120}
120121
122+ Napi::Value Libxml::loadDtds (const Napi::CallbackInfo& info) {
123+ Napi::Env env = info.Env ();
124+ if (info.Length () < 1 ){
125+ Napi::TypeError::New (env, " loadDtds requires at least 1 argument, an array of DTDs" ).ThrowAsJavaScriptException ();
126+ return env.Undefined ();
127+ }
128+ if (!info[0 ].IsArray ()){
129+ Napi::TypeError::New (env, " loadDtds requires an array" ).ThrowAsJavaScriptException ();
130+ return env.Undefined ();
131+ }
132+ Napi::EscapableHandleScope scope (env);
133+ Napi::Array dtdPaths = info[0 ].As <Napi::Array>();
134+ Napi::Array errors = Napi::Array::New (env);
135+ xmlResetLastError ();
136+ XmlSyntaxError::env = &env;
137+ xmlSetStructuredErrorFunc (reinterpret_cast <void *>(&errors),
138+ XmlSyntaxError::PushToArray);
139+ for (unsigned int i = 0 ; i < dtdPaths.Length (); i++){
140+ // Skip elements silently which are not strings
141+ if (dtdPaths.Get (i).IsString ()) {
142+ std::string dtdPath = dtdPaths.Get (i).ToString ().Utf8Value ();
143+ xmlChar* pathDTDCasted = xmlCharStrdup (dtdPath.c_str ());
144+ xmlDtdPtr dtd = xmlParseDTD (NULL , pathDTDCasted);
145+ if (dtd == nullptr ) {
146+ // DTD is bad, we set error and not assign it
147+ XmlSyntaxError::PushToArray (errors, dtdPath.c_str ());
148+ continue ;
149+ }
150+ this ->dtdsPaths .push_back (dtd);
151+ }
152+ }
153+ xmlSetStructuredErrorFunc (nullptr , nullptr );
154+ // We set dtdsLoadedErrors property for js side
155+ if (errors.Length ()){
156+ this ->Value ().Set (" dtdsLoadedErrors" , errors);
157+ } else {
158+ this ->Value ().Delete (" dtdsLoadedErrors" );
159+ }
160+ return env.Undefined ();
161+ }
162+
121163Napi::Value Libxml::getDtd (const Napi::CallbackInfo& info) {
122164 Napi::Env env = info.Env ();
123165 Napi::HandleScope scope (env);
0 commit comments