我使用的是tinymce的代码示例插件,这里提到了我的自定义门户中的https://www.tinymce.com/docs/plugins/codesample/。
一切正常,直到我不得不“重新编辑”存储在数据库中的数据。
当我去编辑的时候,所有的数据都存储在
<pre><code><html> <p>Text</p> </html> </code></pre>
被剥离并仅显示为
<pre><code>Text </code></pre>
从"Tools > Source“查看时
我已经在我的文本区域中使用了"htmlentities“,比如:-
<textarea><?php echo htmlentities($content); ?></textarea>
它仍然剥离了codesample
插件创建的标签中使用的所有html标签。
常见问题解答: 1.第一次添加数据时,一切正常。2.只有当我进入编辑页面时才会出现问题。Tinymce只从codesample
插件中剥离HTML码。Rest所有使用代码样本添加的代码,如"css,python,php“等,都会显示在编辑器中。
发布于 2016-08-14 12:55:54
将数据插入到tinymce中的正确方法不是打印出来,甚至不是使用htmlentities
。考虑以下代码
<div class="editor" id="editor">
</div>
<script>
tinymce.init({
selector: 'div.editor',
theme: 'inlite',
plugins: 'image table link paste contextmenu textpattern autolink',
insert_toolbar: 'quickimage quicktable',
selection_toolbar: 'bold italic | quicklink h1 h2 h3 blockquote',
inline: true,
paste_data_images: true,
automatic_uploads: true,
init_instance_callback : function(ed) {
// ed.setContent("<h1>Title</h1><p>Content...</p>");
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
ed.setContent(xhttp.responseText);
}
};
xhttp.open("GET", "content.php", true);
xhttp.send();
},
content_css: [
'//www.tinymce.com/css/codepen.min.css'
]
});
</script>
并且文件content.php
应该只打印html内容
<?php
$content = ''; // Fetch from database
print $content;
?>
在初始化tinymce时,请注意init_instance_callback
中的函数。这就是在tinymce初始化之后调用的函数。现在,不是直接在编辑器中使用print
,而是在init_instance_callback
中进行ajax调用并在那里呈现它。我放了一个样例注释行,只是为了帮助你做同样的事情。这还将负责安全性验证(如果有,则不执行脚本标记)。
同时获取编辑器的内容,同时将其保存到数据库中
var content = tinyMCE.get('editor').getContent();
然后,您可以发出ajax post请求,将数据保存到数据库。
现在为什么我使用ajax很重要。我尝试了许多其他方法,在这些方法中我可以直接打印它。但这会导致一个安全缺陷,因为脚本标记可能会在编辑器初始化之前运行。
我在这个例子中使用了div,但即使对于文本区域,它也是一样的。另外,不要使用htmlentities,因为那样会对html内容进行转义,而您希望看到的是在tinymce中呈现的内容,而不是转义版本。
发布于 2016-08-18 18:39:24
加载TinyMCE后,使用setContent
函数添加内容:
setup: function (editor) {
editor.on('init', function () {
var theContent = '<pre><code><html> <p>Text</p> </html> </code></pre>';
this.setContent(theContent);
});
}
来源:Tags Get Removed When Using Codesample Plugin with TinyMCE
发布于 2019-01-27 12:45:30
很抱歉地说,我发现TinyMCE在处理“代码”方面太复杂了。我忍受了随处可见的大量&n b s p;,但我只是求助于为代码片段添加图片或链接到模式弹出窗口。
https://stackoverflow.com/questions/38852804
复制相似问题