-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjquery.wrapSelection.js
781 lines (715 loc) · 30.5 KB
/
jquery.wrapSelection.js
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
/**
* wrapSelection jQuery plugin v0.2 beta-2
* @copyright Copyright (c) 2008, Crossway Books
* @author Stephen Smith
* @author Jeremy Peterson
* @author Noe Nieto
* @version 0.2.1
*/
/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true,
strict:false, undef:true, curly:true, browser:true, devel:true,
jquery:true, indent:4, maxerr:50, evil:true, boss:true */
(function ($) {
/**
* jquery getRangeAt function
*/
$.fn.getRangeAt = function () {
var selectionParent = this;// element from the mouseup
var range = $.fn.range;// Reference to range object
// Initialize variables
range.ClearVariables();
range.setRange();// gets Selection range
//Verify what container the selection is allowed in.
//Check if First node Selection is in selectionParent
//Assume mouseUp is in the selectionParent (or last node)
if (this[0] === document) {
// Skips check if called like $().wrapSelection
// Do nothing
}
else {
var checkFirst = $(range.startContainer).parents().index(selectionParent);
var checkLast = $(range.endContainer).parents().index(selectionParent);
// restrict range to a specific container
if (checkFirst === -1 || checkLast === -1) {
range.ClearVariables();
return false;
}
}
// returns range object, no chaining when getting Range
return range;
};
$.fn.wrapSelection = function (options) {
var range = $.fn.range;
var selectClass = 'sel_' + new Date().getTime();// Unique Class, created on each highlight
var defaults = {
fitToWord: true,
wrapRange: false,
selectClass: selectClass,
regexElementBlockers: new RegExp(/^BR$/),// fitToWord Var
regexWordCharacterBasic: new RegExp(/^[A-Za-z0-9'\-]$/),// fitToWord Var
regexWordCharacterFull: new RegExp(/^[A-Za-z0-9':,\-]$/),// fitToWord Var
regexWordPunc: new RegExp(/^[:,]$/),// fitToWord Var
regexWordNumbers: new RegExp(/^[0-9]$/)// fitToWord Var
};
// build main options before element iteration
var opts = $.extend({}, defaults, options);
//Creates the range object
function setWrapRange(element, newRange) {
//console.log('set-Element:', element);
if (newRange) {
$.fn.range = newRange;
}
else {
// test without parent call
$(element).getRangeAt();
}
}
function SplitText() {
var range = $.fn.range;
var myIsSameNode = (range.startContainer === range.endContainer);
if (range.startContainer.nodeType === 3 && range.startOffset > 0) {
var myNew = range.startContainer.splitText(range.startOffset);
//if they're the same node, we want to make sure to assign the end to the
//same as the start
if (myIsSameNode) {
range.endContainer = myNew;
range.endOffset = range.endOffset - range.startContainer.length;
}
range.startContainer = myNew;
range.startOffset = 0;
}
if (range.endContainer.nodeType === 3 && range.endOffset < range.endContainer.length) {
range.endContainer.splitText(range.endOffset);
range.endOffset = range.endContainer.length;
}
}
function getNextChar(myContainer, myOffset) {
if (myOffset < 0) {
var myPrevContainer = $.fn.wrapSelection.dom.GetPreviousTextNode(myContainer);
if (myPrevContainer) {
myContainer = myPrevContainer;
myOffset = myContainer.length;
}
}
if (myOffset < myContainer.length - 1) {
return {container: myContainer,
offset: myOffset + 1,
character: myContainer.nodeValue.substr(myOffset + 1, 1)
};
}
else {
var myNext = $.fn.wrapSelection.dom.GetNextTextNode(myContainer, myContainer.parentNode);
if (!myNext) {
return {container: myContainer,
offset: myOffset,
character: ''
};
}
var myNextElement = $.fn.wrapSelection.dom.GetNextSiblingElement(myContainer);
while (myNextElement && $.fn.compareDocumentPosition(myNext, myNextElement) & 2) {
if (myNextElement.nodeName.match(opts.regexElementBlockers)) {
return {container: myContainer,
offset: myOffset,
character: ''};
}
myNextElement = $.fn.wrapSelection.dom.GetNextSiblingElement(myNextElement);
}
return {container: myNext,
offset: 0,
character: myNext.nodeValue.substr(0, 1)
};
}
}
function getPrevChar(myContainer, myOffset) {
if (myOffset > 0) {
return {container: myContainer,
offset: myOffset - 1,
character: myContainer.nodeValue.substr(myOffset - 1, 1)
};
}
else {
var myPrev = $.fn.wrapSelection.dom.GetPreviousTextNode(myContainer);
if (!myPrev) {
return {container: myContainer, offset: myOffset, character: ''};
}
var myPrevElement = $.fn.wrapSelection.dom.GetPreviousSiblingElement(myContainer);
while (myPrevElement && $.fn.compareDocumentPosition(myPrev, myPrevElement) & 4) {
if (myPrevElement.nodeName.match(opts.regexElementBlockers)) {
return {container: myContainer,
offset: myOffset,
character: ''
};
}
myPrevElement = $.fn.wrapSelection.dom.GetPreviousSiblingElement(myPrevElement);
}
return {container: myPrev,
offset: myPrev.length - 1,
character: myPrev.nodeValue.substr(myPrev.length - 1, 1)
};
}
}
function fitToEndWord(myContainer, myOffset, myType) {
var myChar = '';
if (myOffset > 0) {
myChar = myContainer.nodeValue.substr(myOffset - 1, 1);
}
else {
var myReverse = getPrevChar(myContainer, myOffset);
//if the prev character is also a word, then assume it's part
//of same word and it's ok to go forward
if (opts.regexWordCharacterFull.test(myReverse.character)) {
myChar = myContainer.nodeValue.substr(myOffset, 0, 1);
myOffset = 1;
}
}
if (opts.regexWordCharacterBasic.test(myChar)) {//go forward
if (myType === 'normal') {
var myNormal = getNextChar(myContainer, myOffset - 1);
if (opts.regexWordCharacterFull.test(myNormal.character)) {
return fitToEndWord(myNormal.container,
myNormal.offset + 1,
'normal');
}
}
return {container: myContainer, offset: myOffset};
}
//possibly go back or forward, depending on context
else if (myType === 'normal' && opts.regexWordPunc.test(myChar)) {
var myNormal = getNextChar(myContainer, myOffset);
if (opts.regexWordNumbers.test(myNormal.character)) {
return fitToEndWord(myNormal.container,
myNormal.offset,
'normal');
}
else {
return {container: myContainer, offset: myOffset - 1};
}
}
//otherwise go back
var myReverse = getPrevChar(myContainer, myOffset - 1);
if (myReverse.character.length === 1) {
return fitToEndWord(myReverse.container,
myReverse.offset + 1,
'reverse');
}
else {
return {container: myContainer, offset: myOffset};
}
}
function fitToStartWord(myContainer, myOffset, myType) {
var myChar = myContainer.nodeValue.substr(myOffset, 1);
//go back
if (opts.regexWordCharacterBasic.test(myChar)) {
if (myType === 'normal') {
var myPrev = getPrevChar(myContainer, myOffset);
if (opts.regexWordCharacterFull.test(myPrev.character)) {
return fitToStartWord(myPrev.container,
myPrev.offset,
'normal');
}
}
return {container: myContainer, offset: myOffset};
}
//possibly go back or forward, depending on context
else if (myType === 'normal' && opts.regexWordPunc.test(myChar)) {
var myPrev = getPrevChar(myContainer, myOffset);
if (opts.regexWordNumbers.test(myPrev.character)) {
return fitToStartWord(myPrev.container,
myPrev.offset,
'normal');
}
}
var myNext = getNextChar(myContainer, myOffset);
if (myNext.character.length === 1) {
return fitToStartWord(myNext.container,
myNext.offset,
'reverse');
}
else {
return {container: myContainer, offset: myOffset};
}
}
//Adjusts the range object to go around the words
function FitToWord() {
var range = $.fn.range;
var myStart = fitToStartWord(range.startContainer, range.startOffset, 'normal');
var myEnd = fitToEndWord(range.endContainer, range.endOffset, 'normal');
range.startContainer = myStart.container;
range.startOffset = myStart.offset;
range.endContainer = myEnd.container;
range.endOffset = myEnd.offset;
}
function doWrap() {
var myRange = $.fn.range;
var Spans = [];
if (!myRange.startContainer || !myRange.endContainer) {
return false;
}
var myNodes = myRange.GetContainedNodes();
var iLength = myNodes.length;
//myNodes is arranged by level, so everything at the same level can be surrounded by a <span>
var myNodesSurrounded = 0;
for (var i = 0; i < iLength; i++) {
if (!myNodes[i][0]) {
continue;
}
for (var j = 0, jLength = myNodes[i].length; j < jLength; j++) {
$(myNodes[i][j])
//all child and this as tag
.find('*').add($(myNodes[i][j]))
//get child nodes and add this if it text
.contents().add($(myNodes[i][j]))
//get only text and whap it's
.filter(function(){
if (this.nodeType !== 3)
return false;
return $.trim(this.textContent).length > 0;
}).wrap(makeSpanElement());
myNodesSurrounded += 1;
}
}
return myNodesSurrounded;
}
function makeSpanElement() {
var mySpan = document.createElement('span');
mySpan.className = opts.selectClass;
return mySpan;
}
//Start doing stuff
setWrapRange(this, opts.wrapRange);
if (range.startContainer && range.endContainer) {
if (opts.fitToWord) {
FitToWord();
}
SplitText();
var myCount = doWrap();
if (myCount) {
range.ClearAllRanges();
}
else {
range.ClearVariables();
}
// return opts.selectClass objects
return $('.' + opts.selectClass);
}
else {
return $([]);// return empty node
}
};// END wrapSelection
$.fn.range = {
onlySpacesMatch: new RegExp(/[^\t\r\n ]/),
containedNodes: null,
selection: null,
commonAncestorContainer: null,
startContainer: null,
startOffset: null,
endContainer: null,
endOffset: null,
collapsed: true,// default if null is true
setRange : function () {
if (window.getSelection) {
this.selection = window.getSelection();
}
else if (document.selection) { // should come last; Opera!
this.selection = document.selection.createRange();
}
if (this.selection.getRangeAt) {
var range = this.selection.getRangeAt(0);
}
else { // Safari!
var range = document.createRange();
range.setStart(this.selection.anchorNode,
this.selection.anchorOffset);
range.setEnd(this.selection.focusNode,
this.selection.focusOffset);
}
if (!range.toString().match(this.onlySpacesMatch)) {
return false;
}
this.startContainer = range.startContainer;
this.startOffset = range.startOffset;
this.endContainer = range.endContainer;
this.endOffset = range.endOffset;
this.collapsed = range.collapsed;
},
ClearAllRanges: function () {
if (!$.fn.range.selection) {
return;
}
//Firefox has bugs if you don't do both
$.fn.range.selection.removeAllRanges();
$.fn.range.ClearVariables();
},
ClearVariables: function () {
this.selection = null;
this.commonAncestorContainer = null;
this.containedNodes = null;
this.startContainer = null;
this.startOffset = null;
this.endContainer = null;
this.endOffset = null;
this.collapsed = true;// Defualt is true if collapsed
},
GetContainedNodes: function () {
return this.doGetContainedNodes();
},
doGetContainedNodes: function () {
if (this.containedNodes) {
return this.containedNodes;
}
if (!this.startContainer || !this.endContainer) {
return [];
}
var myStart = this.startContainer;
var myEnd = this.endContainer;
var myNodes = new Array([]);
var myNode = myStart;
var myPosition = $.fn.compareDocumentPosition(myStart, myEnd);
var myParent = myNode.parentNode;
var i = 0;
//while the current node is before
while ((myPosition & 4) || myPosition === 0) {
//the current node contains the end node
if (myPosition & 16) {
myNode = myNode.firstChild;
}
else {
// we're at a new level (either up or down), so we need a new span
if (myParent !== myNode.parentNode) {
i++;
myNodes[i] = [];
myParent = myNode.parentNode;
}
myNodes[i].push(myNode);
myNode = $.fn.wrapSelection.dom.GetNextSiblingOrParent(myNode);
if (myPosition === 0) {
break;
}
}
myPosition = $.fn.compareDocumentPosition(myNode, myEnd);
}
this.containedNodes = myNodes;
return myNodes;
}
};
// DOM Extend
$.fn.wrapSelection.dom = {
GetNextSiblingElement: function (myNode) {
return $.fn.wrapSelection.dom.getElementOrder(myNode, 'next');
},
GetNextSiblingOrParent: function (myNode) {
return $.fn.wrapSelection.dom.getSiblingOrParentOrder(myNode, 'next');
},
GetNextTextNode: function (myNode, myParent) {
while (myNode = $.fn.wrapSelection.dom.getNodeOrder(myNode, myParent, 'next')) {
if (myNode.nodeType === 3) {
return myNode;
}
}
return myNode;
},
GetPreviousSiblingElement: function (myNode) {
return this.getElementOrder(myNode, 'previous');
},
GetPreviousSiblingElement: function (myNode) {
return this.getElementOrder(myNode, 'previous');
},
GetPreviousTextNode: function (myNode, myParent) {
while (myNode = $.fn.wrapSelection.dom.getNodeOrder(myNode, myParent, 'previous')) {
if (myNode.nodeType === 3) {
return myNode;
}
}
return myNode;
},
getElementOrder: function (myNode, myType) {
myType += 'Sibling';
while (myNode[myType] && myNode[myType].nodeType !== 1) {
myNode = myNode[myType];
}
return myNode[myType];
},
getSiblingOrParentOrder: function (myNode, myOrder) {
var mySibling = myOrder + 'Sibling';
if (myNode[mySibling]) {
return myNode[mySibling];
}
else if (myNode.parentNode) {
return this.getSiblingOrParentOrder(myNode.parentNode,
myOrder);
}
else {
return null;
}
},
getNodeOrder: function (myNode, myParent, myOrder) {
//checkCurrent should usually only be called recursively
if (typeof myParent === 'undefined') {
myParent = document.body;
}
if (myNode.hasChildNodes()) {
return (myOrder === 'next') ? myNode.firstChild : myNode.lastChild;
}
if (myNode === myParent) {
return null;
}
var mySibling = (myOrder === 'next') ? 'nextSibling' : 'previousSibling';
if (myNode[mySibling]) {
return myNode[mySibling];
}
while (myNode = myNode.parentNode) {
if (myNode === myParent) {
return null;
}
if (myNode[mySibling]) {
return myNode[mySibling];
}
}
return null;
}
};
// Integrate Internet Explorer Code
if ($.browser.msie) {
$.extend($.fn.range, {
ClearAllRanges: function () {
if (this.selection) {
//clear the current selection; we don't want it hanging around
this.selection.empty();
}
this.ClearVariables();
},
setRange : function () {
this.selection = document.selection;
var myRange = this.selection.createRange();
var myText = myRange.text;
if (!myText.length) {
return false;
}
if (!myText.match(this.onlySpacesMatch)) {
//if only whitespace, return
return false;
}
var myStart = this.getInitialContainer(myRange.duplicate(), 'start');
var myStartIndex = $.fn.wrapSelection.dom.SourceIndex(myStart.container, 'string');
var myEnd = this.getInitialContainer(myRange.duplicate(), 'end');
if (myStartIndex === $.fn.wrapSelection.dom.SourceIndex(myEnd.container, 'string')) {
myStart.container = myEnd.container;
}
this.startContainer = myStart.container;
this.startOffset = myStart.offset;
this.endContainer = myEnd.container;
this.endOffset = myEnd.offset;
this.collapsed = (myStart.container === myEnd.container && myStart.offset === myEnd.offset);
// Fix Hightlight for IE that get's reset by getInitialContainer start node (myNode.insertData)
myRange.select();
return true;
},
getInitialContainer: function (myRange, myType) {
if (myType === 'start') {
//collapse to start
myRange.collapse(true);
}
else {
//collapse to end
myRange.collapse(false);
}
var myParent = myRange.parentElement();
myRange.pasteHTML('<span id="range-temp"></span>');
var myTemp = $('#range-temp')[0];
var myOffset = 0;
var myNode = $.fn.wrapSelection.dom.GetNextTextNode(myTemp, myTemp.parentNode);
if (!myNode) {
myNode = $.fn.wrapSelection.dom.GetPreviousTextNode(myTemp, myTemp.parentNode);
myOffset = myNode.length;
}
myTemp.parentNode.removeChild(myTemp);
// Get's offset and merges adjacent textnodes together
if (myType === "start") {
if (myNode.previousSibling && myNode.previousSibling.nodeType === 3) {
var myPrev = myParent.removeChild(myNode.previousSibling);
myOffset += myPrev.length;
myNode.insertData(0, myPrev.nodeValue);
}
}
else {// End node
if (myNode.previousSibling && myNode.previousSibling.nodeType === 3) {
var myPrev = myNode.previousSibling;
myOffset += myPrev.length;
myParent.removeChild(myNode);
myPrev.appendData(myNode.nodeValue);
myNode = myPrev;
}
}
return { container: myNode, offset: myOffset };
}
});
$.extend($.fn.wrapSelection.dom, {
SourceIndex: function (myNode, myType) {
var myOut = [];
do {
var myOffset = 0;
while (myNode.previousSibling) {
myNode = myNode.previousSibling;
myOffset++;
}
myOut.unshift(myOffset);
}
while (myNode = myNode.parentNode);
if (myType && myType === 'string') {
return myOut.join('.');
}
return myOut;
}
});
}
/** END Internet Explorer Code **/
// compareDocumentPosition - MIT Licensed, by ob. http://plugins.jquery.com/project/compareDocumentPosition
$.fn.compareDocumentPosition = function (node1, node2) {
//node.ownerDocument gives the document object, which isn't the right info for a disconnect
function getRootParent(node) {
var parent;
do {
parent = node;
}
while (node = node.parentNode);
return parent;
}
//Gecko, Opera have it built-in
if ("compareDocumentPosition" in document.documentElement) {
$.fn.compareDocumentPosition = function (node1, node2) {
return node1.compareDocumentPosition(node2);
};
}
//Internet Explorer
else if ("sourceIndex" in document.documentElement && "contains" in document.documentElement) {
$.fn.compareDocumentPosition = function (node1, node2) {
//Compare Position - MIT Licensed, John Resig; http://ejohn.org/blog/comparing-document-position/
//Already checked for equality and disconnect
function comparePosition(node1, node2) {
return (node1.contains(node2) && 16) +
(node2.contains(node1) && 8) +
(node1.sourceIndex >= 0 && node2.sourceIndex >= 0 ?
(node1.sourceIndex < node2.sourceIndex && 4) +
(node1.sourceIndex > node2.sourceIndex && 2) :
1);
}
//get a node with a sourceIndex to use
function getUseNode(node) {
//if the node already has a sourceIndex, use that node
if ("sourceIndex" in node) {
return node;
}
//otherwise, insert a comment (which has a sourceIndex but minimal DOM impact) before the node and use that
return node.parentNode.insertBefore(document.createComment(""), node);
}
if (node1 === node2) {
return 0;
}
//if they don't have the same parent, there's a disconnect
if (getRootParent(node1) !== getRootParent(node2)) {
return 1;
}
//use this if both nodes have a sourceIndex (text nodes don't)
if ("sourceIndex" in node1 && "sourceIndex" in node2) {
return comparePosition(node1, node2);
}
//document will definitely contain the other node
if (node1 === document) {
return 20;
}
else if (node2 === document) {
return 10;
}
//get sourceIndexes to use for both nodes
var useNode1 = getUseNode(node1), useNode2 = getUseNode(node2);
//call this function again to get the result
var result = comparePosition(useNode1, useNode2);
//clean up if needed
if (node1 !== useNode1) {
useNode1.parentNode.removeChild(useNode1);
}
if (node2 !== useNode2) {
useNode2.parentNode.removeChild(useNode2);
}
return result;
};
}
else {
//takes the sortable string from getOffset
function compareOffsetStrings(offset1, offset2) {
//they're siblings or at the same depth
if (offset1.length === offset2.length) {
return (offset1 < offset2) ? 4 : 2;
}
//the first one is either a parent or at a shallower depth
else if (offset1.length < offset2.length) {
//truncate the longer one
var offset2start = offset2.substr(0, offset1.length);
//if they're the same at this point, we know node1 is a parent
if (offset1 === offset2start) {
return 20;
}
//call itself again now that they're the same length
return compareOffsetStrings(offset1, offset2start);
}
else {
//flip the order of the arguments...
var result = compareOffsetStrings(offset2, offset1);
//...then shift the bits to get the correct result
return (result & 4) ? result >> 1 : result << 1;
}
}
//make a string that's sortable to determine a sourceIndex
function getOffsetString(node) {
var offsets = [];
do {
var offset = 0, prev = node;
//count preceding siblings
while (prev = prev.previousSibling) {
offset++;
}
//get the total number of sibling nodes (before and after)
var padLength = node.parentNode.childNodes.length.toString().length;
var offsetLength = offset.toString().length;
//zero-pad the offset to make sure the string compares properly
if (padLength > offsetLength) {
for (; offsetLength <= padLength; offsetLength++) {
offset = "0" + offset;
}
}
offsets.unshift(offset);
}
while ((node = node.parentNode) && node !== document);
//reverse the array to start the string at the top of the tree
//return the final delimited string
return offsets.join(".");
}
//Safari and others; will work in IE
//inspired by base2: http://code.google.com/p/base2/
$.fn.compareDocumentPosition = function (node1, node2) {
if (node1 === node2) {
return 0;
}
if (getRootParent(node1) !== getRootParent(node2)) {
return 1;
}
//contains() only works if both are elements
if (node1 === document ||
("contains" in node1 &&
"contains" in node2 && node1.contains(node2))) {
return 20;
}
else if (node2 === document ||
("contains" in node1 &&
"contains" in node2 && node2.contains(node1))) {
return 10;
}
return compareOffsetStrings(getOffsetString(node1),
getOffsetString(node2));
};
}
//now that we've redefined the function during the first run, run it to get the actual value
return $.fn.compareDocumentPosition(node1, node2);
};
// end of closure
})(jQuery);