1 | "use strict";
|
---|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
---|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
---|
4 | };
|
---|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
6 | exports.filters = void 0;
|
---|
7 | var nth_check_1 = __importDefault(require("nth-check"));
|
---|
8 | var boolbase_1 = require("boolbase");
|
---|
9 | function getChildFunc(next, adapter) {
|
---|
10 | return function (elem) {
|
---|
11 | var parent = adapter.getParent(elem);
|
---|
12 | return parent != null && adapter.isTag(parent) && next(elem);
|
---|
13 | };
|
---|
14 | }
|
---|
15 | exports.filters = {
|
---|
16 | contains: function (next, text, _a) {
|
---|
17 | var adapter = _a.adapter;
|
---|
18 | return function contains(elem) {
|
---|
19 | return next(elem) && adapter.getText(elem).includes(text);
|
---|
20 | };
|
---|
21 | },
|
---|
22 | icontains: function (next, text, _a) {
|
---|
23 | var adapter = _a.adapter;
|
---|
24 | var itext = text.toLowerCase();
|
---|
25 | return function icontains(elem) {
|
---|
26 | return (next(elem) &&
|
---|
27 | adapter.getText(elem).toLowerCase().includes(itext));
|
---|
28 | };
|
---|
29 | },
|
---|
30 | // Location specific methods
|
---|
31 | "nth-child": function (next, rule, _a) {
|
---|
32 | var adapter = _a.adapter, equals = _a.equals;
|
---|
33 | var func = nth_check_1.default(rule);
|
---|
34 | if (func === boolbase_1.falseFunc)
|
---|
35 | return boolbase_1.falseFunc;
|
---|
36 | if (func === boolbase_1.trueFunc)
|
---|
37 | return getChildFunc(next, adapter);
|
---|
38 | return function nthChild(elem) {
|
---|
39 | var siblings = adapter.getSiblings(elem);
|
---|
40 | var pos = 0;
|
---|
41 | for (var i = 0; i < siblings.length; i++) {
|
---|
42 | if (equals(elem, siblings[i]))
|
---|
43 | break;
|
---|
44 | if (adapter.isTag(siblings[i])) {
|
---|
45 | pos++;
|
---|
46 | }
|
---|
47 | }
|
---|
48 | return func(pos) && next(elem);
|
---|
49 | };
|
---|
50 | },
|
---|
51 | "nth-last-child": function (next, rule, _a) {
|
---|
52 | var adapter = _a.adapter, equals = _a.equals;
|
---|
53 | var func = nth_check_1.default(rule);
|
---|
54 | if (func === boolbase_1.falseFunc)
|
---|
55 | return boolbase_1.falseFunc;
|
---|
56 | if (func === boolbase_1.trueFunc)
|
---|
57 | return getChildFunc(next, adapter);
|
---|
58 | return function nthLastChild(elem) {
|
---|
59 | var siblings = adapter.getSiblings(elem);
|
---|
60 | var pos = 0;
|
---|
61 | for (var i = siblings.length - 1; i >= 0; i--) {
|
---|
62 | if (equals(elem, siblings[i]))
|
---|
63 | break;
|
---|
64 | if (adapter.isTag(siblings[i])) {
|
---|
65 | pos++;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return func(pos) && next(elem);
|
---|
69 | };
|
---|
70 | },
|
---|
71 | "nth-of-type": function (next, rule, _a) {
|
---|
72 | var adapter = _a.adapter, equals = _a.equals;
|
---|
73 | var func = nth_check_1.default(rule);
|
---|
74 | if (func === boolbase_1.falseFunc)
|
---|
75 | return boolbase_1.falseFunc;
|
---|
76 | if (func === boolbase_1.trueFunc)
|
---|
77 | return getChildFunc(next, adapter);
|
---|
78 | return function nthOfType(elem) {
|
---|
79 | var siblings = adapter.getSiblings(elem);
|
---|
80 | var pos = 0;
|
---|
81 | for (var i = 0; i < siblings.length; i++) {
|
---|
82 | var currentSibling = siblings[i];
|
---|
83 | if (equals(elem, currentSibling))
|
---|
84 | break;
|
---|
85 | if (adapter.isTag(currentSibling) &&
|
---|
86 | adapter.getName(currentSibling) === adapter.getName(elem)) {
|
---|
87 | pos++;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return func(pos) && next(elem);
|
---|
91 | };
|
---|
92 | },
|
---|
93 | "nth-last-of-type": function (next, rule, _a) {
|
---|
94 | var adapter = _a.adapter, equals = _a.equals;
|
---|
95 | var func = nth_check_1.default(rule);
|
---|
96 | if (func === boolbase_1.falseFunc)
|
---|
97 | return boolbase_1.falseFunc;
|
---|
98 | if (func === boolbase_1.trueFunc)
|
---|
99 | return getChildFunc(next, adapter);
|
---|
100 | return function nthLastOfType(elem) {
|
---|
101 | var siblings = adapter.getSiblings(elem);
|
---|
102 | var pos = 0;
|
---|
103 | for (var i = siblings.length - 1; i >= 0; i--) {
|
---|
104 | var currentSibling = siblings[i];
|
---|
105 | if (equals(elem, currentSibling))
|
---|
106 | break;
|
---|
107 | if (adapter.isTag(currentSibling) &&
|
---|
108 | adapter.getName(currentSibling) === adapter.getName(elem)) {
|
---|
109 | pos++;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | return func(pos) && next(elem);
|
---|
113 | };
|
---|
114 | },
|
---|
115 | // TODO determine the actual root element
|
---|
116 | root: function (next, _rule, _a) {
|
---|
117 | var adapter = _a.adapter;
|
---|
118 | return function (elem) {
|
---|
119 | var parent = adapter.getParent(elem);
|
---|
120 | return (parent == null || !adapter.isTag(parent)) && next(elem);
|
---|
121 | };
|
---|
122 | },
|
---|
123 | scope: function (next, rule, options, context) {
|
---|
124 | var equals = options.equals;
|
---|
125 | if (!context || context.length === 0) {
|
---|
126 | // Equivalent to :root
|
---|
127 | return exports.filters.root(next, rule, options);
|
---|
128 | }
|
---|
129 | if (context.length === 1) {
|
---|
130 | // NOTE: can't be unpacked, as :has uses this for side-effects
|
---|
131 | return function (elem) { return equals(context[0], elem) && next(elem); };
|
---|
132 | }
|
---|
133 | return function (elem) { return context.includes(elem) && next(elem); };
|
---|
134 | },
|
---|
135 | hover: dynamicStatePseudo("isHovered"),
|
---|
136 | visited: dynamicStatePseudo("isVisited"),
|
---|
137 | active: dynamicStatePseudo("isActive"),
|
---|
138 | };
|
---|
139 | /**
|
---|
140 | * Dynamic state pseudos. These depend on optional Adapter methods.
|
---|
141 | *
|
---|
142 | * @param name The name of the adapter method to call.
|
---|
143 | * @returns Pseudo for the `filters` object.
|
---|
144 | */
|
---|
145 | function dynamicStatePseudo(name) {
|
---|
146 | return function dynamicPseudo(next, _rule, _a) {
|
---|
147 | var adapter = _a.adapter;
|
---|
148 | var func = adapter[name];
|
---|
149 | if (typeof func !== "function") {
|
---|
150 | return boolbase_1.falseFunc;
|
---|
151 | }
|
---|
152 | return function active(elem) {
|
---|
153 | return func(elem) && next(elem);
|
---|
154 | };
|
---|
155 | };
|
---|
156 | }
|
---|