Skip to content

Commit 4d91f1d

Browse files
committed
Added check in queueQuery to prevent duplicate selectors
1 parent 85122fe commit 4d91f1d

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/ElementQueries.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,25 @@
177177
if (typeof(allQueries[mode]) == 'undefined') allQueries[mode] = {};
178178
if (typeof(allQueries[mode][property]) == 'undefined') allQueries[mode][property] = {};
179179
if (typeof(allQueries[mode][property][value]) == 'undefined') allQueries[mode][property][value] = selector;
180-
else allQueries[mode][property][value] += ','+selector;
180+
else if (typeof selector === 'string') {
181+
// To build a regex from the selector we need to escape it first. The following function
182+
// is given on MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
183+
var escapedSelector = selector.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
184+
185+
// MATCHES full selector at the beginning, at the end, or somewhere inside the string.
186+
// DOES NOT MATCH if it acts as a subselector or super selector.
187+
var hasSelectorRegex = new RegExp(
188+
'(^' + escapedSelector + ',)|' +
189+
'(,' + escapedSelector + ',)|' +
190+
'(,' + escapedSelector + '$)|' +
191+
'(^' + escapedSelector + '$)'
192+
);
193+
194+
// Add the selector only if it is not already present
195+
if (!hasSelectorRegex.test(allQueries[mode][property][value])) {
196+
allQueries[mode][property][value] += ','+selector;
197+
}
198+
}
181199
}
182200

183201
function getQuery() {

0 commit comments

Comments
 (0)