UNPKG

4.46 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15Object.defineProperty(exports, "__esModule", { value: true });
16const util_1 = require("./util");
17/**
18 * Match the text inside an element, textnode, or comment
19 *
20 * Note: nodeWalkAll with hasTextValue may return an textnode and its parent if
21 * the textnode is the only child in that parent.
22 */
23function hasTextValue(value) {
24 return function (node) {
25 return util_1.getTextContent(node) === value;
26 };
27}
28function 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}
42function 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 * negate an individual predicate, or a group with AND or OR
58 */
59function NOT(predicateFn) {
60 return function (node) {
61 return !predicateFn(node);
62 };
63}
64/**
65 * Returns a predicate that matches any node with a parent matching
66 * `predicateFn`.
67 */
68function 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}
80function hasAttr(attr) {
81 return function (node) {
82 return util_1.getAttributeIndex(node, attr) > -1;
83 };
84}
85function hasAttrValue(attr, value) {
86 return function (node) {
87 return util_1.getAttribute(node, attr) === value;
88 };
89}
90function hasClass(name) {
91 return hasSpaceSeparatedAttrValue('class', name);
92}
93function 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 * Returns true if `regex.match(tagName)` finds a match.
104 *
105 * This will use the lowercased tagName for comparison.
106 */
107function hasMatchingTagName(regex) {
108 return function (node) {
109 if (!node.tagName) {
110 return false;
111 }
112 return regex.test(node.tagName.toLowerCase());
113 };
114}
115function 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}
124exports.hasSpaceSeparatedAttrValue = hasSpaceSeparatedAttrValue;
125function isDocument(node) {
126 return node.nodeName === '#document';
127}
128exports.isDocument = isDocument;
129function isDocumentFragment(node) {
130 return node.nodeName === '#document-fragment';
131}
132exports.isDocumentFragment = isDocumentFragment;
133function isElement(node) {
134 return node.nodeName === node.tagName;
135}
136exports.isElement = isElement;
137function isTextNode(node) {
138 return node.nodeName === '#text';
139}
140exports.isTextNode = isTextNode;
141function isCommentNode(node) {
142 return node.nodeName === '#comment';
143}
144exports.isCommentNode = isCommentNode;
145exports.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//# sourceMappingURL=predicates.js.map
\No newline at end of file