Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 7478b3c

Browse files
authored
Merge pull request #10 from brave-experiments/simplify-types
reduce type possibilities for OperatorResult
2 parents 2aa390e + 536a6cc commit 7478b3c

File tree

3 files changed

+66
-71
lines changed

3 files changed

+66
-71
lines changed

out/main.js

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -147,26 +147,26 @@ const operatorCssSelector = (selector, element) => {
147147
if (trimmedSelector.startsWith('+')) {
148148
const subOperator = _stripOperator('+', trimmedSelector);
149149
if (subOperator === null) {
150-
return null;
150+
return [];
151151
}
152152
const nextSibNode = _nextSiblingElement(element);
153153
if (nextSibNode === null) {
154-
return null;
154+
return [];
155155
}
156-
return nextSibNode.matches(subOperator) ? nextSibNode : null;
156+
return nextSibNode.matches(subOperator) ? [nextSibNode] : [];
157157
}
158158
else if (trimmedSelector.startsWith('~')) {
159159
const subOperator = _stripOperator('~', trimmedSelector);
160160
if (subOperator === null) {
161-
return null;
161+
return [];
162162
}
163163
const allSiblingNodes = _allOtherSiblings(element);
164164
return allSiblingNodes.filter(x => x.matches(subOperator));
165165
}
166166
else if (trimmedSelector.startsWith('>')) {
167167
const subOperator = _stripOperator('>', trimmedSelector);
168168
if (subOperator === null) {
169-
return null;
169+
return [];
170170
}
171171
const allChildNodes = _allChildren(element);
172172
return allChildNodes.filter(x => x.matches(subOperator));
@@ -178,11 +178,11 @@ const operatorCssSelector = (selector, element) => {
178178
return [element];
179179
}
180180
else {
181-
return null;
181+
return [];
182182
}
183183
};
184184
const _hasPlainSelectorCase = (selector, element) => {
185-
return element.matches(selector) ? element : null;
185+
return element.matches(selector) ? [element] : [];
186186
};
187187
const _hasProceduralSelectorCase = (selector, element) => {
188188
var _a;
@@ -191,7 +191,7 @@ const _hasProceduralSelectorCase = (selector, element) => {
191191
? _allChildrenRecursive(element)
192192
: [element];
193193
const matches = compileAndApplyProceduralSelector(selector, initElements);
194-
return matches.length === 0 ? null : element;
194+
return matches.length === 0 ? [] : [element];
195195
};
196196
// Implementation of ":has" rule
197197
const operatorHas = (instruction, element) => {
@@ -206,14 +206,14 @@ const operatorHas = (instruction, element) => {
206206
const operatorHasText = (instruction, element) => {
207207
const text = element.innerText;
208208
const valueTest = _extractValueMatchRuleFromStr(instruction);
209-
return valueTest(text) ? element : null;
209+
return valueTest(text) ? [element] : [];
210210
};
211211
const _notPlainSelectorCase = (selector, element) => {
212-
return element.matches(selector) ? null : element;
212+
return element.matches(selector) ? [] : [element];
213213
};
214214
const _notProceduralSelectorCase = (selector, element) => {
215215
const matches = compileAndApplyProceduralSelector(selector, [element]);
216-
return matches.length === 0 ? element : null;
216+
return matches.length === 0 ? [element] : [];
217217
};
218218
// Implementation of ":not" rule
219219
const operatorNot = (instruction, element) => {
@@ -234,17 +234,17 @@ const operatorMatchesProperty = (instruction, element) => {
234234
if (!valueTest(propValue)) {
235235
continue;
236236
}
237-
return element;
237+
return [element];
238238
}
239-
return null;
239+
return [];
240240
};
241241
// Implementation of ":min-text-length" rule
242242
const operatorMinTextLength = (instruction, element) => {
243243
const minLength = +instruction;
244244
if (minLength === W.NaN) {
245245
throw new Error(`min-text-length: Invalid arg, ${instruction}`);
246246
}
247-
return element.innerText.trim().length >= minLength ? element : null;
247+
return element.innerText.trim().length >= minLength ? [element] : [];
248248
};
249249
// Implementation of ":matches-attr" rule
250250
const operatorMatchesAttr = (instruction, element) => {
@@ -257,9 +257,9 @@ const operatorMatchesAttr = (instruction, element) => {
257257
if (attrValue === null || !valueTest(attrValue)) {
258258
continue;
259259
}
260-
return element;
260+
return [element];
261261
}
262-
return null;
262+
return [];
263263
};
264264
// Implementation of ":matches-css-*" rules
265265
const operatorMatchesCSS = (beforeOrAfter, cssInstruction, element) => {
@@ -269,19 +269,19 @@ const operatorMatchesCSS = (beforeOrAfter, cssInstruction, element) => {
269269
if (styleValue === undefined) {
270270
// We're querying for a style property that doesn't exist, which
271271
// trivially doesn't match then.
272-
return null;
272+
return [];
273273
}
274-
return expectedVal === styleValue ? element : null;
274+
return expectedVal === styleValue ? [element] : [];
275275
};
276276
// Implementation of ":matches-media" rule
277277
const operatorMatchesMedia = (instruction, element) => {
278-
return W.matchMedia(instruction).matches ? element : null;
278+
return W.matchMedia(instruction).matches ? [element] : [];
279279
};
280280
// Implementation of ":matches-path" rule
281281
const operatorMatchesPath = (instruction, element) => {
282282
const pathAndQuery = W.location.pathname + W.location.search;
283283
const matchRule = _extractValueMatchRuleFromStr(instruction);
284-
return matchRule(pathAndQuery) ? element : null;
284+
return matchRule(pathAndQuery) ? [element] : [];
285285
};
286286
const _upwardIntCase = (intNeedle, element) => {
287287
if (intNeedle < 1 || intNeedle >= 256) {
@@ -292,7 +292,13 @@ const _upwardIntCase = (intNeedle, element) => {
292292
currentElement = currentElement.parentNode;
293293
intNeedle -= 1;
294294
}
295-
return (currentElement === null) ? null : _asHTMLElement(currentElement);
295+
if (currentElement === null) {
296+
return [];
297+
}
298+
else {
299+
const htmlElement = _asHTMLElement(currentElement);
300+
return (htmlElement === null) ? [] : [htmlElement];
301+
}
296302
};
297303
const _upwardProceduralSelectorCase = (selector, element) => {
298304
const childFilter = compileProceduralSelector(selector);
@@ -304,11 +310,11 @@ const _upwardProceduralSelectorCase = (selector, element) => {
304310
}
305311
const matches = applyCompiledSelector(childFilter, [currentElement]);
306312
if (matches.length !== 0) {
307-
return currentElement;
313+
return [currentElement];
308314
}
309315
needle = currentElement.parentNode;
310316
}
311-
return null;
317+
return [];
312318
};
313319
const _upwardPlainSelectorCase = (selector, element) => {
314320
let needle = element;
@@ -318,11 +324,11 @@ const _upwardPlainSelectorCase = (selector, element) => {
318324
break;
319325
}
320326
if (currentElement.matches(selector)) {
321-
return currentElement;
327+
return [currentElement];
322328
}
323329
needle = currentElement.parentNode;
324330
}
325-
return null;
331+
return [];
326332
};
327333
// Implementation of ":upward" rule
328334
const operatorUpward = (instruction, element) => {
@@ -432,7 +438,7 @@ const applyCompiledSelector = (selector, initNodes) => {
432438
// if it passes for one element, then it will pass for all elements.
433439
if (fastPathOperatorTypes.includes(operatorType)) {
434440
const firstNode = nodesToConsider[0];
435-
if (operatorFunc(firstNode) === null) {
441+
if (operatorFunc(firstNode).length === 0) {
436442
nodesToConsider = [];
437443
}
438444
// Note that unless we've taken the if-true branch above, then
@@ -443,15 +449,7 @@ const applyCompiledSelector = (selector, initNodes) => {
443449
let newNodesToConsider = [];
444450
for (const aNode of nodesToConsider) {
445451
const result = operatorFunc(aNode);
446-
if (result === null) {
447-
continue;
448-
}
449-
else if (Array.isArray(result)) {
450-
newNodesToConsider = newNodesToConsider.concat(result);
451-
}
452-
else {
453-
newNodesToConsider.push(result);
454-
}
452+
newNodesToConsider = newNodesToConsider.concat(result);
455453
}
456454
nodesToConsider = newNodesToConsider;
457455
}

src/declarations.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type CSSValue = string
1414

1515
type OperatorType = string
1616
type OperatorArg = CSSSelector | ProceduralSelector | string
17-
type OperatorResult = HTMLElement | HTMLElement[] | null
17+
type OperatorResult = HTMLElement[]
1818

1919
type UnboundStringFunc = (arg: string, element: HTMLElement) => OperatorResult
2020
type UnboundChildRuleOrStringFunc = (

0 commit comments

Comments
 (0)