Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: toString encoding #128

Merged
merged 1 commit into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ export class Node {
selfCloseEmpty: boolean;
whitespace: boolean;
type: 'xml' | 'html' | 'xhtml';
encoding?:
| 'HTML'
| 'ASCII'
| 'UTF-8'
| 'UTF-16'
| 'ISO-Latin-1'
| 'ISO-8859-1';
}
): string;
}
Expand Down
17 changes: 14 additions & 3 deletions src/xml_document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ NAN_METHOD(XmlDocument::ToString) {
assert(document);

int options = 0;
const char *encoding = "UTF-8";
Local<Value> encodingOpt = Nan::Null();

if (info[0]->IsObject()) {
Local<Object> obj = Nan::To<Object>(info[0]).ToLocalChecked();
Expand Down Expand Up @@ -216,6 +218,10 @@ NAN_METHOD(XmlDocument::ToString) {
options |= XML_SAVE_WSNONSIG;
}

encodingOpt =
Nan::Get(obj, Nan::New<String>("encoding").ToLocalChecked())
.ToLocalChecked();

Local<Value> type = Nan::Get(obj, Nan::New<String>("type").ToLocalChecked())
.ToLocalChecked();
if (Nan::Equals(type, Nan::New<String>("XML").ToLocalChecked())
Expand Down Expand Up @@ -244,8 +250,13 @@ NAN_METHOD(XmlDocument::ToString) {
options |= XML_SAVE_FORMAT;
}

if (encodingOpt->IsString()) {
Nan::Utf8String encoding_(Nan::To<String>(encodingOpt).ToLocalChecked());
encoding = *encoding_;
}

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);
Expand Down Expand Up @@ -713,8 +724,8 @@ NAN_METHOD(XmlDocument::SchematronValidate) {
return Nan::ThrowError(
"Unable to create a validation context for the Schematron schema");
}
xmlSchematronSetValidStructuredErrors(valid_ctxt,
XmlSyntaxError::PushToArray,
xmlSchematronSetValidStructuredErrors(valid_ctxt,
XmlSyntaxError::PushToArray,
reinterpret_cast<void *>(&errors));

bool valid = xmlSchematronValidateDoc(valid_ctxt, document->xml_obj) == 0;
Expand Down
27 changes: 27 additions & 0 deletions test/html_parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,31 @@ describe('html parser', () => {
doc.toString({ type: 'xml', selfCloseEmpty: true }).indexOf('<a/>') > -1
).toBeTruthy();
});

it('toString with encoding', () => {
let doc = libxml.parseHtml('<a>Something&nbsp;with a space</a>');
expect(doc.toString({ type: 'xhtml' })).toEqual(
expect.not.stringContaining('&nbsp;')
);
expect(doc.toString({ type: 'xhtml', encoding: 'UTF-8' })).toEqual(
doc.toString({ type: 'xhtml' })
);
expect(doc.toString({ type: 'xhtml', encoding: 'HTML' })).toEqual(
expect.stringContaining('&nbsp;')
);
expect(doc.toString({ type: 'xhtml', encoding: 'ASCII' })).toEqual(
expect.stringContaining('&#160;')
);

doc = libxml.parseHtml('<a>Something with an emoji 😀</a>');
expect(doc.toString({ type: 'xhtml', encoding: 'UTF-8' })).toEqual(
expect.stringContaining('😀')
);
expect(doc.toString({ type: 'xhtml', encoding: 'HTML' })).toEqual(
expect.stringContaining('&#128512')
);
expect(doc.toString({ type: 'xhtml', encoding: 'ASCII' })).toEqual(
expect.stringContaining('&#128512')
);
});
});