comparison .cms/lib/codemirror/test/html-hint-test.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/5/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 function escapeHtmlList(o) {
34 return '<code>' +
35 JSON.stringify(o.list,null,2)
36 .replace(/</g, "&lt;")
37 .replace(/>/g, "&gt;") +
38 '</code>'
39 }
40
41 function test(name, spec) {
42 testCM(name, function(cm) {
43 cm.setValue(spec.value);
44 cm.setCursor(spec.cursor);
45 var completion = CodeMirror.hint.html(cm);
46 if (!deepCompare(completion.list, spec.list))
47 throw new Failure("Wrong completion results. Got" +
48 escapeHtmlList(completion) +" but expected" +
49 escapeHtmlList(spec));
50 eqCharPos(completion.from, spec.from,'from-failed');
51 eqCharPos(completion.to, spec.to, 'to-failed');
52 }, {
53 value: spec.value,
54 mode: spec.mode || "text/html"
55 });
56 }
57
58 testData.forEach(function (value) {
59 // Use sane defaults
60 var lines = value.value.split(/\n/);
61 value.to = value.pos || Pos(lines.length-1, lines[lines.length-1].length);
62 value.from = value.from || Pos(lines.length-1,0);
63 value.cursor = value.cursor || value.to;
64 var name = value.name ||value.value;
65 test(name,value)
66 });
67
68 function deepCompare(a, b) {
69 if (a === b) return true;
70 if (!(a && typeof a === "object") ||
71 !(b && typeof b === "object")) return false;
72 var array = a instanceof Array
73 if ((b instanceof Array) !== array) return false;
74 if (array) {
75 if (a.length !== b.length) return false;
76 for (var i = 0; i < a.length; i++) if (!deepCompare(a[i], b[i])) return false
77 } else {
78 for (var p in a) if (!(p in b) || !deepCompare(a[p], b[p])) return false;
79 for (var p in b) if (!(p in a)) return false
80 }
81 return true
82 }
83 })();