CodeMirror:XML代码没有缩进

时间:2015-06-10 08:53:17

标签: javascript codemirror

我正在使用CodeMirror以XML模式显示XML,但代码不会自动缩进。

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

这就是我初始化Code​​Mirror的方法:

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

检查此jsFiddle链接是否有实时版本:https://jsfiddle.net/zrosfz7x

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

为了提供解决方案,我为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>

此处还有更新的jsFiddle:http://jsfiddle.net/zrosfz7x/3/