-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.ts
45 lines (41 loc) · 989 Bytes
/
location.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
interface CheerioElement {
startIndex: number;
}
interface SourcePosition {
line: number;
col: number;
}
/**
* @param {string} html
* @param node
* @return {Pos}
*/
function getLine(html: string, node: CheerioElement): SourcePosition {
if (!node) {
return {line: 1, col: 1};
}
const linesUntil = html.substring(0, node.startIndex).split("\n");
return {line: linesUntil.length, col: linesUntil[linesUntil.length - 1].length + 1};
}
/**
* @param context
* @param node
* @return {{pos:Pos, start:number, end:number}}
*/
function getNodeLoc(context, node) {
const pos = getLine(context.html, node);
let end;
if (node.data) {
end = node.startIndex + node.data.length;
} else if (node.next) { // eslint-disable-line
end = node.next.startIndex;
} else {
end = context.html.length;
}
return {
pos: pos,
start: node.startIndex,
end: end
};
}
export { getLine, getNodeLoc };