source: trip-planner-front/node_modules/domutils/lib/legacy.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.7 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getElementsByTagType = exports.getElementsByTagName = exports.getElementById = exports.getElements = exports.testElement = void 0;
4var domhandler_1 = require("domhandler");
5var querying_1 = require("./querying");
6var Checks = {
7 tag_name: function (name) {
8 if (typeof name === "function") {
9 return function (elem) { return (0, domhandler_1.isTag)(elem) && name(elem.name); };
10 }
11 else if (name === "*") {
12 return domhandler_1.isTag;
13 }
14 return function (elem) { return (0, domhandler_1.isTag)(elem) && elem.name === name; };
15 },
16 tag_type: function (type) {
17 if (typeof type === "function") {
18 return function (elem) { return type(elem.type); };
19 }
20 return function (elem) { return elem.type === type; };
21 },
22 tag_contains: function (data) {
23 if (typeof data === "function") {
24 return function (elem) { return (0, domhandler_1.isText)(elem) && data(elem.data); };
25 }
26 return function (elem) { return (0, domhandler_1.isText)(elem) && elem.data === data; };
27 },
28};
29/**
30 * @param attrib Attribute to check.
31 * @param value Attribute value to look for.
32 * @returns A function to check whether the a node has an attribute with a particular value.
33 */
34function getAttribCheck(attrib, value) {
35 if (typeof value === "function") {
36 return function (elem) { return (0, domhandler_1.isTag)(elem) && value(elem.attribs[attrib]); };
37 }
38 return function (elem) { return (0, domhandler_1.isTag)(elem) && elem.attribs[attrib] === value; };
39}
40/**
41 * @param a First function to combine.
42 * @param b Second function to combine.
43 * @returns A function taking a node and returning `true` if either
44 * of the input functions returns `true` for the node.
45 */
46function combineFuncs(a, b) {
47 return function (elem) { return a(elem) || b(elem); };
48}
49/**
50 * @param options An object describing nodes to look for.
51 * @returns A function executing all checks in `options` and returning `true`
52 * if any of them match a node.
53 */
54function compileTest(options) {
55 var funcs = Object.keys(options).map(function (key) {
56 var value = options[key];
57 return Object.prototype.hasOwnProperty.call(Checks, key)
58 ? Checks[key](value)
59 : getAttribCheck(key, value);
60 });
61 return funcs.length === 0 ? null : funcs.reduce(combineFuncs);
62}
63/**
64 * @param options An object describing nodes to look for.
65 * @param node The element to test.
66 * @returns Whether the element matches the description in `options`.
67 */
68function testElement(options, node) {
69 var test = compileTest(options);
70 return test ? test(node) : true;
71}
72exports.testElement = testElement;
73/**
74 * @param options An object describing nodes to look for.
75 * @param nodes Nodes to search through.
76 * @param recurse Also consider child nodes.
77 * @param limit Maximum number of nodes to return.
78 * @returns All nodes that match `options`.
79 */
80function getElements(options, nodes, recurse, limit) {
81 if (limit === void 0) { limit = Infinity; }
82 var test = compileTest(options);
83 return test ? (0, querying_1.filter)(test, nodes, recurse, limit) : [];
84}
85exports.getElements = getElements;
86/**
87 * @param id The unique ID attribute value to look for.
88 * @param nodes Nodes to search through.
89 * @param recurse Also consider child nodes.
90 * @returns The node with the supplied ID.
91 */
92function getElementById(id, nodes, recurse) {
93 if (recurse === void 0) { recurse = true; }
94 if (!Array.isArray(nodes))
95 nodes = [nodes];
96 return (0, querying_1.findOne)(getAttribCheck("id", id), nodes, recurse);
97}
98exports.getElementById = getElementById;
99/**
100 * @param tagName Tag name to search for.
101 * @param nodes Nodes to search through.
102 * @param recurse Also consider child nodes.
103 * @param limit Maximum number of nodes to return.
104 * @returns All nodes with the supplied `tagName`.
105 */
106function getElementsByTagName(tagName, nodes, recurse, limit) {
107 if (recurse === void 0) { recurse = true; }
108 if (limit === void 0) { limit = Infinity; }
109 return (0, querying_1.filter)(Checks.tag_name(tagName), nodes, recurse, limit);
110}
111exports.getElementsByTagName = getElementsByTagName;
112/**
113 * @param type Element type to look for.
114 * @param nodes Nodes to search through.
115 * @param recurse Also consider child nodes.
116 * @param limit Maximum number of nodes to return.
117 * @returns All nodes with the supplied `type`.
118 */
119function getElementsByTagType(type, nodes, recurse, limit) {
120 if (recurse === void 0) { recurse = true; }
121 if (limit === void 0) { limit = Infinity; }
122 return (0, querying_1.filter)(Checks.tag_type(type), nodes, recurse, limit);
123}
124exports.getElementsByTagType = getElementsByTagType;
Note: See TracBrowser for help on using the repository browser.