从 ckeditor 5 获取 xml
Getting xml from ckeditor 5
如何从 ckeditor 获得 XML 而不是 HTML 的输出?
我以为我可以用
editor.data.processor=new XmlDataProcessor();
但这似乎只适用于编辑器现在在调用 editor.setData()
时需要 XML 但 editor.getData()
仍然 returns HTML 的输入,而不是XML。数据未包含在根元素中,并且 <img>
标记未关闭。
应该转换为 XML 的 toData
方法实现如下,它看起来不像是可以工作的东西,因为它试图使用 _htmlWriter
转换为XML。所以它看起来像是一个从未实现过的功能。
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 );
}
我不知道 CKEditor 5 method for getting data that outputs XML. However, a potential workaround would be to convert the HTML output to XML using XMLSerializer
例如(为方便起见,下面使用了 node
变量,因为 XMLSerializer
需要 DOM Node 作为输入而不是 HTML 字符串:
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>
如何从 ckeditor 获得 XML 而不是 HTML 的输出?
我以为我可以用
editor.data.processor=new XmlDataProcessor();
但这似乎只适用于编辑器现在在调用 editor.setData()
时需要 XML 但 editor.getData()
仍然 returns HTML 的输入,而不是XML。数据未包含在根元素中,并且 <img>
标记未关闭。
应该转换为 XML 的 toData
方法实现如下,它看起来不像是可以工作的东西,因为它试图使用 _htmlWriter
转换为XML。所以它看起来像是一个从未实现过的功能。
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 );
}
我不知道 CKEditor 5 method for getting data that outputs XML. However, a potential workaround would be to convert the HTML output to XML using XMLSerializer
例如(为方便起见,下面使用了 node
变量,因为 XMLSerializer
需要 DOM Node 作为输入而不是 HTML 字符串:
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>