blob: 31f48ee74c7d05bc964c889ff621eaaddee67f92 [file] [log] [blame]
Akron954c6a52020-11-10 14:26:29 +01001"use strict";
2
Helge507dd232026-04-13 18:14:20 +02003define(['hint', 'hint/input', 'hint/contextanalyzer', 'hint/menu', 'hint/item', 'hint/foundries'], function (hintClass, inputClass, contextClass, menuClass, menuItemClass, foundriesClass) {
Nils Diewald19ccee92014-12-08 11:30:08 +00004
Leo Repp46903bf2021-12-18 16:05:53 +01005 function emitKeyboardEvent (element, type, letter, keyCode) {
Akronfac16472018-07-26 16:47:21 +02006 // event type : keydown, keyup, keypress
7 // http://stackoverflow.com/questions/596481/simulate-javascript-key-events
8 // http://stackoverflow.com/questions/961532/firing-a-keyboard-event-in-javascript
Leo Repp46903bf2021-12-18 16:05:53 +01009 /**
10 * Nils Version. Does not work due to bug noted below
11 var keyboardEvent = document.createEvent("KeyboardEvent",);
Akronfac16472018-07-26 16:47:21 +020012 var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ?
13 "initKeyboardEvent" : "initKeyEvent";
14 keyboardEvent[initMethod](
Leo Repp46903bf2021-12-18 16:05:53 +010015
16
17
Akronfac16472018-07-26 16:47:21 +020018 type,
19 true, // bubbles
20 true, // cancelable
21 window, // viewArg: should be window
22 false, // ctrlKeyArg
23 false, // altKeyArg
24 false, // shiftKeyArg
25 false, // metaKeyArg
26 keyCode, // keyCodeArg : unsigned long the virtual key code, else 0
Leo Repp46903bf2021-12-18 16:05:53 +010027 charCode || 0 // charCodeArgs : unsigned long the Unicode character
Akronfac16472018-07-26 16:47:21 +020028 // associated with the depressed key, else 0
Leo Repp46903bf2021-12-18 16:05:53 +010029
Akronfac16472018-07-26 16:47:21 +020030 );
31 element.dispatchEvent(keyboardEvent);
Leo Repp46903bf2021-12-18 16:05:53 +010032 */
33 //Leos Version
34 //https://stackoverflow.com/a/59113178
35
36 //might not work on Chromium
37 element.dispatchEvent(new KeyboardEvent(type, {
38 key: letter,
39 keyCode: keyCode,
40 code: "Key"+letter,
41 which: keyCode, //This is a hack
42 shiftKey: false,
43 ctrlKey: false,
44 metaKey: false,
45 bubbles: true,
46 view: window,
47 charCode: keyCode //This is a hack https://bugs.webkit.org/show_bug.cgi?id=16735
48 // charCodeArgs : unsigned long the Unicode character
49 // associated with the depressed key, else 0
50 }));
Akronfac16472018-07-26 16:47:21 +020051 };
Nils Diewald19ccee92014-12-08 11:30:08 +000052
Akronfac16472018-07-26 16:47:21 +020053 var afterAllFunc = function () {
54 try {
55 var mirrors = document.querySelectorAll(".hint.mirror");
56 for (var i in mirrors) {
57 mirrors[i].parentNode.removeChild(mirrors[i])
58 };
59 }
60 catch (e) {};
61
62 var body = document.body;
63 for (var i in body.children) {
64 if (body.children[i].nodeType && body.children[i].nodeType === 1) {
65 if (!body.children[i].classList.contains("jasmine_html-reporter")) {
66 body.removeChild(body.children[i]);
67 };
68 };
69 };
Helge507dd232026-04-13 18:14:20 +020070
Akronfac16472018-07-26 16:47:21 +020071 KorAP.API.getMatchInfo = undefined;
72 KorAP.context = undefined;
73 // KorAP.annotationHelper = undefined;
74 };
75
76 var beforeAllFunc = function () {
77 KorAP.annotationHelper = KorAP.annotationHelper || {};
78 KorAP.annotationHelper["-"] = [
79 ["Base Annotation", "base/s=", "Structure"],
80 ["CoreNLP", "corenlp/", "Constituency, Named Entities, Part-of-Speech"]
81 ];
82 KorAP.annotationHelper["corenlp/"] = [
83 ["Named Entity", "ne=" , "Combined"],
84 ["Named Entity", "ne_dewac_175m_600=" , "ne_dewac_175m_600"],
85 ["Named Entity", "ne_hgc_175m_600=", "ne_hgc_175m_600"]
86 ];
87 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000088
Nils Diewald7c8ced22015-04-15 19:21:00 +000089 describe('KorAP.InputField', function () {
Akronfac16472018-07-26 16:47:21 +020090 beforeAll(beforeAllFunc);
91 afterAll(afterAllFunc);
Akron5336fd42020-10-09 18:13:51 +020092 let input;
Nils Diewald1c546922015-04-13 01:56:19 +000093
Nils Diewald7c8ced22015-04-15 19:21:00 +000094 beforeEach(function () {
95 input = document.createElement("input");
96 input.setAttribute('type', "text");
97 input.setAttribute("value", "abcdefghijklmno");
98 input.style.position = 'absolute';
99 document.getElementsByTagName('body')[0].appendChild(input);
100 input.style.top = "20px";
101 input.style.left = "30px";
102 input.focus();
103 input.selectionStart = 5;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000104 });
105
Nils Diewald7c8ced22015-04-15 19:21:00 +0000106 afterEach(function () {
107 document.getElementsByTagName("body")[0].removeChild(
Akron95abaf42018-04-26 15:33:22 +0200108 input
Nils Diewald7c8ced22015-04-15 19:21:00 +0000109 );
110 });
Nils Diewald5c5a7472015-04-02 22:13:38 +0000111
Nils Diewald7c8ced22015-04-15 19:21:00 +0000112 it('should be initializable', function () {
113 // Supports: context, searchField
114 var inputField = inputClass.create(input);
Akron24aa0052020-11-10 11:00:34 +0100115 expect(inputField._el).not.toBe(undefined);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000116 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000117
Nils Diewald7c8ced22015-04-15 19:21:00 +0000118 it('should have text', function () {
119 expect(input.value).toEqual('abcdefghijklmno');
120 var inputField = inputClass.create(input);
Nils Diewald19ccee92014-12-08 11:30:08 +0000121
Nils Diewald7c8ced22015-04-15 19:21:00 +0000122 expect(inputField.value()).toEqual("abcdefghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +0000123
Nils Diewald7c8ced22015-04-15 19:21:00 +0000124 expect(input.selectionStart).toEqual(5);
125 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000126 expect(inputField._split()[0]).toEqual("abcde");
127 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +0000128
Nils Diewald7c8ced22015-04-15 19:21:00 +0000129 inputField.insert("xyz");
130 expect(inputField.value()).toEqual('abcdexyzfghijklmno');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000131 expect(inputField._split()[0]).toEqual("abcdexyz");
132 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000133 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000134
Nils Diewald7c8ced22015-04-15 19:21:00 +0000135 it('should be correctly positioned', function () {
136 expect(input.value).toEqual('abcdefghijklmno');
137 var inputField = inputClass.create(input);
138 document.getElementsByTagName("body")[0].appendChild(input);
139 inputField.reposition();
140 expect(input.style.left).toEqual("30px");
141 expect(inputField.mirror().style.left.match(/^(\d+)px$/)[1]).toBeGreaterThan(29);
142 expect(inputField.mirror().style.top.match(/^(\d+)px$/)[1]).toBeGreaterThan(20);
Akronb759ee92024-11-19 18:02:56 +0100143 expect(inputField.selectionRange()[0]).toEqual(5);
144 expect(inputField.selectionRange()[1]).toEqual(5);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000145 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000146
Nils Diewald7c8ced22015-04-15 19:21:00 +0000147 it('should have a correct context', function () {
148 expect(input.value).toEqual('abcdefghijklmno');
149 var inputField = inputClass.create(input);
150 expect(inputField.value()).toEqual("abcdefghijklmno");
151 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000152 expect(inputField._split()[0]).toEqual("abcde");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000153 expect(inputField.context()).toEqual("abcde");
154 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000155
Leo Repp46903bf2021-12-18 16:05:53 +0100156
Nils Diewald7c8ced22015-04-15 19:21:00 +0000157 it('should be correctly triggerable', function () {
158 // https://developer.mozilla.org/samples/domref/dispatchEvent.html
hebastae66b62b2022-01-19 18:03:56 +0100159 var hint = hintClass.create({ "inputField" : input });
Leo Repp46903bf2021-12-18 16:05:53 +0100160 emitKeyboardEvent(hint.inputField()._el, "keypress", "E", 69);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000161 });
Leo Repp46903bf2021-12-18 16:05:53 +0100162
Nils Diewald19ccee92014-12-08 11:30:08 +0000163 });
164
Nils Diewald1c546922015-04-13 01:56:19 +0000165
Nils Diewald7c8ced22015-04-15 19:21:00 +0000166 describe('KorAP.ContextAnalyzer', function () {
Akronfac16472018-07-26 16:47:21 +0200167 beforeAll(beforeAllFunc);
168 afterAll(afterAllFunc);
169
Nils Diewald7c8ced22015-04-15 19:21:00 +0000170 it('should be initializable', function () {
171 var analyzer = contextClass.create(")");
172 expect(analyzer).toBe(undefined);
173 analyzer = contextClass.create(".+?");
174 expect(analyzer).not.toBe(undefined);
175 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000176
Nils Diewald7c8ced22015-04-15 19:21:00 +0000177 it('should check correctly', function () {
Akronfac16472018-07-26 16:47:21 +0200178
179 // Intialize KorAP.context
180 hintClass.create();
181
Akron954c6a52020-11-10 14:26:29 +0100182 const analyzer = contextClass.create(KorAP.context);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000183 expect(analyzer.test("cnx/]cnx/c=")).toEqual("cnx/c=");
184 expect(analyzer.test("cnx/c=")).toEqual("cnx/c=");
185 expect(analyzer.test("cnx/c=np mate/m=mood:")).toEqual("mate/m=mood:");
186 expect(analyzer.test("impcnx/")).toEqual("impcnx/");
187 expect(analyzer.test("cnx/c=npcnx/")).toEqual("npcnx/");
188 expect(analyzer.test("mate/m=degree:pos corenlp/ne_dewac_175m_600="))
Akronfac16472018-07-26 16:47:21 +0200189 .toEqual("corenlp/ne_dewac_175m_600=");
Akron113cc1a2016-01-22 21:17:57 +0100190 expect(analyzer.test("corenlp/")).toEqual("corenlp/");
191 expect(analyzer.test("corenlp/c=")).toEqual("corenlp/c=");
192 expect(analyzer.test("corenlp/c=PP-")).toEqual("corenlp/c=PP-");
193 expect(analyzer.test("corenlp/c=XY-")).toEqual("corenlp/c=XY-");
Akroncff9bac2016-01-25 21:39:38 +0100194 expect(analyzer.test("sgbr/l=")).toEqual("sgbr/l=");
195 expect(analyzer.test("sgbr/lv=")).toEqual("sgbr/lv=");
196 expect(analyzer.test("sgbr/p=")).toEqual("sgbr/p=");
Akronee9ef4a2016-06-03 12:50:08 +0200197 expect(analyzer.test("")).toEqual(undefined);
198 expect(analyzer.test("abcdecnx/")).toEqual("abcdecnx/");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000199 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000200 });
201
Nils Diewald19ccee92014-12-08 11:30:08 +0000202
Nils Diewald7c8ced22015-04-15 19:21:00 +0000203 describe('KorAP.Hint', function () {
Akron954c6a52020-11-10 14:26:29 +0100204
205 let input;
206
Akronfac16472018-07-26 16:47:21 +0200207 beforeAll(beforeAllFunc);
208 afterAll(afterAllFunc);
Nils Diewald19ccee92014-12-08 11:30:08 +0000209
Nils Diewald7c8ced22015-04-15 19:21:00 +0000210 beforeEach(function () {
211 input = document.createElement("input");
212 input.setAttribute("type", "text");
213 input.setAttribute("value", "abcdefghijklmno");
214 input.style.position = 'absolute';
215 input.style.top = "20px";
216 input.style.left = "30px";
217 input.focus();
218 input.selectionStart = 5;
219 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000220
Nils Diewald7c8ced22015-04-15 19:21:00 +0000221 it('should be initializable', function () {
Akronfac16472018-07-26 16:47:21 +0200222
Nils Diewald7c8ced22015-04-15 19:21:00 +0000223 // Supports: context, searchField
224 var hint = hintClass.create({
Akronfac16472018-07-26 16:47:21 +0200225 inputField : input
Nils Diewald7c8ced22015-04-15 19:21:00 +0000226 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000227
Nils Diewald7c8ced22015-04-15 19:21:00 +0000228 expect(hint).toBeTruthy();
229 });
Akron00cd4d12016-05-31 21:01:11 +0200230
Helge507dd232026-04-13 18:14:20 +0200231
Akron00cd4d12016-05-31 21:01:11 +0200232 it('should alert at char pos', function () {
233 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200234 inputField : input
Akron00cd4d12016-05-31 21:01:11 +0200235 });
236
237 expect(hint.active()).toBeFalsy();
238
239 expect(hint.alert(4, 'That does not work!')).toBeTruthy();
240
241 expect(hint.active()).toBeTruthy();
242
243 var container = hint.inputField().container();
244 expect(container.firstChild.classList.contains('hint')).toBe(true);
245 expect(container.firstChild.classList.contains('alert')).toBe(true);
246 expect(container.firstChild.textContent).toEqual('That does not work!');
247 expect(hint.inputField().mirrorValue()).toEqual('abcd');
248
249 expect(hint.alert(4, 'That does not work!')).toBeFalsy();
250
251 // Update - meaning: hide alert
252 hint.update();
253
254 expect(hint.alert().active).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200255 expect(hint.active()).toBeFalsy();
Akron1a5a5872016-09-05 20:17:14 +0200256
257 // Show again
258 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
259 expect(hint.inputField().mirrorValue()).toEqual('abcde');
260 expect(hint.alert().active).toBeTruthy();
261 expect(hint.active()).toBeTruthy();
262
263 // Show menu, hide alert!
264 hint.show(false);
265 expect(hint.active()).toBeTruthy();
266 expect(hint.inputField().mirrorValue()).toEqual('abcde');
267 expect(hint.alert().active).toBeFalsy();
268
Akronfac16472018-07-26 16:47:21 +0200269 // Show again
Akron1a5a5872016-09-05 20:17:14 +0200270 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
271 expect(hint.inputField().mirrorValue()).toEqual('abcde');
272 expect(hint.alert().active).toBeTruthy();
273 expect(hint.active()).toBeTruthy();
274
Akronbe2f9a02025-01-14 09:36:55 +0100275 // Show again with the same message
276 expect(hint.alert().active).toBeTruthy();
277 hint.update();
278 expect(hint.alert().active).toBeFalsy();
279 hint.alert().show();
280 expect(hint.alert().active).toBeTruthy();
281 expect(hint.alert().element().textContent).toEqual("That does not work!");
282
Akron1a5a5872016-09-05 20:17:14 +0200283 // Show menu, hide alert!
284 hint.show(false);
Akronee9ef4a2016-06-03 12:50:08 +0200285 });
Akron00cd4d12016-05-31 21:01:11 +0200286
Akronda5bd3a2020-10-16 17:37:49 +0200287 it('should work both in Chrome and Firefox', function () {
288 var hint = hintClass.create({
289 inputField : input
290 });
291 hint.show(false);
292 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
293
294 // Show menu, hide alert!
295 hint.show(false);
296
297 expect(hint.active()).toBeFalsy();
298 });
299
300
Akronee9ef4a2016-06-03 12:50:08 +0200301 it('should view main menu on default', function () {
302 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200303 inputField : input
Akronee9ef4a2016-06-03 12:50:08 +0200304 });
305
306 expect(hint.active()).toBeFalsy();
307
308 hint.inputField().insert('der Baum corenlp/');
Akronee9ef4a2016-06-03 12:50:08 +0200309
Akron8eaeb2e2016-08-29 18:26:28 +0200310 var cont = hint.inputField().container();
Akron8eaeb2e2016-08-29 18:26:28 +0200311 expect(cont.getElementsByTagName('div').length).toBe(1);
312 expect(cont.getElementsByTagName('ul').length).toBe(0);
313 expect(cont.firstChild).toEqual(cont.firstChild);
314
315 // Show menu, if a relevant context exists
316 // There is a menu for corenlp/
Akron02360e42016-06-07 13:41:12 +0200317 hint.show(false);
318
Leo Repp57997402021-08-18 16:37:52 +0200319 expect(cont.getElementsByTagName('ul').length).toEqual(1+1); //+1 from containermenu (see container/container.js)
320 expect(cont.getElementsByTagName('li').length).toEqual(3);
Akron65c74352016-09-02 17:23:39 +0200321
Akron8eaeb2e2016-08-29 18:26:28 +0200322 // Hide the menu and focus on the input
323 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200324
Leo Repp57997402021-08-18 16:37:52 +0200325 expect(cont.getElementsByTagName('div').length).toEqual(1);
326 expect(cont.getElementsByTagName('li').length).toEqual(0);
Akronee9ef4a2016-06-03 12:50:08 +0200327
Akron02360e42016-06-07 13:41:12 +0200328 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200329
Akronee9ef4a2016-06-03 12:50:08 +0200330 hint.inputField().insert(' hhhh');
Akron65c74352016-09-02 17:23:39 +0200331
Leo Repp57997402021-08-18 16:37:52 +0200332 // show with context if possible
Akron02360e42016-06-07 13:41:12 +0200333 hint.show(false);
Akron65c74352016-09-02 17:23:39 +0200334
Leo Repp57997402021-08-18 16:37:52 +0200335 expect(cont.getElementsByTagName('div').length).toEqual(4);
336 expect(cont.getElementsByTagName('ul').length).toEqual(1+1);//+1 from containermenu (see container/container.js)
337 expect(cont.getElementsByTagName('li').length).toEqual(2);
Akron02360e42016-06-07 13:41:12 +0200338
Akronee9ef4a2016-06-03 12:50:08 +0200339 hint.unshow();
340 hint.inputField().insert(' aaaa/');
341
Leo Repp57997402021-08-18 16:37:52 +0200342 // show with context necessarily
Akronee9ef4a2016-06-03 12:50:08 +0200343 hint.show(true);
344
Leo Repp57997402021-08-18 16:37:52 +0200345 expect(cont.getElementsByTagName('div').length).toEqual(1);
346 expect(cont.getElementsByTagName('ul').length).toEqual(0); //here not +1: context doesnt fit
Akron1a5a5872016-09-05 20:17:14 +0200347 });
348
Akron95abaf42018-04-26 15:33:22 +0200349
350 it('should open menus depending on the context', function () {
351 var hint = hintClass.create({
352 inputField : input
353 });
354 hint.inputField().reset();
355
356 expect(hint.active()).toBeFalsy();
357
358 // show with context
359 hint.show(false);
360
361 expect(hint.active()).toBeTruthy();
Leo Repp57997402021-08-18 16:37:52 +0200362 var cont = hint.inputField().container();
363 expect(cont.getElementsByTagName('li')[0].firstChild.innerText).toEqual("Base Annotation");
Akron95abaf42018-04-26 15:33:22 +0200364
365 // Type in prefix
366 hint.active().prefix("cor").show();
367 expect(hint.active().prefix()).toEqual("cor");
368
369 // Click first step
Leo Repp57997402021-08-18 16:37:52 +0200370 expect(cont.getElementsByTagName('li')[0].firstChild.firstChild.innerText).toEqual("Cor");
371 cont.getElementsByTagName('li')[0].click();
Akron95abaf42018-04-26 15:33:22 +0200372
373 expect(hint.active()).toBeTruthy();
374
375 // Click second step
Leo Repp57997402021-08-18 16:37:52 +0200376 expect(cont.getElementsByTagName('li')[0].firstChild.innerText).toEqual("Named Entity");
377 cont.getElementsByTagName('li')[0].click()
Akron95abaf42018-04-26 15:33:22 +0200378
379 // Invisible menu
Leo Repp57997402021-08-18 16:37:52 +0200380 expect(cont.getElementsByTagName('li')[0]).toBeUndefined();
Akron95abaf42018-04-26 15:33:22 +0200381
382 // Inactive menu
383 expect(hint.active()).toBeFalsy();
384
385 // show with context
386 hint.show(false);
387
388 // No prefix
389 expect(hint.active().prefix()).toEqual("");
390 });
391
392
Akron1a5a5872016-09-05 20:17:14 +0200393 it('should not view main menu if context is mandatory', function () {
394 var hint = hintClass.create({
395 inputField : input
396 });
397
398 expect(hint.active()).toBeFalsy();
399
400 // Fine
401 hint.inputField().insert('der Baum corenlp/');
402 hint.show(true);
403 expect(hint.active()).toBeTruthy();
404
405 // Not analyzable
406 hint.inputField().insert('jhgjughjfhgnhfcvgnhj');
407 hint.show(true);
408 expect(hint.active()).toBeFalsy();
409
410 // Not available
411 hint.inputField().insert('jhgjughjfhgnhfcvgnhj/');
412 hint.show(true);
413 expect(hint.active()).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200414 });
Akron5746ecf2018-06-23 10:57:24 +0200415
416
417 it('should show the assistant bar on blur', function () {
418 var hint = hintClass.create({
419 inputField : input
420 });
421 // Fine
422 hint.inputField().insert('der Baum corenlp/');
423 hint.show(true);
424 expect(hint.active()).toBeTruthy();
425
426 // Blur
Akrone0789112018-08-31 14:32:04 +0200427 hint.active().hide();
Akron5746ecf2018-06-23 10:57:24 +0200428 expect(hint.active()).toBeFalsy();
429 });
Akron954c6a52020-11-10 14:26:29 +0100430
431 it('should support prefix', function () {
432 const hint = hintClass.create({
433 inputField : input
434 });
435 hint.inputField().reset();
436
437 expect(hint.active()).toBeFalsy();
438
439 // show with context
440 hint.show(false);
441
442 expect(hint.active()).toBeTruthy();
443
444 const menu = hint.active();
445
446 expect(menu.element().nodeName).toEqual('UL');
447
448 menu.limit(8);
449
450 // view
451 menu.show();
452
453 expect(menu.prefix()).toBe('');
454 expect(hint.active()).toBeTruthy();
455
456 // Type in prefix
457 hint.active().prefix("cor").show();
Leo Repp57997402021-08-18 16:37:52 +0200458 expect(hint.active()._prefix.value()).toBe("cor");
Akron954c6a52020-11-10 14:26:29 +0100459 expect(hint.active().prefix()).toEqual("cor");
Akron954c6a52020-11-10 14:26:29 +0100460 expect(input.value).toEqual("");
Leo Repp57997402021-08-18 16:37:52 +0200461 expect(hint.active()._prefix["isSelectable"]).not.toBeNull();
462 expect(hint._menuCollection['-']._prefix["isSelectable"]).not.toBeNull();
463 expect(hint.active()._prefix).toBe(hint._menuCollection['-']._prefix);
464 expect(hint.active()._prefix.element()).toBe(hint._menuCollection['-']._prefix.element());
Akron954c6a52020-11-10 14:26:29 +0100465 hint.active()._prefix.element().click();
Leo Repp57997402021-08-18 16:37:52 +0200466
467
Akron954c6a52020-11-10 14:26:29 +0100468 expect(input.value).toEqual("cor");
469 expect(hint.active()).toBeFalsy();
470
471 // view
472 menu.show();
473 expect(menu.prefix()).toBe('');
474
475 });
476
Leo Repp46903bf2021-12-18 16:05:53 +0100477
478 it('should highlight the prefix if no item matches.', function () {
479 const hint = hintClass.create({
480 inputField : input
481 });
482 hint.inputField().reset();
483
484 expect(hint.active()).toBeFalsy();
485
486 // show with context
487 hint.show(false);
488
489 expect(hint.active()).toBeTruthy();
490
491 const menu = hint.active();
492 expect(menu.limit(3).show(3)).toBe(true);
493 menu.element().focus();
494
495
496
497 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
498 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
499 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
500 expect(menu.container()._cItemPrefix.active()).toBeTruthy();
501 expect(menu.prefix()).toEqual("EEE");
502 expect(menu.element().classList.contains("visible")).toBeTruthy();
503 expect(menu.container().element().classList.contains("visible")).toBeTruthy();
504
505 emitKeyboardEvent(menu.element(),'keydown'," ",13);
506 //Should call reset() and hide()
507 // hint containermenu should do something different.
508 expect(menu.prefix()).toEqual("");
509 expect(menu.element().classList.contains("visible")).toBeFalsy();
510 expect(menu.container().element().classList.contains("visible")).toBeFalsy();
511 });
Leo Repp57997402021-08-18 16:37:52 +0200512
Helge507dd232026-04-13 18:14:20 +0200513 it('should filter available foundries based on configuration', function () {
514
515 KorAP.annotationHelper["-"] = [
516 ["Base Annotation", "base/s=", "Structure"],
517 ["CoreNLP", "corenlp/", "Constituency, Named Entities, Part-of-Speech"],
518 ["TreeTagger", "tt/", "Lemma, Part-of-Speech"]
519 ];
520 KorAP.annotationHelper.filterByConfig();
521 let foundries = KorAP.annotationHelper["-"].map(e => e[1]);
522 expect(foundries).toContain("base/s=");
523 expect(foundries).toContain("corenlp/");
524 expect(foundries).toContain("tt/");
Akron65c74352016-09-02 17:23:39 +0200525
Helge507dd232026-04-13 18:14:20 +0200526
527 //set configuration to corenlp and tt
528 document.body.setAttribute('data-hint-foundries', 'corenlp,tt');
529 KorAP.annotationHelper.filterByConfig();
530 foundries = KorAP.annotationHelper["-"].map(e => e[1]);
531 expect(foundries).not.toContain("base/s=");
532 expect(foundries).toContain("corenlp/");
533 expect(foundries).toContain("tt/");
534
535 //set configuration to corenlp only, with different case and whitespaces
536 document.body.setAttribute('data-hint-foundries', ' coREnlp');
537 KorAP.annotationHelper.filterByConfig();
538 foundries = KorAP.annotationHelper["-"].map(e => e[1]);
539 expect(foundries).not.toContain("base/s=");
540 expect(foundries).toContain("corenlp/");
541 expect(foundries).not.toContain("tt/");
542
543 //Clean up
544 document.body.removeAttribute('data-hint-foundries');
545 });
546
547 it('should apply configured foundry filter if initialized', function () {
548
549 KorAP.annotationHelper["-"] = [
550 ["Base Annotation", "base/s=", "Structure"],
551 ["CoreNLP", "corenlp/", "Constituency, Named Entities, Part-of-Speech"],
552 ["TreeTagger", "tt/", "Lemma, Part-of-Speech"]
553 ];
554
555 document.body.setAttribute('data-hint-foundries', 'tt');
556
557 var hint = hintClass.create({
558 inputField : input
559 });
560 expect(hint).toBeTruthy();
561
562 let foundries = KorAP.annotationHelper["-"].map(e => e[1]);
563 expect(foundries).not.toContain("base/s=");
564 expect(foundries).not.toContain("corenlp/");
565 expect(foundries).toContain("tt/");
566
567 //Clean up
568 document.body.removeAttribute('data-hint-foundries');
569 });
570
Akron02360e42016-06-07 13:41:12 +0200571 xit('should remove all menus on escape');
Nils Diewald19ccee92014-12-08 11:30:08 +0000572 });
573
Akron65c74352016-09-02 17:23:39 +0200574
Nils Diewald7c8ced22015-04-15 19:21:00 +0000575 describe('KorAP.HintMenuItem', function () {
Akronfac16472018-07-26 16:47:21 +0200576 beforeAll(beforeAllFunc);
577 afterAll(afterAllFunc);
578
Nils Diewald7c8ced22015-04-15 19:21:00 +0000579 it('should be initializable', function () {
580 expect(
Akronfac16472018-07-26 16:47:21 +0200581 function() { menuItemClass.create([]) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000582 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000583
Nils Diewald7c8ced22015-04-15 19:21:00 +0000584 expect(
Akronfac16472018-07-26 16:47:21 +0200585 function() { menuItemClass.create(['CoreNLP']) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000586 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000587
Nils Diewald7c8ced22015-04-15 19:21:00 +0000588 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
589 expect(menuItem.name()).toEqual('CoreNLP');
590 expect(menuItem.action()).toEqual('corenlp/');
591 expect(menuItem.desc()).toBeUndefined();
Nils Diewald19ccee92014-12-08 11:30:08 +0000592
Nils Diewald7c8ced22015-04-15 19:21:00 +0000593 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200594 ['CoreNLP', 'corenlp/', 'It\'s funny']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000595 );
596 expect(menuItem.name()).toEqual('CoreNLP');
597 expect(menuItem.action()).toEqual('corenlp/');
598 expect(menuItem.desc()).not.toBeUndefined();
599 expect(menuItem.desc()).toEqual('It\'s funny');
600 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000601
Akronfac16472018-07-26 16:47:21 +0200602
Nils Diewald7c8ced22015-04-15 19:21:00 +0000603 it('should have an element', function () {
604 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
605 expect(menuItem.element()).not.toBe(undefined);
606 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000607
Nils Diewald7c8ced22015-04-15 19:21:00 +0000608 var title = menuItem.element().firstChild;
609 expect(title.nodeName).toEqual("SPAN");
610 expect(title.firstChild.nodeType).toEqual(3);
611 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
612 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
613 expect(menuItem.element().childNodes[1]).toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000614
Nils Diewald7c8ced22015-04-15 19:21:00 +0000615 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200616 ['CoreNLP', 'corenlp/', 'my DescRiption']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000617 );
618 expect(menuItem.element()).not.toBe(undefined);
619 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000620
Nils Diewald7c8ced22015-04-15 19:21:00 +0000621 title = menuItem.element().firstChild;
622 expect(title.nodeName).toEqual("SPAN");
623 expect(title.firstChild.nodeType).toEqual(3); // TextNode
624 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
Nils Diewald19ccee92014-12-08 11:30:08 +0000625
Nils Diewald7c8ced22015-04-15 19:21:00 +0000626 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
627 expect(menuItem.element().childNodes[1]).not.toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000628
Nils Diewald7c8ced22015-04-15 19:21:00 +0000629 var desc = menuItem.element().lastChild;
630 expect(desc.nodeName).toEqual("SPAN");
631 expect(desc.firstChild.nodeType).toEqual(3); // TextNode
632 expect(desc.firstChild.nodeValue).toEqual("my DescRiption");
633 });
Nils Diewald1c546922015-04-13 01:56:19 +0000634
Nils Diewald19ccee92014-12-08 11:30:08 +0000635
Nils Diewald7c8ced22015-04-15 19:21:00 +0000636 it('should be activatable and deactivateable by class', function () {
637 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
638 expect(menuItem.active()).toBe(false);
639 expect(menuItem.element().getAttribute("class")).toBe(null);
640 menuItem.active(true);
641 expect(menuItem.active()).toBe(true);
642 expect(menuItem.element().getAttribute("class")).toEqual("active");
643 menuItem.active(false); // Is active
644 expect(menuItem.active()).toBe(false);
645 expect(menuItem.element().getAttribute("class")).toEqual("");
646 menuItem.active(true);
647 expect(menuItem.active()).toBe(true);
648 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000649
Nils Diewald7c8ced22015-04-15 19:21:00 +0000650 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
651 expect(menuItem.active()).toBe(false);
652 expect(menuItem.element().getAttribute("class")).toBe(null);
653 menuItem.active(false); // Is not active
654 expect(menuItem.active()).toBe(false);
655 expect(menuItem.element().getAttribute("class")).toBe(null);
656 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000657
Akronfac16472018-07-26 16:47:21 +0200658
Nils Diewald7c8ced22015-04-15 19:21:00 +0000659 it('should be set to boundary', function () {
660 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
661 expect(menuItem.active()).toBe(false);
662 expect(menuItem.element().getAttribute("class")).toBe(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000663
Nils Diewald7c8ced22015-04-15 19:21:00 +0000664 // Set active
665 menuItem.active(true);
666 expect(menuItem.active()).toBe(true);
667 expect(menuItem.noMore()).toBe(false);
668 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000669
Nils Diewald7c8ced22015-04-15 19:21:00 +0000670 // Set no more
671 menuItem.noMore(true);
672 expect(menuItem.active()).toBe(true);
673 expect(menuItem.noMore()).toBe(true);
674 expect(menuItem.element().getAttribute("class")).toEqual("active no-more");
Nils Diewald1c546922015-04-13 01:56:19 +0000675
Nils Diewald7c8ced22015-04-15 19:21:00 +0000676 // No no more
677 menuItem.noMore(false);
678 expect(menuItem.active()).toBe(true);
679 expect(menuItem.noMore()).toBe(false);
680 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald1c546922015-04-13 01:56:19 +0000681
Nils Diewald7c8ced22015-04-15 19:21:00 +0000682 // Set no more, deactivate
683 menuItem.noMore(true);
684 menuItem.active(false);
685 expect(menuItem.active()).toBe(false);
686 expect(menuItem.noMore()).toBe(true);
687 expect(menuItem.element().getAttribute("class")).toEqual("no-more");
Nils Diewald19ccee92014-12-08 11:30:08 +0000688
Nils Diewald7c8ced22015-04-15 19:21:00 +0000689 // Set active
690 menuItem.active(true);
691 expect(menuItem.active()).toBe(true);
692 expect(menuItem.noMore()).toBe(true);
693 expect(menuItem.element().getAttribute("class")).toEqual("no-more active");
694 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000695
Akronfac16472018-07-26 16:47:21 +0200696
Nils Diewald7c8ced22015-04-15 19:21:00 +0000697 it('should be highlightable', function () {
698 // Highlight in the middle
699 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
700 menuItem.highlight("ren");
701 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000702
Nils Diewald7c8ced22015-04-15 19:21:00 +0000703 menuItem.lowlight();
704 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000705
Nils Diewald7c8ced22015-04-15 19:21:00 +0000706 // Starting highlight
707 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
708 menuItem.highlight("cor");
709 expect(menuItem.element().innerHTML).toEqual("<span><mark>Cor</mark>eNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000710
Nils Diewald7c8ced22015-04-15 19:21:00 +0000711 menuItem.lowlight();
712 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
713
714 // Starting highlight - short
715 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
716 menuItem.highlight("c");
717 expect(menuItem.element().innerHTML).toEqual("<span><mark>C</mark>oreNLP</span>");
718
719 menuItem.lowlight();
720 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
721
722 // Highlight at the end
723 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
724 menuItem.highlight("nlp");
725 expect(menuItem.element().innerHTML).toEqual("<span>Core<mark>NLP</mark></span>");
726
727 menuItem.lowlight();
728 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
729
730 // Highlight at the end - short
731 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
732 menuItem.highlight("p");
733 expect(menuItem.element().innerHTML).toEqual("<span>CoreNL<mark>P</mark></span>");
734
735 menuItem.lowlight();
736 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
737
738 // No highlight
739 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
740 menuItem.highlight("xp");
741 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
742
743 menuItem.lowlight();
744 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
745
746 // Highlight in the middle - first
747 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
748
749 menuItem.highlight("ren");
750 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span><span class=\"desc\">This is my Example</span>");
751
752 menuItem.lowlight();
753 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
754
755 // Highlight in the middle - second
756 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
757 menuItem.highlight("ampl");
758 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Ex<mark>ampl</mark>e</span>');
759
760 menuItem.lowlight();
761 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
762
763 // Highlight in the middle - both
764 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
765
766 menuItem.highlight("e");
767 expect(menuItem.element().innerHTML).toEqual('<span>Cor<mark>e</mark>NLP</span><span class="desc">This is my <mark>E</mark>xampl<mark>e</mark></span>');
768
769 menuItem.lowlight();
770 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
771
772 // Highlight in the end - second
773 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
774 menuItem.highlight("le");
775 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Examp<mark>le</mark></span>");
776
777 menuItem.lowlight();
778 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
779
780 // Highlight at the beginning - second
781 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
782 menuItem.highlight("this");
783 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\"><mark>This</mark> is my Example</span>");
784
785 menuItem.lowlight();
786 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
787 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000788 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000789
Akronfac16472018-07-26 16:47:21 +0200790
Nils Diewald7c8ced22015-04-15 19:21:00 +0000791 describe('KorAP.HintMenu', function () {
Akronfac16472018-07-26 16:47:21 +0200792 beforeAll(beforeAllFunc);
793 afterAll(afterAllFunc);
794
Nils Diewald7c8ced22015-04-15 19:21:00 +0000795 var list = [
796 ["Constituency", "c=", "Example 1"],
797 ["Lemma", "l="],
798 ["Morphology", "m=", "Example 2"],
799 ["Part-of-Speech", "p="],
800 ["Syntax", "syn="]
801 ];
Nils Diewald19ccee92014-12-08 11:30:08 +0000802
Nils Diewald7c8ced22015-04-15 19:21:00 +0000803 it('should be initializable', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000804 var menu = menuClass.create(null, "cnx/", list);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000805 expect(menu.element().nodeName).toEqual('UL');
Akron65c74352016-09-02 17:23:39 +0200806 // expect(menu.element().style.opacity).toEqual("0");
Nils Diewald19ccee92014-12-08 11:30:08 +0000807
Nils Diewald7c8ced22015-04-15 19:21:00 +0000808 menu.limit(8);
Nils Diewald19ccee92014-12-08 11:30:08 +0000809
Nils Diewald7c8ced22015-04-15 19:21:00 +0000810 // view
811 menu.show();
Akron6ed13992016-05-23 18:06:05 +0200812 expect(menu.prefix()).toBe('');
Nils Diewald19ccee92014-12-08 11:30:08 +0000813
Nils Diewald7c8ced22015-04-15 19:21:00 +0000814 // First element in list
815 expect(menu.item(0).active()).toBe(true);
816 expect(menu.item(0).noMore()).toBe(true);
817
818 // Middle element in list
819 expect(menu.item(2).active()).toBe(false);
820 expect(menu.item(2).noMore()).toBe(false);
Nils Diewald19ccee92014-12-08 11:30:08 +0000821
Nils Diewald7c8ced22015-04-15 19:21:00 +0000822 // Last element in list
823 expect(menu.item(menu.length() - 1).active()).toBe(false);
824 expect(menu.item(menu.length() - 1).noMore()).toBe(true);
Akron6ed13992016-05-23 18:06:05 +0200825
826 expect(menu.shownItem(0).active()).toBeTruthy();
827 expect(menu.shownItem(0).lcField()).toEqual(' constituency example 1');
828 expect(menu.shownItem(1).lcField()).toEqual(' lemma');
829 expect(menu.shownItem(2).lcField()).toEqual(' morphology example 2');
830
831 menu.next();
832 expect(menu.shownItem(1).active()).toBeTruthy();
833
834 menu.next();
835 expect(menu.shownItem(2).active()).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000836 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000837 });
838});