UNPKG

ayakashi

Version:

The next generation web scraping framework

119 lines (118 loc) 3.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tagWalk = exports.domWalk = void 0; const query_1 = require("./query"); function domWalk(env, query, scope) { let results = []; const limit = query.limit || Number.MAX_SAFE_INTEGER; const skip = query.skip || 0; let passCounter = 0; let addedCounter = 0; function _domWalk(node, callback) { if (callback(node) === false) return false; let currentNode = (node && node.firstChild) || null; while (currentNode != null) { if (_domWalk(currentNode, callback) === false) return false; currentNode = currentNode.nextSibling; } return true; } if (query.order === "desc") { _domWalk(scope, el => { if (el === scope) return; if (el && el.nodeName === "#text") return; if (!query.where) { results.push(el); } else { if (query_1.runQuery(env, query, el) === true) { results.push(el); } } }); results.reverse(); results = results.splice(skip, limit); } else { _domWalk(scope, el => { if (el === scope) return; if (el && el.nodeName === "#text") return; let hasMatch = false; if (!query.where) { hasMatch = true; } else { hasMatch = query_1.runQuery(env, query, el); } if (hasMatch === true) { passCounter += 1; if (passCounter > skip && addedCounter < limit) { results.push(el); addedCounter += 1; } } }); } return results; } exports.domWalk = domWalk; function tagWalk(env, query, scope) { const results = []; const limit = query.limit || Number.MAX_SAFE_INTEGER; const skip = query.skip || 0; let passCounter = 0; let addedCounter = 0; const tags = Array.from(scope.getElementsByTagName("*")); if (query.order === "desc") { tags.reverse().forEach(el => { if (el === scope) return; if (el && el.nodeName === "#text") return; let hasMatch = false; if (!query.where) { hasMatch = true; } else { hasMatch = query_1.runQuery(env, query, el); } if (hasMatch === true) { passCounter += 1; if (passCounter > skip && addedCounter < limit) { results.push(el); addedCounter += 1; } } }); } else { tags.forEach(el => { if (el === scope) return; if (el && el.nodeName === "#text") return; let hasMatch = false; if (!query.where) { hasMatch = true; } else { hasMatch = query_1.runQuery(env, query, el); } if (hasMatch === true) { passCounter += 1; if (passCounter > skip && addedCounter < limit) { results.push(el); addedCounter += 1; } } }); } return results; } exports.tagWalk = tagWalk;