This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathHTMLInstrumentation-test.js
More file actions
2705 lines (2426 loc) · 128 KB
/
HTMLInstrumentation-test.js
File metadata and controls
2705 lines (2426 loc) · 128 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint evil: true, regexp: true */
/*global describe, beforeEach, afterEach, it, runs, expect, spyOn, jasmine, Node */
/*unittests: HTML Instrumentation*/
define(function (require, exports, module) {
"use strict";
// Load dependent modules
var HTMLInstrumentation = require("language/HTMLInstrumentation"),
HTMLSimpleDOM = require("language/HTMLSimpleDOM"),
RemoteFunctions = require("text!LiveDevelopment/Agents/RemoteFunctions.js"),
SpecRunnerUtils = require("spec/SpecRunnerUtils"),
WellFormedDoc = require("text!spec/HTMLInstrumentation-test-files/wellformed.html"),
NotWellFormedDoc = require("text!spec/HTMLInstrumentation-test-files/omitEndTags.html"),
InvalidHTMLDoc = require("text!spec/HTMLInstrumentation-test-files/invalidHTML.html");
RemoteFunctions = eval("(" + RemoteFunctions.trim() + ")()");
var editor,
instrumentedHTML,
elementCount,
elementIds = {};
function createBlankDOM() {
// This creates a DOM for a blank document that we can clone when we want to simulate
// starting from an empty document (which, in the browser, includes implied html/head/body
// tags). We have to also strip the tagIDs from this DOM since they won't appear in the
// browser in this case.
var dom = HTMLSimpleDOM.build("<html><head></head><body></body></html>", true);
Object.keys(dom.nodeMap).forEach(function (key) {
var node = dom.nodeMap[key];
delete node.tagID;
});
dom.nodeMap = {};
return dom;
}
function removeDescendentsFromNodeMap(nodeMap, node) {
delete nodeMap[node.tagID];
if (node.children) {
node.children.forEach(function (child) {
removeDescendentsFromNodeMap(nodeMap, child);
});
}
}
var entityParsingNode = window.document.createElement("div");
/**
* domFeatures is a prototype object that augments a SimpleDOM object to have more of the
* features of a real DOM object. It specifically adds the features required for
* the RemoteFunctions code that applies patches and is not a general DOM implementation.
*
* Standard DOM methods below are not documented, but the ones unique to this test harness
* are.
*/
var domFeatures = Object.create(new HTMLSimpleDOM.SimpleNode(), {
firstChild: {
get: function () {
return this.children[0];
}
},
lastChild: {
get: function () {
return this.children[this.children.length - 1];
}
},
siblings: {
get: function () {
return this.parent.children;
}
},
nextSibling: {
get: function () {
var siblings = this.siblings;
var index = siblings.indexOf(this);
return siblings[index + 1];
}
},
previousSibling: {
get: function () {
var siblings = this.siblings;
var index = siblings.indexOf(this);
return siblings[index - 1];
}
},
nodeType: {
get: function () {
if (this.children) {
return Node.ELEMENT_NODE;
} else if (this.content) {
return Node.TEXT_NODE;
}
}
},
childNodes: {
get: function () {
var children = this.children;
if (!children.item) {
children.item = function (index) {
return children[index];
};
}
return children;
}
},
// At this time, innerHTML and textContent are used for entity parsing
// only. If that changes, we'll have bigger issues to deal with.
innerHTML: {
set: function (text) {
entityParsingNode.innerHTML = text;
},
get: function () {
return entityParsingNode.innerHTML;
}
},
textContent: {
set: function (text) {
entityParsingNode.textContent = text;
},
get: function () {
return entityParsingNode.textContent;
}
}
});
$.extend(domFeatures, {
insertBefore: function (newElement, referenceElement) {
if (newElement.parent && newElement.parent !== this) {
newElement.remove();
}
var index = this.children.indexOf(referenceElement);
if (index === -1) {
console.error("Unexpected attempt to reference a non-existent element:", referenceElement);
console.log(this.children);
}
this.children.splice(index, 0, newElement);
newElement.parent = this;
newElement.addToNodeMap();
},
appendChild: function (newElement) {
if (newElement.parent && newElement.parent !== this) {
newElement.remove();
}
this.children.push(newElement);
newElement.parent = this;
newElement.addToNodeMap();
},
/**
* The nodeMap keeps track of the Brackets-assigned tag ID to node object mapping.
* This method adds this element to the nodeMap if it has a data-brackets-id
* attribute set (something that the client-side applyEdits code will do).
*/
addToNodeMap: function () {
if (this.attributes && this.attributes["data-brackets-id"]) {
var nodeMap = this.getNodeMap();
if (nodeMap) {
nodeMap[this.attributes["data-brackets-id"]] = this;
} else {
console.error("Unable to get nodeMap from", this);
}
}
},
remove: function () {
if (this.tagID) {
var nodeMap = this.getNodeMap();
if (nodeMap) {
removeDescendentsFromNodeMap(nodeMap, this);
}
}
var siblings = this.siblings;
var index = siblings.indexOf(this);
if (index > -1) {
siblings.splice(index, 1);
this.parent = null;
} else {
console.error("Unexpected attempt to remove (not in siblings)", this);
}
},
/**
* Search node by node up the tree until a nodeMap is found. Returns undefined
* if no nodeMap is found.
*/
getNodeMap: function () {
var elem = this,
nodeMap;
while (elem) {
nodeMap = elem.nodeMap;
if (nodeMap) {
break;
}
elem = elem.parent;
}
return nodeMap;
},
setAttribute: function (key, value) {
if (key === "data-brackets-id") {
this.tagID = value;
var nodeMap = this.getNodeMap();
if (nodeMap) {
nodeMap[key] = this;
} else {
console.error("no nodemap found for ", this);
}
}
this.attributes[key] = value;
},
removeAttribute: function (key) {
delete this.attributes[key];
},
returnFailure: function (other) {
console.log("TEST FAILURE AT TAG ID ", this.tagID, this, other);
console.log("Patched: ", HTMLSimpleDOM._dumpDOM(this.parent || this));
console.log("DOM generated from revised text: ", HTMLSimpleDOM._dumpDOM(other.parent || other));
return false;
},
/**
* Compares two SimpleDOMs with the expectation that they are exactly the same.
*/
compare: function (other) {
if (this.children) {
if (this.tag !== other.tag) {
expect("Tag " + this.tag + " for tagID " + this.tagID).toEqual(other.tag);
return this.returnFailure(other);
}
if (this.tagID !== other.tagID) {
expect("tagID " + this.tagID).toEqual(other.tagID);
return this.returnFailure(other);
}
delete this.attributes["data-brackets-id"];
expect(this.attributes).toEqual(other.attributes);
// Skip implied tags in this (fake browser) DOM. (The editor's DOM
// should never have implied tags.)
var myChildren = [];
this.children.forEach(function (child) {
var isImplied = (child.tag === "html" || child.tag === "head" || child.tag === "body") && child.tagID === undefined;
if (!isImplied) {
myChildren.push(child);
}
});
if (myChildren.length !== other.children.length) {
expect("tagID " + this.tagID + " has " + myChildren.length + " unimplied children").toEqual(other.children.length);
return this.returnFailure(other);
}
var i;
for (i = 0; i < myChildren.length; i++) {
if (!myChildren[i].compare(other.children[i])) {
return false;
}
}
} else {
if (this.content !== other.content) {
expect(this.content).toEqual(other.content);
return this.returnFailure(other);
}
}
return true;
}
});
/**
* Creates a deep clone of a SimpleDOM tree, adding the domFeatures as it goes
* along.
*
* @param {Object} root root node of the SimpleDOM to clone
* @return {Object} cloned SimpleDOM with domFeatures applied
*/
function cloneDOM(root) {
var nodeMap = {};
// If there's no DOM to clone, then we must be starting from an empty document,
// so start with a document that already has implied <html>/<head>/<body>, since
// that's what the browser does.
if (!root) {
root = createBlankDOM();
}
function doClone(parent, node) {
var newNode = Object.create(domFeatures);
newNode.parent = parent;
if (node.tagID) {
nodeMap[node.tagID] = newNode;
newNode.tagID = node.tagID;
}
newNode.content = node.content;
if (node.children) {
newNode.tag = node.tag;
newNode.attributes = $.extend({}, node.attributes);
newNode.children = node.children.map(function (child) {
return doClone(newNode, child);
});
} else {
newNode.content = node.content;
}
return newNode;
}
var newRoot = doClone(null, root);
newRoot.nodeMap = nodeMap;
return newRoot;
}
/**
* The RemoteFunctions code that applies edits to the DOM expects only a few things to
* be present on the document object. This FakeDocument bridges the gap between a
* SimpleDOM and real DOM for the purposes of applying edits.
*
* @param {Object} dom The DOM we're wrapping with this document.
*/
var FakeDocument = function (dom) {
var self = this;
this.dom = dom;
this.nodeMap = dom.nodeMap;
// Walk the DOM looking for html/head/body tags. We can't use the nodeMap for this
// because it might be nulled out in the cases where we're simulating the browser
// creating implicit html/head/body tags.
function walk(node) {
if (node.tag === "html") {
self.documentElement = node;
} else if (node.tag === "head" || node.tag === "body") {
self[node.tag] = node;
}
if (node.children) {
node.children.forEach(walk);
}
}
walk(dom);
};
// The DOM edit code only performs this kind of query
var bracketsIdQuery = /\[data-brackets-id='(\d+)'\]/;
FakeDocument.prototype = {
createTextNode: function (content) {
var text = Object.create(domFeatures);
text.content = content;
return text;
},
createElement: function (tag) {
var el = Object.create(domFeatures);
el.tag = tag;
el.attributes = {};
el.children = [];
el.nodeMap = this.nodeMap;
return el;
},
querySelectorAll: function (query) {
var match = bracketsIdQuery.exec(query);
expect(match).toBeTruthy();
if (!match) {
return [];
}
var id = match[1];
function walk(node) {
if (String(node.tagID) === id) {
return node;
}
if (node.children) {
var i, result;
for (i = 0; i < node.children.length; i++) {
result = walk(node.children[i]);
if (result) {
return result;
}
}
}
}
var element = walk(this.dom);
if (element) {
return [element];
}
}
};
describe("HTML Instrumentation", function () {
function getIdToTagMap(instrumentedHTML, map) {
var count = 0;
var elementIdRegEx = /<(\w+?)\s+(?:[^<]*?\s)*?data-brackets-id='(\S+?)'/gi,
match,
tagID,
tagName;
do {
match = elementIdRegEx.exec(instrumentedHTML);
if (match) {
tagID = match[2];
tagName = match[1];
// Verify that the newly found ID is unique.
expect(map[tagID]).toBeUndefined();
map[tagID] = tagName.toLowerCase();
count++;
}
} while (match);
return count;
}
function checkTagIdAtPos(pos, expectedTag) {
var tagID = HTMLInstrumentation._getTagIDAtDocumentPos(editor, pos);
if (!expectedTag) {
expect(tagID).toBe(-1);
} else {
expect(elementIds[tagID]).toBe(expectedTag);
}
}
function verifyMarksCreated() {
var cm = editor._codeMirror,
marks = cm.getAllMarks();
expect(marks.length).toBeGreaterThan(0);
}
describe("interaction with document and editor", function () {
beforeEach(function () {
HTMLInstrumentation._resetCache();
runs(function () {
editor = SpecRunnerUtils.createMockEditor(WellFormedDoc, "html").editor;
expect(editor).toBeTruthy();
});
});
it("should properly regenerate marks when instrumented HTML is re-requested after document is edited", function () {
runs(function () {
var instrumented = HTMLInstrumentation.generateInstrumentedHTML(editor);
getIdToTagMap(instrumented, elementIds);
checkTagIdAtPos({line: 12, ch: 1}, "h1");
editor.document.replaceRange("123456789012345678901234567890", {line: 12, ch: 0});
instrumented = HTMLInstrumentation.generateInstrumentedHTML(editor);
elementIds = {};
getIdToTagMap(instrumented, elementIds);
checkTagIdAtPos({line: 12, ch: 1}, "body");
checkTagIdAtPos({line: 12, ch: 31}, "h1");
var lines = instrumented.split("\n");
expect(lines[12]).toMatch(/^123456789012345678901234567890<h1 data-brackets-id='[0-9]+'>GETTING STARTED WITH BRACKETS<\/h1>$/);
});
});
});
describe("HTML Instrumentation in wellformed HTML", function () {
beforeEach(function () {
runs(function () {
editor = SpecRunnerUtils.createMockEditor(WellFormedDoc, "html").editor;
expect(editor).toBeTruthy();
spyOn(editor.document, "getText").andCallThrough();
instrumentedHTML = HTMLInstrumentation.generateInstrumentedHTML(editor);
elementCount = getIdToTagMap(instrumentedHTML, elementIds);
if (elementCount) {
HTMLInstrumentation._markText(editor);
verifyMarksCreated();
}
});
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(editor.document);
editor = null;
instrumentedHTML = "";
elementCount = 0;
elementIds = {};
});
it("should instrument all start tags except some empty tags", function () {
runs(function () {
expect(elementCount).toEqual(15);
});
});
it("should have created cache and never call document.getText() again", function () {
runs(function () {
// scanDocument call here is to test the cache.
// HTMLInstrumentation.generateInstrumentedHTML call in "beforeEach"
// in turn calls scanDocument. Each function calls document.getText once
// and hence we've already had 2 calls from "beforeEach", but the following
// call should not call it again.
HTMLInstrumentation.scanDocument(editor.document);
expect(editor.document.getText.callCount).toBe(2);
});
});
it("should have recreated cache when document timestamp is different", function () {
runs(function () {
// update document timestamp with current time.
editor.document.diskTimestamp = new Date();
// This is an intentional repeat call to recreate the cache.
HTMLInstrumentation.scanDocument(editor.document);
// 2 calls from generateInstrumentedHTML call and one call
// from above scanDocument call. so total is 3.
expect(editor.document.getText.callCount).toBe(3);
});
});
it("should get 'img' tag for cursor positions inside img tag.", function () {
runs(function () {
checkTagIdAtPos({ line: 37, ch: 4 }, "img"); // before <img
checkTagIdAtPos({ line: 37, ch: 95 }, "img"); // after />
checkTagIdAtPos({ line: 37, ch: 65 }, "img"); // inside src attribute value
});
});
it("should get the parent 'a' tag for cursor positions between 'img' and its parent 'a' tag.", function () {
runs(function () {
checkTagIdAtPos({ line: 37, ch: 1 }, "a"); // before " <img"
checkTagIdAtPos({ line: 38, ch: 0 }, "a"); // before </a>
});
});
it("No tag at cursor positions outside of the 'html' tag", function () {
runs(function () {
checkTagIdAtPos({ line: 0, ch: 4 }, ""); // inside 'doctype' tag
checkTagIdAtPos({ line: 41, ch: 0 }, ""); // after </html>
});
});
it("Should get parent tag (body) for all cursor positions inside an html comment", function () {
runs(function () {
checkTagIdAtPos({ line: 15, ch: 1 }, "body"); // cursor between < and ! in the comment start
checkTagIdAtPos({ line: 16, ch: 15 }, "body");
checkTagIdAtPos({ line: 17, ch: 3 }, "body"); // cursor after -->
});
});
it("should get 'meta/link' tag for cursor positions in meta/link tags, not 'head' tag", function () {
runs(function () {
checkTagIdAtPos({ line: 5, ch: 64 }, "meta");
checkTagIdAtPos({ line: 8, ch: 12 }, "link");
});
});
it("Should get 'title' tag at cursor positions (either in the content or begin/end tag)", function () {
runs(function () {
checkTagIdAtPos({ line: 6, ch: 11 }, "title"); // inside the begin tag
checkTagIdAtPos({ line: 6, ch: 30 }, "title"); // in the content
checkTagIdAtPos({ line: 6, ch: 50 }, "title"); // inside the end tag
});
});
it("Should get 'h2' tag at cursor positions (either in the content or begin or end tag)", function () {
runs(function () {
checkTagIdAtPos({ line: 13, ch: 1 }, "h2"); // inside the begin tag
checkTagIdAtPos({ line: 13, ch: 20 }, "h2"); // in the content
checkTagIdAtPos({ line: 13, ch: 27 }, "h2"); // inside the end tag
});
});
});
describe("HTML Instrumentation in valid but not wellformed HTML", function () {
beforeEach(function () {
runs(function () {
editor = SpecRunnerUtils.createMockEditor(NotWellFormedDoc, "html").editor;
expect(editor).toBeTruthy();
instrumentedHTML = HTMLInstrumentation.generateInstrumentedHTML(editor);
elementCount = getIdToTagMap(instrumentedHTML, elementIds);
if (elementCount) {
HTMLInstrumentation._markText(editor);
verifyMarksCreated();
}
});
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(editor.document);
editor = null;
instrumentedHTML = "";
elementCount = 0;
elementIds = {};
});
it("should instrument all start tags except some empty tags", function () {
runs(function () {
expect(elementCount).toEqual(43);
});
});
it("should get 'p' tag for cursor positions before the succeding start tag of an unclosed 'p' tag", function () {
runs(function () {
checkTagIdAtPos({ line: 8, ch: 36 }, "p"); // at the end of the line that has p start tag
checkTagIdAtPos({ line: 8, ch: 2 }, "p"); // at the beginning of the <p>
checkTagIdAtPos({ line: 8, ch: 4 }, "p"); // inside <p> tag
checkTagIdAtPos({ line: 8, ch: 5 }, "p"); // after <p> tag
checkTagIdAtPos({ line: 9, ch: 0 }, "p"); // before <h1> tag, but considered to be the end of 'p' tag
});
});
it("should get 'h1' tag for cursor positions inside 'h1' that is following an unclosed 'p' tag.", function () {
runs(function () {
checkTagIdAtPos({ line: 9, ch: 20 }, "h1"); // inside text content of h1 tag
checkTagIdAtPos({ line: 9, ch: 52 }, "h1"); // inside </h1>
});
});
it("should get 'wbr' tag for cursor positions inside <wbr>, not its parent 'h1' tag.", function () {
runs(function () {
checkTagIdAtPos({ line: 9, ch: 10 }, "wbr"); // inside <wbr> that is in h1 content
});
});
it("should get 'li' tag for cursor positions inside the content of an unclosed 'li' tag", function () {
runs(function () {
checkTagIdAtPos({ line: 12, ch: 12 }, "li"); // inside first list item
checkTagIdAtPos({ line: 14, ch: 12 }, "li"); // inside third list item
checkTagIdAtPos({ line: 15, ch: 0 }, "li"); // before </ul> tag that follows an unclosed 'li'
});
});
it("should get 'br' tag for cursor positions inside <br>, not its parent 'li' tag", function () {
runs(function () {
checkTagIdAtPos({ line: 13, ch: 22 }, "br"); // inside the <br> tag of the second list item
});
});
it("should get 'ul' tag for cursor positions within 'ul' but outside of any unclosed 'li'.", function () {
runs(function () {
checkTagIdAtPos({ line: 12, ch: 0 }, "ul"); // before first '<li>' tag
checkTagIdAtPos({ line: 15, ch: 8 }, "ul"); // inside </ul>
});
});
it("should get 'table' tag for cursor positions that are not in any unclosed child tags", function () {
runs(function () {
checkTagIdAtPos({ line: 17, ch: 17 }, "table"); // inside an attribute of table tag
checkTagIdAtPos({ line: 32, ch: 6 }, "table"); // inside </table> tag
});
});
it("should get 'tr' tag for cursor positions between child tags", function () {
runs(function () {
checkTagIdAtPos({ line: 21, ch: 0 }, "tr"); // after a 'th' but before the start tag of another one
});
});
it("should get 'input' tag for cursor positions inside one of the 'input' tags.", function () {
runs(function () {
checkTagIdAtPos({ line: 34, ch: 61 }, "input"); // at the end of first input tag
checkTagIdAtPos({ line: 35, ch: 4 }, "input"); // at the first position of the 2nd input tag
});
});
it("should get 'option' tag for cursor positions in any unclosed 'option' tag.", function () {
runs(function () {
checkTagIdAtPos({ line: 40, ch: 0 }, "option"); // before second '<option>' tag
checkTagIdAtPos({ line: 41, ch: 28 }, "option"); // after third option tag that is unclosed
});
});
it("should NOT get 'option' tag for cursor positions in the parent tags of an unclosed 'option'.", function () {
runs(function () {
checkTagIdAtPos({ line: 42, ch: 5 }, "select"); // inside '</select>' tag
checkTagIdAtPos({ line: 43, ch: 5 }, "form"); // inside '</form>' tag
});
});
it("should get 'label' tag for cursor positions in the 'label' tag or its content.", function () {
runs(function () {
checkTagIdAtPos({ line: 37, ch: 17 }, "label"); // in the attribute of 'label' tag
checkTagIdAtPos({ line: 37, ch: 49 }, "label"); // in the text content
checkTagIdAtPos({ line: 37, ch: 55 }, "label"); // in the end 'label' tag
});
});
it("should get 'form' tag for cursor positions NOT in any form element.", function () {
runs(function () {
checkTagIdAtPos({ line: 35, ch: 0 }, "form"); // between two input tags
checkTagIdAtPos({ line: 43, ch: 2 }, "form"); // before </form> tag
});
});
it("should get 'hr' tag for cursor positions in <hr> tag, not its parent <form> tag.", function () {
runs(function () {
checkTagIdAtPos({ line: 36, ch: 6 }, "hr"); // inside <hr>
});
});
it("should get 'script' tag for cursor positions anywhere inside the tag including CDATA.", function () {
runs(function () {
checkTagIdAtPos({ line: 46, ch: 6 }, "script"); // before '<' of CDATA
checkTagIdAtPos({ line: 48, ch: 7 }, "script"); // right before '>' of CDATA
checkTagIdAtPos({ line: 45, ch: 18 }, "script"); // inside an attribute value of 'script' tag
checkTagIdAtPos({ line: 47, ch: 20 }, "script"); // before '<' of a literal string
checkTagIdAtPos({ line: 49, ch: 9 }, "script"); // inside 'script' end tag
});
});
it("should get 'footer' tag that is explicitly using all uppercase tag names.", function () {
runs(function () {
checkTagIdAtPos({ line: 50, ch: 3 }, "footer"); // in <FOOTER>
checkTagIdAtPos({ line: 50, ch: 20 }, "footer"); // in the text content
checkTagIdAtPos({ line: 50, ch: 30 }, "footer"); // in </FOOTER>
});
});
it("should get 'body' for text after an h1 that closed a previous uncleosd paragraph", function () {
runs(function () {
checkTagIdAtPos({ line: 53, ch: 2 }, "body"); // in the text content after the h1
});
});
});
describe("HTML Instrumentation in an HTML page with some invalid markups", function () {
beforeEach(function () {
runs(function () {
editor = SpecRunnerUtils.createMockEditor(InvalidHTMLDoc, "html").editor;
expect(editor).toBeTruthy();
instrumentedHTML = HTMLInstrumentation.generateInstrumentedHTML(editor);
elementCount = getIdToTagMap(instrumentedHTML, elementIds);
if (elementCount) {
HTMLInstrumentation._markText(editor);
verifyMarksCreated();
}
});
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(editor.document);
editor = null;
instrumentedHTML = "";
elementCount = 0;
elementIds = {};
});
it("should instrument all start tags except some empty tags", function () {
runs(function () {
expect(elementCount).toEqual(39);
});
});
it("should get 'script' tag for cursor positions anywhere inside the tag including CDATA.", function () {
runs(function () {
checkTagIdAtPos({ line: 6, ch: 11 }, "script"); // before '<' of CDATA
checkTagIdAtPos({ line: 8, ch: 12 }, "script"); // right before '>' of CDATA
checkTagIdAtPos({ line: 5, ch: 33 }, "script"); // inside an attribute value of 'script' tag
checkTagIdAtPos({ line: 7, ch: 25 }, "script"); // after '<' of a literal string
checkTagIdAtPos({ line: 9, ch: 9 }, "script"); // inside 'script' end tag
});
});
it("should get 'style' tag for cursor positions anywhere inside the tag including CDATA.", function () {
runs(function () {
checkTagIdAtPos({ line: 11, ch: 11 }, "style"); // before '<' of CDATA
checkTagIdAtPos({ line: 13, ch: 12 }, "style"); // right before '>' of CDATA
checkTagIdAtPos({ line: 10, ch: 26 }, "style"); // before '>' of the 'style' tag
checkTagIdAtPos({ line: 12, ch: 33 }, "style"); // inside a property value
checkTagIdAtPos({ line: 14, ch: 9 }, "style"); // inside 'style' end tag
});
});
it("should get 'i' tag for cursor position before </b>.", function () {
runs(function () {
checkTagIdAtPos({ line: 18, ch: 20 }, "i"); // after <i> and before </b>
checkTagIdAtPos({ line: 18, ch: 28 }, "i"); // immediately before </b>
});
});
it("should get 'p' tag after </b> because the </b> closed the overlapping <i>.", function () {
runs(function () {
checkTagIdAtPos({ line: 18, ch: 34 }, "p"); // between </b> and </i>
});
});
it("should get 'body' tag in a paragraph that has missing <p> tag, but has </p>", function () {
runs(function () {
checkTagIdAtPos({ line: 19, ch: 15 }, "body"); // before </p>
checkTagIdAtPos({ line: 19, ch: 38 }, "body"); // inside </p>
});
});
it("should get 'hr' tag for cursor positions in any forms of <hr> tag", function () {
runs(function () {
checkTagIdAtPos({ line: 48, ch: 7 }, "hr"); // inside <hr>
checkTagIdAtPos({ line: 50, ch: 9 }, "hr"); // inside <hr />
});
});
it("should get 'h2' tag for cursor positions between <wbr> and its invalide end tag.", function () {
runs(function () {
checkTagIdAtPos({ line: 20, ch: 35 }, "h2"); // in the text between <wbr> and </wbr>
});
});
it("should get 'wbr' tag for cursor positions inside <wbr>, not its parent <h2> tag.", function () {
runs(function () {
checkTagIdAtPos({ line: 20, ch: 30 }, "wbr"); // inside <wbr>
});
});
it("should get 'h2' tag for cursor positions inside invalid </wbr> tag.", function () {
runs(function () {
checkTagIdAtPos({ line: 20, ch: 40 }, "h2"); // inside </wbr>
});
});
it("should get 'name' tag for cursor positions before <name> and </name>.", function () {
runs(function () {
checkTagIdAtPos({ line: 21, ch: 8 }, "name"); // inside <name>
checkTagIdAtPos({ line: 21, ch: 12 }, "name"); // inside content of 'mame' tag
checkTagIdAtPos({ line: 21, ch: 22 }, "name"); // inside </name>
});
});
it("should get 'th' tag for cursor positions in any 'th' and their text contents.", function () {
runs(function () {
checkTagIdAtPos({ line: 24, ch: 16 }, "th"); // inside first th content
checkTagIdAtPos({ line: 25, ch: 21 }, "th"); // inside second </th>
checkTagIdAtPos({ line: 26, ch: 17 }, "th"); // at the end of third th content
checkTagIdAtPos({ line: 27, ch: 0 }, "th"); // before the next <tr>
});
});
it("should get 'input' tag for cursor positions in any input tag.", function () {
runs(function () {
checkTagIdAtPos({ line: 39, ch: 57 }, "input"); // inside value attribute that has <
checkTagIdAtPos({ line: 39, ch: 64 }, "input"); // between / and > of input tag
checkTagIdAtPos({ line: 40, ch: 61 }, "input"); // inside value attribute that has >
checkTagIdAtPos({ line: 40, ch: 63 }, "input"); // right before the invalid </input>
});
});
it("should get 'form' tag for cursor positions in any invalid end tag inside the form.", function () {
runs(function () {
checkTagIdAtPos({ line: 40, ch: 65 }, "form"); // inside </input>
});
});
it("should get 'p' tag for cursor positions inside an unclosed paragraph nested in a link.", function () {
runs(function () {
checkTagIdAtPos({ line: 49, ch: 71 }, "p"); // before </a> but after <p> tag
});
});
it("should get 'a' tag for cursor positions not in the unclosed 'p' child tag.", function () {
runs(function () {
checkTagIdAtPos({ line: 49, ch: 32 }, "a"); // inside </a>
checkTagIdAtPos({ line: 49, ch: 72 }, "a"); // inside </a>
});
});
});
// Log useful information when debugging a test.
function debuggingDump(result, previousDOM) {
console.log("Old DOM", HTMLSimpleDOM._dumpDOM(previousDOM));
console.log("New DOM", HTMLSimpleDOM._dumpDOM(result.dom));
console.log("Edits", JSON.stringify(result.edits, null, 2));
}
// Workaround for JSHint to not complain about the unused function
void(debuggingDump);
describe("HTML Instrumentation in dirty files", function () {
var changeList, offsets;
function setupEditor(docText, useOffsets) {
runs(function () {
if (useOffsets) {
var result = SpecRunnerUtils.parseOffsetsFromText(docText);
docText = result.text;
offsets = result.offsets;
}
editor = SpecRunnerUtils.createMockEditor(docText, "html").editor;
expect(editor).toBeTruthy();
editor.on("change.instrtest", function (event, editor, change) {
changeList = change;
});
instrumentedHTML = HTMLInstrumentation.generateInstrumentedHTML(editor);
elementCount = getIdToTagMap(instrumentedHTML, elementIds);
});
}
function checkMarkSanity() {
// Ensure that we don't have multiple marks for the same tagID.
var marks = editor._codeMirror.getAllMarks(),
foundMarks = {};
marks.forEach(function (mark) {
if (mark.hasOwnProperty("tagID")) {
if (foundMarks[mark.tagID]) {
expect("mark with ID " + mark.tagID).toBe("unique");
}
foundMarks[mark.tagID] = true;
}
});
}
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(editor.document);
editor = null;
instrumentedHTML = "";
elementCount = 0;
elementIds = {};
changeList = null;
offsets = null;
});
function doEditTest(origText, editFn, expectationFn, incremental, noRefresh) {
// We need to fully reset the editor/mark state between the full and incremental tests
// because if new DOM nodes are added by the edit, those marks will be present after the
// full test, messing up the incremental test.
if (!noRefresh) {
editor.document.refreshText(origText);
}
var previousDOM = HTMLSimpleDOM.build(editor.document.getText()),
result;
var clonedDOM = cloneDOM(previousDOM);
HTMLInstrumentation._markTextFromDOM(editor, previousDOM);
editFn(editor, previousDOM);
// Note that even if we pass a change list, `_updateDOM` will still choose to do a
// full reparse and diff if the change includes a structural character.
result = HTMLInstrumentation._updateDOM(previousDOM, editor, (incremental ? changeList : null));
checkMarkSanity();
var doc = new FakeDocument(clonedDOM);
var editHandler = new RemoteFunctions.DOMEditHandler(doc);
editHandler.apply(result.edits);
clonedDOM.compare(result.dom);
expectationFn(result, previousDOM, incremental);
}
function doFullAndIncrementalEditTest(editFn, expectationFn) {
var origText = editor.document.getText();
doEditTest(origText, editFn, expectationFn, false);
changeList = null;
if (HTMLInstrumentation._allowIncremental) {
doEditTest(origText, editFn, expectationFn, true);
}
}
// Common functionality between typeAndExpect() and deleteAndExpect().
function doOperationAndExpect(editor, curDOM, pos, edits, wasInvalid, numIterations, operationFn, posUpdateFn) {
var i, result, clonedDOM;
for (i = 0; i < numIterations; i++) {
clonedDOM = cloneDOM(curDOM);
operationFn(i, pos);
result = HTMLInstrumentation._updateDOM(curDOM, editor, wasInvalid ? null : changeList);
if (!edits) {
expect(Array.isArray(result.errors)).toBe(true);
wasInvalid = true;
} else {
var expectedEdit = edits[i];