简体   繁体   English

在TinyMCE中使用Codesample插件时,标签会被删除

[英]Tags Get Removed When Using Codesample Plugin with TinyMCE

Since 4.3.0 TinyMCE includes Codesample plugin that lets you enter code snippets. 从4.3.0版本开始,TinyMCE包含Codesample插件,可让您输入代码片段。 This works very well for languages like Java, PHP, C# etc. that are not directly running in the browser. 这对于未直接在浏览器中运行的语言(例如Java,PHP,C#等)非常有效。 You save your code snippet to the server, load it back into the browser, edit it, and save it back to the server again - no hassle. 您可以将代码段保存到服务器,然后将其重新加载到浏览器中,对其进行编辑,然后再次将其保存回服务器-无需麻烦。

If you want to do it with HTML, JavaScript, or XML, then it seems not to be possible to load the code snippet back into the browser after saving it to the server. 如果要使用HTML,JavaScript或XML进行操作,则在将代码段保存到服务器后似乎无法将其重新加载到浏览器中。 Most of the tags will be removed, despite being already encoded before. 尽管之前已进行过编码,但大多数标签都将被删除。

See TinyMCE Fiddle 1 and TinyMCE Fiddle 2 that try to illustrate the problem. 请参阅试图说明该问题的TinyMCE Fiddle 1TinyMCE Fiddle 2

Any ideas? 有任何想法吗? Many thanks in advance! 提前谢谢了!

If you want to reload content into TinyMCE that was previously stored in your database I would use TinyMCE's JavaScript APIs to load the data after the editor is initialized. 如果您想将先前存储在数据库中的内容重新加载到TinyMCE中,我将在初始化编辑器后使用TinyMCE的JavaScript API加载数据。 I have created a fiddle to show how you would do this. 我创建了一个小提琴来展示您将如何执行此操作。

http://fiddle.tinymce.com/50faab/3 http://fiddle.tinymce.com/50faab/3

In this example the content that would have come out of TinyMCE from a prior edit is in the theContent variable: 在此示例中,先前的编辑可能从TinyMCE中产生的内容位于theContent变量中:

var theContent = '<pre class="language-markup"><code>&lt;ul&gt;&lt;li&gt;one&lt;/li&gt;&lt;li&gt;two&lt;/li&gt;&lt;/ul&gt;</code></pre>';

(In a real application you would of course grab this from the database and inject it into the web page instead of hard coding the value) (在真实的应用程序中,您当然会从数据库中获取它,然后将其注入到网页中,而不是对值进行硬编码)

I then use the TinyMCE API for setContent to add the content to TinyMCE once its loaded: 然后,我将TinyMCE API用于setContent,以在内容加载后将其添加到TinyMCE:

setup: function (editor) {
    editor.on('init', function () {
    var theContent = '<pre class="language-markup"><code>&lt;ul&gt;&lt;li&gt;one&lt;/li&gt;&lt;li&gt;two&lt;/li&gt;&lt;/ul&gt;</code></pre>';
        this.setContent(theContent);
    });
}

When you do it this way the editor will properly syntax highlight the code sample content (as seen in the Fiddle). 当您这样做时,编辑器将正确地语法突出显示代码示例内容(如Fiddle中所示)。

<textarea> tags and HTML are a difficult combination if you have anything beyond the simplest of HTML so I would avoid dropping HTML directly into the <textarea> . 如果您没有最简单的HTML内容,则<textarea>标记和HTML很难组合,因此我将避免将HTML直接放入<textarea>

Expanding on Michael's answer, putting your content into a hidden DIV, then grabbing it's html works better for me: 扩展Michael的答案,将您的内容放入隐藏的DIV中,然后获取它的html对我来说更好:

<div class="theDiv">
    <pre class="language-markup"> 
       <code>&lt;ul&gt;&lt;li&gt;one&lt;/li&gt;&lt;li&gt;two&lt;/li&gt;&lt;/ul&gt;</code>
    </pre>
</div>

var theContent = $('.theDiv').html();

The answer of Felix Riesterer in the forum of TinyMCE might be of help as well: Felix Riesterer在TinyMCE论坛上的回答也可能会有所帮助:

Yes, in the way I pointed out: < > and " need to be properly encoded as < > and " because these characters have a special meaning in HTML context. 是的,以我指出的方式:<>和“必须正确编码为<>和”,因为这些字符在HTML上下文中具有特殊含义。 So if you want to comply with the specs (and TinyMCE expects you to do so!) you need to convert these characters accordingly. 因此,如果您想遵守规范(并且TinyMCE希望您这样做!),则需要相应地转换这些字符。 There is no way around that. 没有办法解决。

And yes, I forgot to mention that & (ampersand) needs to be encoded as & as well. 是的,我忘了提到&(与号)也需要编码为&。

You might have to double-encode stuff: If you want to see "" you need to code &lt;h1&gt; 您可能需要对内容进行双重编码:如果要查看“”,则需要对&lt; h1&gt; so the HTML code renders as plain text. 因此HTML代码呈现为纯文本。 The logic behind it is like this: 其背后的逻辑是这样的:

1. content gets decoded into raw text (&lt; gets translated to <, &amp; becomes &) 1.内容被解码为原始文本(&lt;被翻译为<,&amp;成为&)

2.raw text gets treated as original HTML code for the editor contents (&lt;h1&gt; renders as <h1>, <h1> renders as a first-level heading ) 2.原始文本被视为编辑器内容的原始HTML代码(&lt; h1&gt;呈现为<h1>,<h1>呈现为第一级标题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM