pseudos.js
3.27 KB
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
81
82
83
84
85
86
87
88
89
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyPseudoArgs = exports.pseudos = void 0;
// While filters are precompiled, pseudos get called when they are needed
exports.pseudos = {
empty: function (elem, _a) {
var adapter = _a.adapter;
return !adapter.getChildren(elem).some(function (elem) {
// FIXME: `getText` call is potentially expensive.
return adapter.isTag(elem) || adapter.getText(elem) !== "";
});
},
"first-child": function (elem, _a) {
var adapter = _a.adapter, equals = _a.equals;
var firstChild = adapter
.getSiblings(elem)
.find(function (elem) { return adapter.isTag(elem); });
return firstChild != null && equals(elem, firstChild);
},
"last-child": function (elem, _a) {
var adapter = _a.adapter, equals = _a.equals;
var siblings = adapter.getSiblings(elem);
for (var i = siblings.length - 1; i >= 0; i--) {
if (equals(elem, siblings[i]))
return true;
if (adapter.isTag(siblings[i]))
break;
}
return false;
},
"first-of-type": function (elem, _a) {
var adapter = _a.adapter, equals = _a.equals;
var siblings = adapter.getSiblings(elem);
var elemName = adapter.getName(elem);
for (var i = 0; i < siblings.length; i++) {
var currentSibling = siblings[i];
if (equals(elem, currentSibling))
return true;
if (adapter.isTag(currentSibling) &&
adapter.getName(currentSibling) === elemName) {
break;
}
}
return false;
},
"last-of-type": function (elem, _a) {
var adapter = _a.adapter, equals = _a.equals;
var siblings = adapter.getSiblings(elem);
var elemName = adapter.getName(elem);
for (var i = siblings.length - 1; i >= 0; i--) {
var currentSibling = siblings[i];
if (equals(elem, currentSibling))
return true;
if (adapter.isTag(currentSibling) &&
adapter.getName(currentSibling) === elemName) {
break;
}
}
return false;
},
"only-of-type": function (elem, _a) {
var adapter = _a.adapter, equals = _a.equals;
var elemName = adapter.getName(elem);
return adapter
.getSiblings(elem)
.every(function (sibling) {
return equals(elem, sibling) ||
!adapter.isTag(sibling) ||
adapter.getName(sibling) !== elemName;
});
},
"only-child": function (elem, _a) {
var adapter = _a.adapter, equals = _a.equals;
return adapter
.getSiblings(elem)
.every(function (sibling) { return equals(elem, sibling) || !adapter.isTag(sibling); });
},
};
function verifyPseudoArgs(func, name, subselect) {
if (subselect === null) {
if (func.length > 2) {
throw new Error("pseudo-selector :".concat(name, " requires an argument"));
}
}
else if (func.length === 2) {
throw new Error("pseudo-selector :".concat(name, " doesn't have any arguments"));
}
}
exports.verifyPseudoArgs = verifyPseudoArgs;