1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | function 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 |
|
21 | function 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 | }
|
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 |
|
46 | class HtmlSourceError extends Error {
|
47 | constructor(error, startIndex, endIndex, source) {
|
48 | super(error);
|
49 | this.name = 'HtmlSourceError';
|
50 | this.message = `${this.name}: ${this.message}`;
|
51 | this.startIndex = startIndex;
|
52 | this.endIndex = endIndex;
|
53 | this.source = source;
|
54 | const startPosition = offsetToPosition(source, this.startIndex);
|
55 | const endPosition = offsetToPosition(source, this.endIndex);
|
56 | this.message += ` (From line ${startPosition.line}, column ${startPosition.column}; to line ${endPosition.line}, column ${endPosition.column})`;
|
57 |
|
58 | this.stack = false;
|
59 | }
|
60 |
|
61 | }
|
62 |
|
63 | exports.default = HtmlSourceError; |
\ | No newline at end of file |