| "use strict"; |
| Object.defineProperty(exports, "__esModule", { value: true }); |
| exports.testElement = testElement; |
| exports.getElements = getElements; |
| exports.getElementById = getElementById; |
| exports.getElementsByTagName = getElementsByTagName; |
| exports.getElementsByClassName = getElementsByClassName; |
| exports.getElementsByTagType = getElementsByTagType; |
| var domhandler_1 = require("domhandler"); |
| var querying_js_1 = require("./querying.js"); |
| |
| |
| |
| var Checks = { |
| tag_name: function (name) { |
| if (typeof name === "function") { |
| return function (elem) { return (0, domhandler_1.isTag)(elem) && name(elem.name); }; |
| } |
| else if (name === "*") { |
| return domhandler_1.isTag; |
| } |
| return function (elem) { return (0, domhandler_1.isTag)(elem) && elem.name === name; }; |
| }, |
| tag_type: function (type) { |
| if (typeof type === "function") { |
| return function (elem) { return type(elem.type); }; |
| } |
| return function (elem) { return elem.type === type; }; |
| }, |
| tag_contains: function (data) { |
| if (typeof data === "function") { |
| return function (elem) { return (0, domhandler_1.isText)(elem) && data(elem.data); }; |
| } |
| return function (elem) { return (0, domhandler_1.isText)(elem) && elem.data === data; }; |
| }, |
| }; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function getAttribCheck(attrib, value) { |
| if (typeof value === "function") { |
| return function (elem) { return (0, domhandler_1.isTag)(elem) && value(elem.attribs[attrib]); }; |
| } |
| return function (elem) { return (0, domhandler_1.isTag)(elem) && elem.attribs[attrib] === value; }; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function combineFuncs(a, b) { |
| return function (elem) { return a(elem) || b(elem); }; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function compileTest(options) { |
| var funcs = Object.keys(options).map(function (key) { |
| var value = options[key]; |
| return Object.prototype.hasOwnProperty.call(Checks, key) |
| ? Checks[key](value) |
| : getAttribCheck(key, value); |
| }); |
| return funcs.length === 0 ? null : funcs.reduce(combineFuncs); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function testElement(options, node) { |
| var test = compileTest(options); |
| return test ? test(node) : true; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function getElements(options, nodes, recurse, limit) { |
| if (limit === void 0) { limit = Infinity; } |
| var test = compileTest(options); |
| return test ? (0, querying_js_1.filter)(test, nodes, recurse, limit) : []; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function getElementById(id, nodes, recurse) { |
| if (recurse === void 0) { recurse = true; } |
| if (!Array.isArray(nodes)) |
| nodes = [nodes]; |
| return (0, querying_js_1.findOne)(getAttribCheck("id", id), nodes, recurse); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function getElementsByTagName(tagName, nodes, recurse, limit) { |
| if (recurse === void 0) { recurse = true; } |
| if (limit === void 0) { limit = Infinity; } |
| return (0, querying_js_1.filter)(Checks["tag_name"](tagName), nodes, recurse, limit); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function getElementsByClassName(className, nodes, recurse, limit) { |
| if (recurse === void 0) { recurse = true; } |
| if (limit === void 0) { limit = Infinity; } |
| return (0, querying_js_1.filter)(getAttribCheck("class", className), nodes, recurse, limit); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function getElementsByTagType(type, nodes, recurse, limit) { |
| if (recurse === void 0) { recurse = true; } |
| if (limit === void 0) { limit = Infinity; } |
| return (0, querying_js_1.filter)(Checks["tag_type"](type), nodes, recurse, limit); |
| } |
| |