html-hint-test.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function() {
  4. var Pos = CodeMirror.Pos;
  5. namespace = "html-hint_";
  6. testData =[
  7. {
  8. name: "html-element",
  9. value: "<htm",
  10. list: ["<html"]
  11. },
  12. {
  13. name: "element-close",
  14. value: "<a href='#a'>\n</",
  15. list: ["</a>"]
  16. },
  17. {
  18. name: "linkref-attribute",
  19. value: "<link hreflang='z",
  20. from: Pos(0,"<link hreflang=".length),
  21. list: ["'zh'","'za'","'zu'"]
  22. },
  23. {
  24. name: "html-completion",
  25. value: "<html>\n",
  26. list: ["<head","<body","</html>"]
  27. }
  28. ];
  29. function escapeHtmlList(o) {
  30. return '<code>' +
  31. JSON.stringify(o.list,null,2)
  32. .replace(/</g, "&lt;")
  33. .replace(/>/g, "&gt;") +
  34. '</code>'
  35. }
  36. function test(name, spec) {
  37. testCM(name, function(cm) {
  38. cm.setValue(spec.value);
  39. cm.setCursor(spec.cursor);
  40. var completion = CodeMirror.hint.html(cm);
  41. if (!deepCompare(completion.list, spec.list))
  42. throw new Failure("Wrong completion results. Got" +
  43. escapeHtmlList(completion) +" but expected" +
  44. escapeHtmlList(spec));
  45. eqCharPos(completion.from, spec.from,'from-failed');
  46. eqCharPos(completion.to, spec.to, 'to-failed');
  47. }, {
  48. value: spec.value,
  49. mode: spec.mode || "text/html"
  50. });
  51. }
  52. testData.forEach(function (value) {
  53. // Use sane defaults
  54. var lines = value.value.split(/\n/);
  55. value.to = value.pos || Pos(lines.length-1, lines[lines.length-1].length);
  56. value.from = value.from || Pos(lines.length-1,0);
  57. value.cursor = value.cursor || value.to;
  58. var name = value.name ||value.value;
  59. test(name,value)
  60. });
  61. function deepCompare(a, b) {
  62. if (a === b) return true;
  63. if (!(a && typeof a === "object") ||
  64. !(b && typeof b === "object")) return false;
  65. var array = a instanceof Array
  66. if ((b instanceof Array) !== array) return false;
  67. if (array) {
  68. if (a.length !== b.length) return false;
  69. for (var i = 0; i < a.length; i++) if (!deepCompare(a[i], b[i])) return false
  70. } else {
  71. for (var p in a) if (!(p in b) || !deepCompare(a[p], b[p])) return false;
  72. for (var p in b) if (!(p in a)) return false
  73. }
  74. return true
  75. }
  76. })();