mode_test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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('&', '&amp;').replace('<', '&lt;').replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
  68. ;
  69. }
  70. function compare(text, expected, mode) {
  71. var expectedOutput = [];
  72. for (var i = 0; i < expected.length; ++i) {
  73. var sty = expected[i].style;
  74. if (sty && sty.indexOf(" ")) sty = sty.split(' ').sort().join(' ');
  75. expectedOutput.push({style: sty, text: expected[i].text});
  76. }
  77. var observedOutput = highlight(text, mode);
  78. var s = "";
  79. var diff = highlightOutputsDifferent(expectedOutput, observedOutput);
  80. if (diff != null) {
  81. s += '<div class="mt-test mt-fail">';
  82. s += '<pre>' + esc(text) + '</pre>';
  83. s += '<div class="cm-s-default">';
  84. s += 'expected:';
  85. s += prettyPrintOutputTable(expectedOutput, diff);
  86. s += 'observed: [<a onclick="this.parentElement.className+=\' mt-state-unhide\'">display states</a>]';
  87. s += prettyPrintOutputTable(observedOutput, diff);
  88. s += '</div>';
  89. s += '</div>';
  90. }
  91. if (observedOutput.indentFailures) {
  92. for (var i = 0; i < observedOutput.indentFailures.length; i++)
  93. s += "<div class='mt-test mt-fail'>" + esc(observedOutput.indentFailures[i]) + "</div>";
  94. }
  95. if (s) throw new Failure(s);
  96. }
  97. function stringify(obj) {
  98. function replacer(key, obj) {
  99. if (typeof obj == "function") {
  100. var m = obj.toString().match(/function\s*[^\s(]*/);
  101. return m ? m[0] : "function";
  102. }
  103. return obj;
  104. }
  105. if (window.JSON && JSON.stringify)
  106. return JSON.stringify(obj, replacer, 2);
  107. return "[unsupported]"; // Fail safely if no native JSON.
  108. }
  109. function highlight(string, mode) {
  110. var state = mode.startState();
  111. var lines = string.replace(/\r\n/g,'\n').split('\n');
  112. var st = [], pos = 0;
  113. for (var i = 0; i < lines.length; ++i) {
  114. var line = lines[i], newLine = true;
  115. if (mode.indent) {
  116. var ws = line.match(/^\s*/)[0];
  117. var indent = mode.indent(state, line.slice(ws.length));
  118. if (indent != CodeMirror.Pass && indent != ws.length)
  119. (st.indentFailures || (st.indentFailures = [])).push(
  120. "Indentation of line " + (i + 1) + " is " + indent + " (expected " + ws.length + ")");
  121. }
  122. var stream = new CodeMirror.StringStream(line);
  123. if (line == "" && mode.blankLine) mode.blankLine(state);
  124. /* Start copied code from CodeMirror.highlight */
  125. while (!stream.eol()) {
  126. for (var j = 0; j < 10 && stream.start >= stream.pos; j++)
  127. var compare = mode.token(stream, state);
  128. if (j == 10)
  129. throw new Failure("Failed to advance the stream." + stream.string + " " + stream.pos);
  130. var substr = stream.current();
  131. if (compare && compare.indexOf(" ") > -1) compare = compare.split(' ').sort().join(' ');
  132. stream.start = stream.pos;
  133. if (pos && st[pos-1].style == compare && !newLine) {
  134. st[pos-1].text += substr;
  135. } else if (substr) {
  136. st[pos++] = {style: compare, text: substr, state: stringify(state)};
  137. }
  138. // Give up when line is ridiculously long
  139. if (stream.pos > 5000) {
  140. st[pos++] = {style: null, text: this.text.slice(stream.pos)};
  141. break;
  142. }
  143. newLine = false;
  144. }
  145. }
  146. return st;
  147. }
  148. function highlightOutputsDifferent(o1, o2) {
  149. var minLen = Math.min(o1.length, o2.length);
  150. for (var i = 0; i < minLen; ++i)
  151. if (o1[i].style != o2[i].style || o1[i].text != o2[i].text) return i;
  152. if (o1.length > minLen || o2.length > minLen) return minLen;
  153. }
  154. function prettyPrintOutputTable(output, diffAt) {
  155. var s = '<table class="mt-output">';
  156. s += '<tr>';
  157. for (var i = 0; i < output.length; ++i) {
  158. var style = output[i].style, val = output[i].text;
  159. s +=
  160. '<td class="mt-token"' + (i == diffAt * 2 ? " style='background: pink'" : "") + '>' +
  161. '<span class="cm-' + esc(String(style)) + '">' +
  162. esc(val.replace(/ /g,'\xb7')) + // · MIDDLE DOT
  163. '</span>' +
  164. '</td>';
  165. }
  166. s += '</tr><tr>';
  167. for (var i = 0; i < output.length; ++i) {
  168. s += '<td class="mt-style"><span>' + (output[i].style || null) + '</span></td>';
  169. }
  170. if(output[0].state) {
  171. s += '</tr><tr class="mt-state-row" title="State AFTER each token">';
  172. for (var i = 0; i < output.length; ++i) {
  173. s += '<td class="mt-state"><pre>' + esc(output[i].state) + '</pre></td>';
  174. }
  175. }
  176. s += '</tr></table>';
  177. return s;
  178. }
  179. })();