forked from ilinsky/xpath2.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_helper.js
64 lines (58 loc) · 2.02 KB
/
doc_helper.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
/*
* XPath2 for NodeJS
* Some utility functions and xmldom DOMAdapter
*/
function parentChain(a) {
var r=[];
while(a.parentNode||a.ownerElement) r=[a=(a.parentNode||a.ownerElement)].concat(r);
return r;
}
function commonAncestor(a,b) {
if (b.length<a.length) return commonAncestor(b,a);
var c=null;
for(var n in a)
if (a[n]===b[n]) c=a[n]; else return c;
return c;
}
function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
function docGUID(doc) {
if (!doc.guid) doc.guid=guid();
return doc.guid;
}
var DOCUMENT_POSITION_DISCONNECTED=1;
var DOCUMENT_POSITION_PRECEDING=2;
var DOCUMENT_POSITION_FOLLOWING=4;
var DOCUMENT_POSITION_CONTAINS=8;
var DOCUMENT_POSITION_CONTAINED_BY=16;
var DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC=32;
function compareDocumentPosition(a,b) {
var vars={};
if (a===b) return 0;
if (a.ownerDocument!==b.ownerDocument)
return DOCUMENT_POSITION_DISCONNECTED|DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
|(docGUID(a.ownerDocument)>docGUID(b.ownerDocument)?DOCUMENT_POSITION_FOLLOWING:DOCUMENT_POSITION_PRECEDING);
vars.aa=parentChain(a);
vars.ab=parentChain(b);
if (vars.aa.indexOf(b)>=0) return DOCUMENT_POSITION_CONTAINS;
if (vars.ab.indexOf(a)>=0) return DOCUMENT_POSITION_CONTAINED_BY;
vars.ca=commonAncestor(vars.aa,vars.ab);
/*if (!vars.ca)
throw new Error("BUG!");*/
//console.log("compare order on common ancestor");
for(var n in vars.ca.childNodes) {
vars.n=vars.ca.childNodes[n];
//console.log("compare self");
//strange... this seems to be swapped, but otherwise nodes come out of order
if (vars.n===a) return DOCUMENT_POSITION_FOLLOWING;
if (vars.n===b) return DOCUMENT_POSITION_PRECEDING;
//console.log("compare chain");
if (vars.aa.indexOf(vars.n)>=0) return DOCUMENT_POSITION_FOLLOWING;
if (vars.ab.indexOf(vars.n)>=0) return DOCUMENT_POSITION_PRECEDING;
};
//throw new Error("BUG!");
};