forked from DanielJDufour/xml-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-tags-by-path.js
33 lines (31 loc) · 1.18 KB
/
find-tags-by-path.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const findTagsByName = require("./find-tags-by-name.js");
function findTagsByPath(xml, path, options) {
const debug = (options && options.debug) || false;
const returnOnFirst = (options && options.returnOnFirst) || false;
let tags = findTagsByName(xml, path.shift(), { debug, nested: false });
if (debug) console.log("first tags are:", tags);
for (let pathIndex = 0; pathIndex < path.length; pathIndex++) {
const tagName = path[pathIndex];
if (debug) console.log("tagName:", tagName);
let allSubTags = [];
for (let tagIndex = 0; tagIndex < tags.length; tagIndex++) {
const tag = tags[tagIndex];
const subTags = findTagsByName(tag.outer, tagName, {
debug,
startIndex: 1
});
if (debug) console.log("subTags.length:", subTags.length);
if (subTags.length > 0) {
subTags.forEach(subTag => {
(subTag.start += tag.start), (subTag.end += tag.start);
});
if (returnOnFirst && pathIndex === path.length - 1) return [subTags[0]];
allSubTags = allSubTags.concat(subTags);
}
}
tags = allSubTags;
}
return tags;
}
module.exports = findTagsByPath;
module.exports.default = findTagsByPath;