【问题标题】:What are the requirements for /mode/html in Ace editor?Ace 编辑器对 /mode/html 有什么要求?
【发布时间】:2014-01-19 03:13:34
【问题描述】:

我可以让mode/javascript 与:

JS
https://github.com/ajaxorg/ace-builds/blob/master/src-noconflict/ace.js

主题
https://github.com/ajaxorg/ace-builds/blob/master/src-noconflict/theme-tomorrow.js

模式
https://github.com/ajaxorg/ace-builds/blob/master/src-noconflict/mode-javascript.js

工人
https://raw.github.com/ajaxorg/ace-builds/master/src-noconflict/worker-javascript.js

HTML

<script src="/static/js/ace/ace.js"></script>

<div class="my_ace_editor">
function foo(items) {
    var x = "All this is syntax highlighted";
    return x;
}
</div>

CSS

#my_ace_editor {
    position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
}

jQuery

$(document).ready(function() {
    var editor = ace.edit("my_ace_editor");
    editor.setTheme("ace/theme/tomorrow");
    editor.getSession().setMode("ace/mode/javascript");
});

但是在尝试显示 HTML 时没有任何运气:

HTML

<div id="my_ace_editor"><p>test</p></div>

jQuery

$(document).ready(function() {
    var editor = ace.edit("my_ace_editor");
    editor.setTheme("ace/theme/tomorrow");
    editor.getSession().setMode("ace/mode/html");
});

并已添加:

https://raw.github.com/ajaxorg/ace-builds/master/src-noconflict/mode-html.js

如果我在 HTML 编辑器中输入,标签和内容会应用语法高亮显示。

但它在加载时并没有显示实际的 HTML - 它只显示test

没有 Firebug 错误,我可以看到应该在 Firebug 中突出显示的实际 HTML。

是否需要其他文件或设置?

编辑:如果我使用,我可以在 Ace 编辑器中正确显示 HTML 内容:

editor.setValue("<html>test</html>",-1);

但我需要能够从 HTML 本身而不是在 jQuery 中设置值。

内容正在从数据库中加载(即它是“动态内容”),我不确定这是否会有所不同?

【问题讨论】:

    标签: ace-editor


    【解决方案1】:

    如果您不想转义 HTML 代码,只需使用带有以下类型和样式集的 script 标记

    <script type="text/plain" style="display: block;" id="ace-1">
      <a href="test.html">hello world</a>
      test test
    </script> 
    

    【讨论】:

      【解决方案2】:

      你需要转义html,而不是

      <div id="my_ace_editor"><p>html&test</p></div>
      

      应该是

      <div id="my_ace_editor">&lt;p>html&amp;test&lt;/p></div>
      

      这适用于所有模式,而不仅仅是 html

      【讨论】:

      • 我正在使用 MongoDD。如果我直接从 shell 将此 HTML 实体 (&amp;lt;) 插入数据库,Ace 编辑器将正常工作。使用 RockMongo(如 MongoDB 的 phpMyAdmin)时,它会将 HTML 实体转换为相应的字符(即 &amp;lt;&amp;lt;),然后 Ace 编辑器不会产生所需的结果。如何在 RockMongo 界面中转义 &amp;lt; - 这是特定的 RockMongo 问题吗?
      • 或者更确切地说,我如何强制 RockMongo 保留 &amp;lt; 而不是将其转换为 &amp;lt;
      • 你不应该在数据库中改变它,如果你使用 php 使用 htmlspecialchars 函数am1.php.net/…。另请注意,此问题与 ace 无关,如果您想在 div 中显示文本,则需要应用相同的转义。
      • 应用程序在 Python 和 MongoDB 上运行(动态查询结果以 JSON 格式转储并返回到 jQuery 中的getJSON() 函数)。我希望在使用 Ace 编辑器时,我只需要定义包含代码(从数据库加载)的 div,它就会在 Ace 编辑器界面中正确呈现。我接受了答案,因为它回答了原始问题。也许我需要查看从 Python 到 jQuery 的内容编码,或者可能是 jQuery 本身。当我找到解决方案时,我会更新原始帖子。
      • 你如何“定义包含代码的 div(从数据库加载)”你有没有想过如果你的代码有 &lt;script&gt;alert("hi")&lt;/script&gt; 会发生什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      相关资源
      最近更新 更多