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