How do I get the output from ckeditor as XML instead of HTML?
I thought I could just use
editor.data.processor=new XmlDataProcessor();
but that only seems to work for input where the editor now requires XML when calling editor.setData()
but editor.getData()
still returns HTML, instead of XML. The data is not contained in a root element, and <img>
tags are not closed.
The toData
method which should convert to XML, is implemented as follows which doesn't look like something which could ever work since it tries to use _htmlWriter
to convert to XML. So it just looks like a feature nobody ever implemented.
toData( viewFragment ) {
// Convert view DocumentFragment to DOM DocumentFragment.
const domFragment = this._domConverter.viewToDom( viewFragment, document );
// Convert DOM DocumentFragment to XML output.
// There is no need to use dedicated for XML serializing method because BasicHtmlWriter works well in this case.
return this._htmlWriter.getHtml( domFragment );
}
I am not aware of a CKEditor 5 method for getting data that outputs XML. However, a potential workaround would be to convert the HTML output to XML using XMLSerializer
For example (the node
variable is used below for convenience since XMLSerializer
requires a DOM Node as input rather than a HTML string):
const node = document.createElement('div');
ClassicEditor
.create(document.querySelector('#editor'))
.then((editor) => {
editor.ui.focusTracker.on('change:isFocused', (event, name, value) => {
if (!value) {
const serializer = new XMLSerializer();
node.innerHTML = editor.getData();
console.log(serializer.serializeToString(node));
}
});
})
.catch(error => {
console.error(error);
});
<script src="https://cdn.ckeditor.com/ckeditor5/11.2.0/classic/ckeditor.js"></script>
<div>Enter something and click outside the CKEditor to view the xml string in the console</div>
<textarea name="content" id="editor"></textarea>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With