Encoding of entities - no perfect solution?

时间:2015-04-23 05:45:38

标签: ckeditor redmine

We use the ckeditor plugin for redmine (https://github.com/a-ono/redmine_ckeditor) and experience the following problem:

  • if encoding of basicEntities / entities is set to false, xml/html text is not shown correctly when stored via the CKEditor (https://github.com/a-ono/redmine_ckeditor/issues/158), there also is a problem with pressing the show sourcecode button or editing content repeatedly
  • if these parameters are not set, problems with quotes in code blocks (shown as &quot), links with parameters (& is incorrectly encoded) and wiki links with umlauts or other accentuated letters are broken (https://github.com/a-ono/redmine_ckeditor/issues/132)

As a-ono, the dev of the plugin, puts it: "There seems to be no perfect solution."

I found http://komlenic.com/246/encoding-entities-to-work-with-ckeditor-3/ , http://ckeditor.com/forums/Support/inside-tries-create-paragraph#comment-54348 and some additional information about "forceSimpleAmpersand:true" as well as config.entities_latin = false;, but I'm not too sure how to proceed. We are in the process of moving additional users to Redmine, but it's quite a show-stopper if they are not able to post links or xml/html content (e.g. as part of an error report)

So this goes out to all CKEditor Pros. Any hints?

2 个答案:

答案 0 :(得分:2)

很抱歉这说,但看起来Redmine的插件坏了。 CKEditor本身无需执行任何操作 - 它可以正确读取和写入实体如果数据正确加载。如果某些实体在将它们保存到数据库并将其加载回来后被解码或编码太多,则意味着后端已损坏。而不是触及默认情况下完全正常的CKEditor选项,后端应该是固定的(意味着Redmine的插件,或者更不可能是Redmine本身的插件)。

让我们考虑以下情况。您想写下有关<xml>标记的评论。它的HTML将是:

<p>This is a tag: <code>&lt;xml&gt;</code>.</p>

如果使用与editor.getData()的自动集成,这也是<textarea>或者发布到服务器的内容。

现在,如果你这样做(例如在demo中):

editor.setData( '<p>This is a tag: <code>&lt;xml&gt;</code>.</p>' );
一切都会好起来的。将显示相同的内容:

Loading data using editor.setData()

然而,许多开发人员使用CKEditor与<textarea>的集成,不幸的是,他们并不完全了解它是如何工作的。让我们将存储在数据库中的确切数据加载到textarea:

<textarea><p>This is a tag: <code>&lt;xml&gt;</code>.</p></textarea>

如果您现在尝试使用textarea.value从JavaScript读取此textarea的值,您将获得:

<p>This is a tag: <code><xml></code>.</p>

正如您所看到的,<xml>标记的编码已丢失,因为HTML &lt;中的编码被视为<

因此,您需要做的是在将数据加载到textarea之前再次对数据进行编码:

<textarea>&lt;p&gt;This is a tag: &lt;code&gt;&amp;lt;xml&amp;gt;&lt;/code&gt;.&lt;/p&gt;</textarea>

现在看到所有<个字符变为&lt;,但已编码的&lt;变为&amp;lt;。这将确保在将数据打印到HTML时正确保留所有实体。如您所见,它与CKEditor无关。这同样适用于显示在CKEditor中创建的数据 - 必须保留编码。如果丢失某些东西,则意味着后端对数据进行编码或解码,但不应该。

答案 1 :(得分:0)

我们有一位用户坚持使用python解释器&#34;&gt;&gt;&gt;&#34;在他的python代码块中的维基。 这些转换为HTML实体&amp; gt;&amp; gt;&amp; gt;保存到wiki数据库时。查看时,这些不会转换回&#34;&gt;&gt;&gt;&#34;因为它在&lt; pre&gt;内块。

我发现更改插件/ redmine_ckeditor / lib / redmine_ckeditor / wiki_formatting / formatter.rb第17行:

    %Q[<pre>\n<code class="#{lang} syntaxhl">#{
      Redmine::SyntaxHighlighting.highlight_by_language(code, lang)
    }</code>\n</pre>]

    %Q[<pre>\n<code class="#{lang} syntaxhl">#{
      Redmine::SyntaxHighlighting.highlight_by_language(CGI.unescapeHTML(code), lang)
    }</code>\n</pre>]

解决了代码中HTML实体的问题。