Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

185 linhas
6.5KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var Pos = CodeMirror.Pos;
  13. function cmp(a, b) { return a.line - b.line || a.ch - b.ch; }
  14. var nameStartChar = "A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD";
  15. var nameChar = nameStartChar + "\-\:\.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";
  16. var xmlTagStart = new RegExp("<(/?)([" + nameStartChar + "][" + nameChar + "]*)", "g");
  17. function Iter(cm, line, ch, range) {
  18. this.line = line; this.ch = ch;
  19. this.cm = cm; this.text = cm.getLine(line);
  20. this.min = range ? Math.max(range.from, cm.firstLine()) : cm.firstLine();
  21. this.max = range ? Math.min(range.to - 1, cm.lastLine()) : cm.lastLine();
  22. }
  23. function tagAt(iter, ch) {
  24. var type = iter.cm.getTokenTypeAt(Pos(iter.line, ch));
  25. return type && /\btag\b/.test(type);
  26. }
  27. function nextLine(iter) {
  28. if (iter.line >= iter.max) return;
  29. iter.ch = 0;
  30. iter.text = iter.cm.getLine(++iter.line);
  31. return true;
  32. }
  33. function prevLine(iter) {
  34. if (iter.line <= iter.min) return;
  35. iter.text = iter.cm.getLine(--iter.line);
  36. iter.ch = iter.text.length;
  37. return true;
  38. }
  39. function toTagEnd(iter) {
  40. for (;;) {
  41. var gt = iter.text.indexOf(">", iter.ch);
  42. if (gt == -1) { if (nextLine(iter)) continue; else return; }
  43. if (!tagAt(iter, gt + 1)) { iter.ch = gt + 1; continue; }
  44. var lastSlash = iter.text.lastIndexOf("/", gt);
  45. var selfClose = lastSlash > -1 && !/\S/.test(iter.text.slice(lastSlash + 1, gt));
  46. iter.ch = gt + 1;
  47. return selfClose ? "selfClose" : "regular";
  48. }
  49. }
  50. function toTagStart(iter) {
  51. for (;;) {
  52. var lt = iter.ch ? iter.text.lastIndexOf("<", iter.ch - 1) : -1;
  53. if (lt == -1) { if (prevLine(iter)) continue; else return; }
  54. if (!tagAt(iter, lt + 1)) { iter.ch = lt; continue; }
  55. xmlTagStart.lastIndex = lt;
  56. iter.ch = lt;
  57. var match = xmlTagStart.exec(iter.text);
  58. if (match && match.index == lt) return match;
  59. }
  60. }
  61. function toNextTag(iter) {
  62. for (;;) {
  63. xmlTagStart.lastIndex = iter.ch;
  64. var found = xmlTagStart.exec(iter.text);
  65. if (!found) { if (nextLine(iter)) continue; else return; }
  66. if (!tagAt(iter, found.index + 1)) { iter.ch = found.index + 1; continue; }
  67. iter.ch = found.index + found[0].length;
  68. return found;
  69. }
  70. }
  71. function toPrevTag(iter) {
  72. for (;;) {
  73. var gt = iter.ch ? iter.text.lastIndexOf(">", iter.ch - 1) : -1;
  74. if (gt == -1) { if (prevLine(iter)) continue; else return; }
  75. if (!tagAt(iter, gt + 1)) { iter.ch = gt; continue; }
  76. var lastSlash = iter.text.lastIndexOf("/", gt);
  77. var selfClose = lastSlash > -1 && !/\S/.test(iter.text.slice(lastSlash + 1, gt));
  78. iter.ch = gt + 1;
  79. return selfClose ? "selfClose" : "regular";
  80. }
  81. }
  82. function findMatchingClose(iter, tag) {
  83. var stack = [];
  84. for (;;) {
  85. var next = toNextTag(iter), end, startLine = iter.line, startCh = iter.ch - (next ? next[0].length : 0);
  86. if (!next || !(end = toTagEnd(iter))) return;
  87. if (end == "selfClose") continue;
  88. if (next[1]) { // closing tag
  89. for (var i = stack.length - 1; i >= 0; --i) if (stack[i] == next[2]) {
  90. stack.length = i;
  91. break;
  92. }
  93. if (i < 0 && (!tag || tag == next[2])) return {
  94. tag: next[2],
  95. from: Pos(startLine, startCh),
  96. to: Pos(iter.line, iter.ch)
  97. };
  98. } else { // opening tag
  99. stack.push(next[2]);
  100. }
  101. }
  102. }
  103. function findMatchingOpen(iter, tag) {
  104. var stack = [];
  105. for (;;) {
  106. var prev = toPrevTag(iter);
  107. if (!prev) return;
  108. if (prev == "selfClose") { toTagStart(iter); continue; }
  109. var endLine = iter.line, endCh = iter.ch;
  110. var start = toTagStart(iter);
  111. if (!start) return;
  112. if (start[1]) { // closing tag
  113. stack.push(start[2]);
  114. } else { // opening tag
  115. for (var i = stack.length - 1; i >= 0; --i) if (stack[i] == start[2]) {
  116. stack.length = i;
  117. break;
  118. }
  119. if (i < 0 && (!tag || tag == start[2])) return {
  120. tag: start[2],
  121. from: Pos(iter.line, iter.ch),
  122. to: Pos(endLine, endCh)
  123. };
  124. }
  125. }
  126. }
  127. CodeMirror.registerHelper("fold", "xml", function(cm, start) {
  128. var iter = new Iter(cm, start.line, 0);
  129. for (;;) {
  130. var openTag = toNextTag(iter)
  131. if (!openTag || iter.line != start.line) return
  132. var end = toTagEnd(iter)
  133. if (!end) return
  134. if (!openTag[1] && end != "selfClose") {
  135. var startPos = Pos(iter.line, iter.ch);
  136. var endPos = findMatchingClose(iter, openTag[2]);
  137. return endPos && cmp(endPos.from, startPos) > 0 ? {from: startPos, to: endPos.from} : null
  138. }
  139. }
  140. });
  141. CodeMirror.findMatchingTag = function(cm, pos, range) {
  142. var iter = new Iter(cm, pos.line, pos.ch, range);
  143. if (iter.text.indexOf(">") == -1 && iter.text.indexOf("<") == -1) return;
  144. var end = toTagEnd(iter), to = end && Pos(iter.line, iter.ch);
  145. var start = end && toTagStart(iter);
  146. if (!end || !start || cmp(iter, pos) > 0) return;
  147. var here = {from: Pos(iter.line, iter.ch), to: to, tag: start[2]};
  148. if (end == "selfClose") return {open: here, close: null, at: "open"};
  149. if (start[1]) { // closing tag
  150. return {open: findMatchingOpen(iter, start[2]), close: here, at: "close"};
  151. } else { // opening tag
  152. iter = new Iter(cm, to.line, to.ch, range);
  153. return {open: here, close: findMatchingClose(iter, start[2]), at: "open"};
  154. }
  155. };
  156. CodeMirror.findEnclosingTag = function(cm, pos, range, tag) {
  157. var iter = new Iter(cm, pos.line, pos.ch, range);
  158. for (;;) {
  159. var open = findMatchingOpen(iter, tag);
  160. if (!open) break;
  161. var forward = new Iter(cm, pos.line, pos.ch, range);
  162. var close = findMatchingClose(forward, open.tag);
  163. if (close) return {open: open, close: close};
  164. }
  165. };
  166. // Used by addon/edit/closetag.js
  167. CodeMirror.scanForClosingTag = function(cm, pos, name, end) {
  168. var iter = new Iter(cm, pos.line, pos.ch, end ? {from: 0, to: end} : null);
  169. return findMatchingClose(iter, name);
  170. };
  171. });