UNPKG

1.44 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8function getIndices(value) {
9 const result = [];
10 let index = value.indexOf("\n");
11
12 while (index !== -1) {
13 result.push(index + 1);
14 index = value.indexOf("\n", index + 1);
15 }
16
17 result.push(value.length + 1);
18 return result;
19}
20
21function offsetToPosition(source, offset) {
22 let index = -1;
23 const indices = getIndices(source);
24 const {
25 length
26 } = indices;
27
28 if (offset < 0) {
29 return {};
30 } // eslint-disable-next-line no-plusplus
31
32
33 while (++index < length) {
34 if (indices[index] > offset) {
35 return {
36 line: index + 1,
37 column: offset - (indices[index - 1] || 0) + 1,
38 offset
39 };
40 }
41 }
42
43 return {};
44}
45
46class HtmlSourceError extends Error {
47 constructor(error, startOffset, endOffset, source) {
48 super(error);
49 this.name = "HtmlSourceError";
50 this.message = `${this.name}: ${this.message}`;
51 this.startOffset = startOffset;
52 this.endOffset = endOffset;
53 this.source = source;
54 const startPosition = offsetToPosition(source, this.startOffset);
55 const endPosition = offsetToPosition(source, this.endOffset);
56 this.message += ` (From line ${startPosition.line}, column ${startPosition.column}; to line ${endPosition.line}, column ${endPosition.column})`; // We don't need stack
57
58 this.stack = false;
59 }
60
61}
62
63exports.default = HtmlSourceError;
\No newline at end of file