CodeMirror: XML代码没有缩进

CodeMirror: XML code not being indented

本文关键字:缩进 代码 XML CodeMirror      更新时间:2023-09-26

我使用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
});

查看jsFiddle链接获取实时版本:https://jsfiddle.net/zrosfz7x.

任何想法?

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