Search code examples
javascriptcodemirror

CodeMirror: XML code not being indented


I am using CodeMirror to display XML in XML mode, but the code is not being automatically indented.

I checked and the XML mode does implement indent(state, textAfter, fullLine), which handles the indenting, so it should be working.

This is how I am initializing CodeMirror:

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

Check this jsFiddle link for a live version: https://jsfiddle.net/zrosfz7x.

Any ideas?


Solution

  • In order to provide a solution I have added an external beautifier for xml. Here's a complete working example.

    <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>
    

    also here the updated jsFiddle: http://jsfiddle.net/zrosfz7x/3/