Skip to content

Commit 707d770

Browse files
committed
code review: fix recursivity in HTML filtering's procedural selectors
1 parent 31791f2 commit 707d770

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

src/js/html-filtering.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
this.pselector = new PSelector(task[1]);
6363
};
6464
PSelectorIfTask.prototype.target = true;
65+
Object.defineProperty(PSelectorIfTask.prototype, 'invalid', {
66+
get: function() {
67+
return this.pselector.invalid;
68+
}
69+
});
6570
PSelectorIfTask.prototype.exec = function(input) {
6671
var output = [];
6772
for ( var node of input ) {
@@ -113,7 +118,6 @@
113118
[ ':xpath', PSelectorXpathTask ]
114119
]);
115120
}
116-
this.invalid = false;
117121
this.raw = o.raw;
118122
this.selector = o.selector;
119123
this.tasks = [];
@@ -125,10 +129,16 @@
125129
this.invalid = true;
126130
break;
127131
}
128-
this.tasks.push(new ctor(task));
132+
var pselector = new ctor(task);
133+
if ( pselector instanceof PSelectorIfTask && pselector.invalid ) {
134+
this.invalid = true;
135+
break;
136+
}
137+
this.tasks.push(pselector);
129138
}
130139
};
131140
PSelector.prototype.operatorToTaskMap = undefined;
141+
PSelector.prototype.invalid = false;
132142
PSelector.prototype.prime = function(input) {
133143
var root = input || docRegister;
134144
if ( this.selector !== '' ) {
@@ -145,6 +155,19 @@
145155
}
146156
return nodes;
147157
};
158+
PSelector.prototype.test = function(input) {
159+
if ( this.invalid ) { return false; }
160+
var nodes = this.prime(input), AA = [ null ], aa;
161+
for ( var node of nodes ) {
162+
AA[0] = node; aa = AA;
163+
for ( var task of this.tasks ) {
164+
aa = task.exec(aa);
165+
if ( aa.length === 0 ) { break; }
166+
}
167+
if ( aa.length !== 0 ) { return true; }
168+
}
169+
return false;
170+
};
148171

149172
var logOne = function(details, selector) {
150173
loggerRegister.writeOne(

0 commit comments

Comments
 (0)