From 6cd3e04abbbc7d9ab554dea079b9cb3a171adcc4 Mon Sep 17 00:00:00 2001 From: Rich Hodgkins Date: Fri, 29 Apr 2022 02:25:51 +0100 Subject: [PATCH] Added encoding for toString() method (#482) * Failing test case for stripped out entities * Allow encoding to be specified for toString() --- src/xml_document.cc | 10 +++++++++- test/html_parser.js | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/xml_document.cc b/src/xml_document.cc index 8e4f8251..2ed3dd56 100644 --- a/src/xml_document.cc +++ b/src/xml_document.cc @@ -177,6 +177,7 @@ NAN_METHOD(XmlDocument::ToString) assert(document); int options = 0; + const char *encoding = "UTF-8"; if (info.Length() > 0) { if (info[0]->IsBoolean()) { @@ -186,6 +187,13 @@ NAN_METHOD(XmlDocument::ToString) } else if (info[0]->IsObject()) { v8::Local obj = Nan::To(info[0]).ToLocalChecked(); + // choose encoding declaration + v8::Local encodingOpt = Nan::Get(obj, Nan::New("encoding").ToLocalChecked()).ToLocalChecked(); + if (encodingOpt->IsString()) { + std::string encoding_ = *Nan::Utf8String(encodingOpt); + encoding = encoding_.c_str(); + } + // drop the xml declaration if (Nan::Get(obj, Nan::New("declaration").ToLocalChecked()).ToLocalChecked()->IsFalse()) { options |= XML_SAVE_NO_DECL; @@ -227,7 +235,7 @@ NAN_METHOD(XmlDocument::ToString) } xmlBuffer* buf = xmlBufferCreate(); - xmlSaveCtxt* savectx = xmlSaveToBuffer(buf, "UTF-8", options); + xmlSaveCtxt* savectx = xmlSaveToBuffer(buf, encoding, options); xmlSaveTree(savectx, (xmlNode*)document->xml_obj); xmlSaveFlush(savectx); xmlSaveClose(savectx); diff --git a/test/html_parser.js b/test/html_parser.js index bde64eab..1bbd2bea 100644 --- a/test/html_parser.js +++ b/test/html_parser.js @@ -151,5 +151,13 @@ module.exports.toString = function(assert) { assert.ok(doc.toString({ type: 'xml' }).indexOf(' -1); assert.ok(doc.toString({ type: 'xhtml' }).indexOf(' -1); assert.ok(doc.toString({ type: 'xml', selfCloseEmpty:true }).indexOf('') > -1); + + doc = libxml.parseHtml("Something with a space"); + assert.ok(doc.toString().indexOf(' -1); + assert.ok(doc.toString({ type: 'xhtml' }).indexOf(' -1); + assert.ok(doc.toString({ type: 'xhtml' }).indexOf(' ') === -1); + assert.ok(doc.toString({ type: 'xhtml', encoding: 'HTML' }).indexOf(' ') > -1); + assert.ok(doc.toString({ type: 'xhtml', encoding: 'ASCII' }).indexOf(' ') > -1); assert.done(); }