Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting xml from ckeditor 5

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 );
}
like image 839
MTilsted Avatar asked Sep 25 '18 13:09

MTilsted


1 Answers

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>
like image 53
benvc Avatar answered Nov 02 '22 15:11

benvc