You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.9 KiB

5 months ago
  1. /**
  2. * Helper to test CodeMirror highlighting modes. It pretty prints output of the
  3. * highlighter and can check against expected styles.
  4. *
  5. * Mode tests are registered by calling test.mode(testName, mode,
  6. * tokens), where mode is a mode object as returned by
  7. * CodeMirror.getMode, and tokens is an array of lines that make up
  8. * the test.
  9. *
  10. * These lines are strings, in which styled stretches of code are
  11. * enclosed in brackets `[]`, and prefixed by their style. For
  12. * example, `[keyword if]`. Brackets in the code itself must be
  13. * duplicated to prevent them from being interpreted as token
  14. * boundaries. For example `a[[i]]` for `a[i]`. If a token has
  15. * multiple styles, the styles must be separated by ampersands, for
  16. * example `[tag&error </hmtl>]`.
  17. *
  18. * See the test.js files in the css, markdown, gfm, and stex mode
  19. * directories for examples.
  20. */
  21. (function() {
  22. function findSingle(str, pos, ch) {
  23. for (;;) {
  24. var found = str.indexOf(ch, pos);
  25. if (found == -1) return null;
  26. if (str.charAt(found + 1) != ch) return found;
  27. pos = found + 2;
  28. }
  29. }
  30. var styleName = /[\w&-_]+/g;
  31. function parseTokens(strs) {
  32. var tokens = [], plain = "";
  33. for (var i = 0; i < strs.length; ++i) {
  34. if (i) plain += "\n";
  35. var str = strs[i], pos = 0;
  36. while (pos < str.length) {
  37. var style = null, text;
  38. if (str.charAt(pos) == "[" && str.charAt(pos+1) != "[") {
  39. styleName.lastIndex = pos + 1;
  40. var m = styleName.exec(str);
  41. style = m[0].replace(/&/g, " ");
  42. var textStart = pos + style.length + 2;
  43. var end = findSingle(str, textStart, "]");
  44. if (end == null) throw new Error("Unterminated token at " + pos + " in '" + str + "'" + style);
  45. text = str.slice(textStart, end);
  46. pos = end + 1;
  47. } else {
  48. var end = findSingle(str, pos, "[");
  49. if (end == null) end = str.length;
  50. text = str.slice(pos, end);
  51. pos = end;
  52. }
  53. text = text.replace(/\[\[|\]\]/g, function(s) {return s.charAt(0);});
  54. tokens.push({style: style, text: text});
  55. plain += text;
  56. }
  57. }
  58. return {tokens: tokens, plain: plain};
  59. }
  60. test.mode = function(name, mode, tokens, modeName) {
  61. var data = parseTokens(tokens);
  62. return test((modeName || mode.name) + "_" + name, function() {
  63. return compare(data.plain, data.tokens, mode);
  64. });
  65. };
  66. function esc(str) {
  67. return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
  68. }
  69. function compare(text, expected, mode) {
  70. var expectedOutput = [];
  71. for (var i = 0; i < expected.length; ++i) {
  72. var sty = expected[i].style;
  73. if (sty && sty.indexOf(" ")) sty = sty.split(' ').sort().join(' ');
  74. expectedOutput.push({style: sty, text: expected[i].text});
  75. }
  76. var observedOutput = highlight(text, mode);
  77. var s = "";
  78. var diff = highlightOutputsDifferent(expectedOutput, observedOutput);
  79. if (diff != null) {
  80. s += '<div class="mt-test mt-fail">';
  81. s += '<pre>' + esc(text) + '</pre>';
  82. s += '<div class="cm-s-default">';
  83. s += 'expected:';
  84. s += prettyPrintOutputTable(expectedOutput, diff);
  85. s += 'observed: [<a onclick="this.parentElement.className+=\' mt-state-unhide\'">display states</a>]';
  86. s += prettyPrintOutputTable(observedOutput, diff);
  87. s += '</div>';
  88. s += '</div>';
  89. }
  90. if (observedOutput.indentFailures) {
  91. for (var i = 0; i < observedOutput.indentFailures.length; i++)
  92. s += "<div class='mt-test mt-fail'>" + esc(observedOutput.indentFailures[i]) + "</div>";
  93. }
  94. if (s) throw new Failure(s);
  95. }
  96. function stringify(obj) {
  97. function replacer(key, obj) {
  98. if (typeof obj == "function") {
  99. var m = obj.toString().match(/function\s*[^\s(]*/);
  100. return m ? m[0] : "function";
  101. }
  102. return obj;
  103. }
  104. if (window.JSON && JSON.stringify)
  105. return JSON.stringify(obj, replacer, 2);
  106. return "[unsupported]"; // Fail safely if no native JSON.
  107. }
  108. function highlight(string, mode) {
  109. var state = mode.startState();
  110. var lines = string.replace(/\r\n/g,'\n').split('\n');
  111. var st = [], pos = 0;
  112. for (var i = 0; i < lines.length; ++i) {
  113. var line = lines[i], newLine = true;
  114. if (mode.indent) {
  115. var ws = line.match(/^\s*/)[0];
  116. var indent = mode.indent(state, line.slice(ws.length), line);
  117. if (indent != CodeMirror.Pass && indent != ws.length)
  118. (st.indentFailures || (st.indentFailures = [])).push(
  119. "Indentation of line " + (i + 1) + " is " + indent + " (expected " + ws.length + ")");
  120. }
  121. var stream = new CodeMirror.StringStream(line, 4, {
  122. lookAhead: function(n) { return lines[i + n] }
  123. });
  124. if (line == "" && mode.blankLine) mode.blankLine(state);
  125. /* Start copied code from CodeMirror.highlight */
  126. while (!stream.eol()) {
  127. for (var j = 0; j < 10 && stream.start >= stream.pos; j++)
  128. var compare = mode.token(stream, state);
  129. if (j == 10)
  130. throw new Failure("Failed to advance the stream." + stream.string + " " + stream.pos);
  131. var substr = stream.current();
  132. if (compare && compare.indexOf(" ") > -1) compare = compare.split(' ').sort().join(' ');
  133. stream.start = stream.pos;
  134. if (pos && st[pos-1].style == compare && !newLine) {
  135. st[pos-1].text += substr;
  136. } else if (substr) {
  137. st[pos++] = {style: compare, text: substr, state: stringify(state)};
  138. }
  139. // Give up when line is ridiculously long
  140. if (stream.pos > 5000) {
  141. st[pos++] = {style: null, text: this.text.slice(stream.pos)};
  142. break;
  143. }
  144. newLine = false;
  145. }
  146. }
  147. return st;
  148. }
  149. function highlightOutputsDifferent(o1, o2) {
  150. var minLen = Math.min(o1.length, o2.length);
  151. for (var i = 0; i < minLen; ++i)
  152. if (o1[i].style != o2[i].style || o1[i].text != o2[i].text) return i;
  153. if (o1.length > minLen || o2.length > minLen) return minLen;
  154. }
  155. function prettyPrintOutputTable(output, diffAt) {
  156. var s = '<table class="mt-output">';
  157. s += '<tr>';
  158. for (var i = 0; i < output.length; ++i) {
  159. var style = output[i].style, val = output[i].text;
  160. s +=
  161. '<td class="mt-token"' + (i == diffAt ? " style='background: pink'" : "") + '>' +
  162. '<span class="cm-' + esc(String(style)) + '">' +
  163. esc(val.replace(/ /g,'\xb7')) + // · MIDDLE DOT
  164. '</span>' +
  165. '</td>';
  166. }
  167. s += '</tr><tr>';
  168. for (var i = 0; i < output.length; ++i) {
  169. s += '<td class="mt-style"><span>' + (output[i].style || null) + '</span></td>';
  170. }
  171. if(output[0].state) {
  172. s += '</tr><tr class="mt-state-row" title="State AFTER each token">';
  173. for (var i = 0; i < output.length; ++i) {
  174. s += '<td class="mt-state"><pre>' + esc(output[i].state) + '</pre></td>';
  175. }
  176. }
  177. s += '</tr></table>';
  178. return s;
  179. }
  180. })();