This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
/
self_or_descendant_state.js
80 lines (62 loc) · 1.88 KB
/
self_or_descendant_state.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
var debug = true;
function trace(message) {
if (debug) {
console.log(message);
}
}
function SelfOrDescendantState(axis, name, predicates) {
this.axis = 'self-or-descendant';
this.name = name;
if (predicates) {
// strip '[' and ']'
this.predicates = predicates.slice(1).slice(0, -1);
}
}
//SelfOrDescendantState.prototype.enter = function(depth) {
// trace('// state enter, state: ' + this.name + ' depth: ' + depth);
// this.enteredDepth = depth;
//};
//
//SelfOrDescendantState.prototype.leave = function(depth) {
// trace('// state leave, state: ' + this.name + ' depth: ' + depth);
//};
SelfOrDescendantState.prototype.matches = function(node, depth) {
trace('// match? node: ' + node.name + ' depth: ' + depth);
var match = this.matchesName(node);
if (match && this.predicates) {
match = match && this.matchesPredicate(node);
}
if (match) {
trace('// match');
this.enteredDepth = depth;
}
return match;
};
SelfOrDescendantState.prototype.unmatches = function(tag, depth) {
trace('// unmatch? depth: ' + depth + ' enteredDepth: ' + this.enteredDepth);
var unmatch = depth <= this.enteredDepth;
if (unmatch) {
trace('// unmatch');
}
return unmatch;
};
SelfOrDescendantState.prototype.matchesName = function(node) {
return node.name === this.name;
};
SelfOrDescendantState.prototype.matchesPredicate = function(node) {
// XXX: hardcoded to test @attr = literal
var predicate = this.predicates[0];
var left = predicate[0];
var op = predicate[1];
var right = predicate[2];
var lValue = node.attributes[left[1]];
var rValue = right;
if (op === '=') {
return lValue === rValue;
}
return false;
};
SelfOrDescendantState.prototype.toString = function() {
return '//' + this.name;
};
module.exports = SelfOrDescendantState;