1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const util_1 = require("./util");
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function hasTextValue(value) {
|
24 | return function (node) {
|
25 | return util_1.getTextContent(node) === value;
|
26 | };
|
27 | }
|
28 | function OR( /* ...rules */) {
|
29 | const rules = new Array(arguments.length);
|
30 | for (let i = 0; i < arguments.length; i++) {
|
31 | rules[i] = arguments[i];
|
32 | }
|
33 | return function (node) {
|
34 | for (let i = 0; i < rules.length; i++) {
|
35 | if (rules[i](node)) {
|
36 | return true;
|
37 | }
|
38 | }
|
39 | return false;
|
40 | };
|
41 | }
|
42 | function AND( /* ...rules */) {
|
43 | const rules = new Array(arguments.length);
|
44 | for (let i = 0; i < arguments.length; i++) {
|
45 | rules[i] = arguments[i];
|
46 | }
|
47 | return function (node) {
|
48 | for (let i = 0; i < rules.length; i++) {
|
49 | if (!rules[i](node)) {
|
50 | return false;
|
51 | }
|
52 | }
|
53 | return true;
|
54 | };
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 | function NOT(predicateFn) {
|
60 | return function (node) {
|
61 | return !predicateFn(node);
|
62 | };
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function parentMatches(predicateFn) {
|
69 | return function (node) {
|
70 | let parent = node.parentNode;
|
71 | while (parent !== undefined) {
|
72 | if (predicateFn(parent)) {
|
73 | return true;
|
74 | }
|
75 | parent = parent.parentNode;
|
76 | }
|
77 | return false;
|
78 | };
|
79 | }
|
80 | function hasAttr(attr) {
|
81 | return function (node) {
|
82 | return util_1.getAttributeIndex(node, attr) > -1;
|
83 | };
|
84 | }
|
85 | function hasAttrValue(attr, value) {
|
86 | return function (node) {
|
87 | return util_1.getAttribute(node, attr) === value;
|
88 | };
|
89 | }
|
90 | function hasClass(name) {
|
91 | return hasSpaceSeparatedAttrValue('class', name);
|
92 | }
|
93 | function hasTagName(name) {
|
94 | const n = name.toLowerCase();
|
95 | return function (node) {
|
96 | if (!node.tagName) {
|
97 | return false;
|
98 | }
|
99 | return node.tagName.toLowerCase() === n;
|
100 | };
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | function hasMatchingTagName(regex) {
|
108 | return function (node) {
|
109 | if (!node.tagName) {
|
110 | return false;
|
111 | }
|
112 | return regex.test(node.tagName.toLowerCase());
|
113 | };
|
114 | }
|
115 | function hasSpaceSeparatedAttrValue(name, value) {
|
116 | return function (element) {
|
117 | const attributeValue = util_1.getAttribute(element, name);
|
118 | if (typeof attributeValue !== 'string') {
|
119 | return false;
|
120 | }
|
121 | return attributeValue.split(' ').indexOf(value) !== -1;
|
122 | };
|
123 | }
|
124 | exports.hasSpaceSeparatedAttrValue = hasSpaceSeparatedAttrValue;
|
125 | function isDocument(node) {
|
126 | return node.nodeName === '#document';
|
127 | }
|
128 | exports.isDocument = isDocument;
|
129 | function isDocumentFragment(node) {
|
130 | return node.nodeName === '#document-fragment';
|
131 | }
|
132 | exports.isDocumentFragment = isDocumentFragment;
|
133 | function isElement(node) {
|
134 | return node.nodeName === node.tagName;
|
135 | }
|
136 | exports.isElement = isElement;
|
137 | function isTextNode(node) {
|
138 | return node.nodeName === '#text';
|
139 | }
|
140 | exports.isTextNode = isTextNode;
|
141 | function isCommentNode(node) {
|
142 | return node.nodeName === '#comment';
|
143 | }
|
144 | exports.isCommentNode = isCommentNode;
|
145 | exports.predicates = {
|
146 | hasClass: hasClass,
|
147 | hasAttr: hasAttr,
|
148 | hasAttrValue: hasAttrValue,
|
149 | hasMatchingTagName: hasMatchingTagName,
|
150 | hasSpaceSeparatedAttrValue: hasSpaceSeparatedAttrValue,
|
151 | hasTagName: hasTagName,
|
152 | hasTextValue: hasTextValue,
|
153 | AND: AND,
|
154 | OR: OR,
|
155 | NOT: NOT,
|
156 | parentMatches: parentMatches,
|
157 | };
|
158 |
|
\ | No newline at end of file |