1
0

dtd.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. /*
  4. DTD mode
  5. Ported to CodeMirror by Peter Kroon <plakroon@gmail.com>
  6. Report bugs/issues here: https://github.com/codemirror/CodeMirror/issues
  7. GitHub: @peterkroon
  8. */
  9. (function(mod) {
  10. if (typeof exports == "object" && typeof module == "object") // CommonJS
  11. mod(require("../../lib/codemirror"));
  12. else if (typeof define == "function" && define.amd) // AMD
  13. define(["../../lib/codemirror"], mod);
  14. else // Plain browser env
  15. mod(CodeMirror);
  16. })(function(CodeMirror) {
  17. "use strict";
  18. CodeMirror.defineMode("dtd", function(config) {
  19. var indentUnit = config.indentUnit, type;
  20. function ret(style, tp) {type = tp; return style;}
  21. function tokenBase(stream, state) {
  22. var ch = stream.next();
  23. if (ch == "<" && stream.eat("!") ) {
  24. if (stream.eatWhile(/[\-]/)) {
  25. state.tokenize = tokenSGMLComment;
  26. return tokenSGMLComment(stream, state);
  27. } else if (stream.eatWhile(/[\w]/)) return ret("keyword", "doindent");
  28. } else if (ch == "<" && stream.eat("?")) { //xml declaration
  29. state.tokenize = inBlock("meta", "?>");
  30. return ret("meta", ch);
  31. } else if (ch == "#" && stream.eatWhile(/[\w]/)) return ret("atom", "tag");
  32. else if (ch == "|") return ret("keyword", "seperator");
  33. else if (ch.match(/[\(\)\[\]\-\.,\+\?>]/)) return ret(null, ch);//if(ch === ">") return ret(null, "endtag"); else
  34. else if (ch.match(/[\[\]]/)) return ret("rule", ch);
  35. else if (ch == "\"" || ch == "'") {
  36. state.tokenize = tokenString(ch);
  37. return state.tokenize(stream, state);
  38. } else if (stream.eatWhile(/[a-zA-Z\?\+\d]/)) {
  39. var sc = stream.current();
  40. if( sc.substr(sc.length-1,sc.length).match(/\?|\+/) !== null )stream.backUp(1);
  41. return ret("tag", "tag");
  42. } else if (ch == "%" || ch == "*" ) return ret("number", "number");
  43. else {
  44. stream.eatWhile(/[\w\\\-_%.{,]/);
  45. return ret(null, null);
  46. }
  47. }
  48. function tokenSGMLComment(stream, state) {
  49. var dashes = 0, ch;
  50. while ((ch = stream.next()) != null) {
  51. if (dashes >= 2 && ch == ">") {
  52. state.tokenize = tokenBase;
  53. break;
  54. }
  55. dashes = (ch == "-") ? dashes + 1 : 0;
  56. }
  57. return ret("comment", "comment");
  58. }
  59. function tokenString(quote) {
  60. return function(stream, state) {
  61. var escaped = false, ch;
  62. while ((ch = stream.next()) != null) {
  63. if (ch == quote && !escaped) {
  64. state.tokenize = tokenBase;
  65. break;
  66. }
  67. escaped = !escaped && ch == "\\";
  68. }
  69. return ret("string", "tag");
  70. };
  71. }
  72. function inBlock(style, terminator) {
  73. return function(stream, state) {
  74. while (!stream.eol()) {
  75. if (stream.match(terminator)) {
  76. state.tokenize = tokenBase;
  77. break;
  78. }
  79. stream.next();
  80. }
  81. return style;
  82. };
  83. }
  84. return {
  85. startState: function(base) {
  86. return {tokenize: tokenBase,
  87. baseIndent: base || 0,
  88. stack: []};
  89. },
  90. token: function(stream, state) {
  91. if (stream.eatSpace()) return null;
  92. var style = state.tokenize(stream, state);
  93. var context = state.stack[state.stack.length-1];
  94. if (stream.current() == "[" || type === "doindent" || type == "[") state.stack.push("rule");
  95. else if (type === "endtag") state.stack[state.stack.length-1] = "endtag";
  96. else if (stream.current() == "]" || type == "]" || (type == ">" && context == "rule")) state.stack.pop();
  97. else if (type == "[") state.stack.push("[");
  98. return style;
  99. },
  100. indent: function(state, textAfter) {
  101. var n = state.stack.length;
  102. if( textAfter.match(/\]\s+|\]/) )n=n-1;
  103. else if(textAfter.substr(textAfter.length-1, textAfter.length) === ">"){
  104. if(textAfter.substr(0,1) === "<")n;
  105. else if( type == "doindent" && textAfter.length > 1 )n;
  106. else if( type == "doindent")n--;
  107. else if( type == ">" && textAfter.length > 1)n;
  108. else if( type == "tag" && textAfter !== ">")n;
  109. else if( type == "tag" && state.stack[state.stack.length-1] == "rule")n--;
  110. else if( type == "tag")n++;
  111. else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule" && type === ">")n--;
  112. else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule")n;
  113. else if( textAfter.substr(0,1) !== "<" && textAfter.substr(0,1) === ">" )n=n-1;
  114. else if( textAfter === ">")n;
  115. else n=n-1;
  116. //over rule them all
  117. if(type == null || type == "]")n--;
  118. }
  119. return state.baseIndent + n * indentUnit;
  120. },
  121. electricChars: "]>"
  122. };
  123. });
  124. CodeMirror.defineMIME("application/xml-dtd", "dtd");
  125. });