CodeMirror:未缩进 XML 代码

Gen*_*liu 4 javascript codemirror

我使用 CodeMirror 以 XML 模式显示 XML,但代码没有自动缩进。

我检查过,XML 模式确实实现了indent(state, textAfter, fullLine),它处理缩进,所以它应该可以工作。

这就是我初始化 CodeMirror 的方式:

CodeMirror.fromTextArea(document.getElementById("test"), {
    mode: 'application/xml',
    theme: 'eclipse',
    lineNumbers: true,
    lineWrapping: true,
    readOnly: true,
    cursorBlinkRate: -1
});
Run Code Online (Sandbox Code Playgroud)

检查此 jsFiddle 链接以获取实时版本:https ://jsfiddle.net/zrosfz7x 。

有任何想法吗?

Nil*_*lsB 9

为了提供解决方案,我为 xml 添加了一个外部美化器。这是一个完整的工作示例。

<html>
<head>
    <meta charset="UTF-8">
    <link rel=stylesheet href="//codemirror.net/lib/codemirror.css">
    <script src=//codemirror.net/lib/codemirror.js></script>
    <script src=//codemirror.net/mode/xml/xml.js></script>
    <script src="//cdn.rawgit.com/vkiryukhin/vkBeautify/master/vkbeautify.js"></script>
</head>
<body>  
  <textarea id="test"><?xml version="1.0" encoding="UTF-8" ?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note></textarea>
  <script>
    document.getElementById("test").value = vkbeautify.xml(document.getElementById("test").value);
    CodeMirror.fromTextArea(document.getElementById("test"), {
      mode: 'application/xml',
      // theme: 'eclipse',
      lineNumbers: true,
      lineWrapping: true,
      readOnly: true,
      cursorBlinkRate: -1
    });    
  </script>
</body>    
</html>
Run Code Online (Sandbox Code Playgroud)

还有这里更新的 jsFiddle:http : //jsfiddle.net/zrosfz7x/3/