htmljs-parser 5.0.4 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,7 +32,7 @@ export declare class Parser {
32
32
  lines: undefined | number[];
33
33
  constructor(options: Options);
34
34
  read(range: Range): string;
35
- positionAt(index: number): import("../internal").Position;
35
+ positionAt(offset: number): import("../internal").Position;
36
36
  locationAt(range: Range): import("../internal").Location;
37
37
  enterState<P extends Meta>(state: StateDefinition<P>): P;
38
38
  exitState(): void;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type ParserOptions, type Range } from "./internal";
2
- export { TagType, ErrorCode, type ParserOptions as Handlers, type Position, type Location, type Ranges, type Range, } from "./internal";
2
+ export { TagType, ErrorCode, getLines, getLocation, getPosition, type ParserOptions as Handlers, type Position, type Location, type Ranges, type Range, } from "./internal";
3
3
  /**
4
4
  * Creates a new Marko parser.
5
5
  */
package/dist/index.js CHANGED
@@ -21,7 +21,10 @@ var src_exports = {};
21
21
  __export(src_exports, {
22
22
  ErrorCode: () => ErrorCode,
23
23
  TagType: () => TagType,
24
- createParser: () => createParser
24
+ createParser: () => createParser,
25
+ getLines: () => getLines,
26
+ getLocation: () => getLocation,
27
+ getPosition: () => getPosition
25
28
  });
26
29
  module.exports = __toCommonJS(src_exports);
27
30
 
@@ -67,26 +70,13 @@ var TagType = /* @__PURE__ */ ((TagType2) => {
67
70
  function isWhitespaceCode(code) {
68
71
  return code <= 32 /* SPACE */;
69
72
  }
70
- function getLoc(lines, range) {
71
- const start = getPos(lines, 0, range.start);
72
- const end = range.start === range.end ? start : getPos(lines, start.line, range.end);
73
+ function getLocation(lines, startOffset, endOffset) {
74
+ const start = getPosition(lines, startOffset);
75
+ const end = startOffset === endOffset ? start : getPosAfterLine(lines, start.line, endOffset);
73
76
  return { start, end };
74
77
  }
75
- function getPos(lines, startLine, index) {
76
- let max = lines.length - 1;
77
- let line = startLine;
78
- while (line < max) {
79
- const mid = 1 + line + max >>> 1;
80
- if (lines[mid] <= index) {
81
- line = mid;
82
- } else {
83
- max = mid - 1;
84
- }
85
- }
86
- return {
87
- line,
88
- character: index - lines[line]
89
- };
78
+ function getPosition(lines, offset) {
79
+ return getPosAfterLine(lines, 0, offset);
90
80
  }
91
81
  function getLines(src) {
92
82
  const lines = [0];
@@ -107,6 +97,22 @@ function htmlEOF() {
107
97
  }
108
98
  }
109
99
  }
100
+ function getPosAfterLine(lines, startLine, index) {
101
+ let max = lines.length - 1;
102
+ let line = startLine;
103
+ while (line < max) {
104
+ const mid = 1 + line + max >>> 1;
105
+ if (lines[mid] <= index) {
106
+ line = mid;
107
+ } else {
108
+ max = mid - 1;
109
+ }
110
+ }
111
+ return {
112
+ line,
113
+ character: index - lines[line]
114
+ };
115
+ }
110
116
 
111
117
  // src/core/Parser.ts
112
118
  var Parser = class {
@@ -131,11 +137,11 @@ var Parser = class {
131
137
  read(range) {
132
138
  return this.data.slice(range.start, range.end);
133
139
  }
134
- positionAt(index) {
135
- return getPos(this.lines || (this.lines = getLines(this.data)), 0, index);
140
+ positionAt(offset) {
141
+ return getPosition(this.lines || (this.lines = getLines(this.data)), offset);
136
142
  }
137
143
  locationAt(range) {
138
- return getLoc(this.lines || (this.lines = getLines(this.data)), range);
144
+ return getLocation(this.lines || (this.lines = getLines(this.data)), range.start, range.end);
139
145
  }
140
146
  enterState(state) {
141
147
  this.activeState = state;
@@ -2119,5 +2125,8 @@ function createParser(handlers) {
2119
2125
  0 && (module.exports = {
2120
2126
  ErrorCode,
2121
2127
  TagType,
2122
- createParser
2128
+ createParser,
2129
+ getLines,
2130
+ getLocation,
2131
+ getPosition
2123
2132
  });
package/dist/index.mjs CHANGED
@@ -46,26 +46,13 @@ var TagType = /* @__PURE__ */ ((TagType2) => {
46
46
  function isWhitespaceCode(code) {
47
47
  return code <= 32 /* SPACE */;
48
48
  }
49
- function getLoc(lines, range) {
50
- const start = getPos(lines, 0, range.start);
51
- const end = range.start === range.end ? start : getPos(lines, start.line, range.end);
49
+ function getLocation(lines, startOffset, endOffset) {
50
+ const start = getPosition(lines, startOffset);
51
+ const end = startOffset === endOffset ? start : getPosAfterLine(lines, start.line, endOffset);
52
52
  return { start, end };
53
53
  }
54
- function getPos(lines, startLine, index) {
55
- let max = lines.length - 1;
56
- let line = startLine;
57
- while (line < max) {
58
- const mid = 1 + line + max >>> 1;
59
- if (lines[mid] <= index) {
60
- line = mid;
61
- } else {
62
- max = mid - 1;
63
- }
64
- }
65
- return {
66
- line,
67
- character: index - lines[line]
68
- };
54
+ function getPosition(lines, offset) {
55
+ return getPosAfterLine(lines, 0, offset);
69
56
  }
70
57
  function getLines(src) {
71
58
  const lines = [0];
@@ -86,6 +73,22 @@ function htmlEOF() {
86
73
  }
87
74
  }
88
75
  }
76
+ function getPosAfterLine(lines, startLine, index) {
77
+ let max = lines.length - 1;
78
+ let line = startLine;
79
+ while (line < max) {
80
+ const mid = 1 + line + max >>> 1;
81
+ if (lines[mid] <= index) {
82
+ line = mid;
83
+ } else {
84
+ max = mid - 1;
85
+ }
86
+ }
87
+ return {
88
+ line,
89
+ character: index - lines[line]
90
+ };
91
+ }
89
92
 
90
93
  // src/core/Parser.ts
91
94
  var Parser = class {
@@ -110,11 +113,11 @@ var Parser = class {
110
113
  read(range) {
111
114
  return this.data.slice(range.start, range.end);
112
115
  }
113
- positionAt(index) {
114
- return getPos(this.lines || (this.lines = getLines(this.data)), 0, index);
116
+ positionAt(offset) {
117
+ return getPosition(this.lines || (this.lines = getLines(this.data)), offset);
115
118
  }
116
119
  locationAt(range) {
117
- return getLoc(this.lines || (this.lines = getLines(this.data)), range);
120
+ return getLocation(this.lines || (this.lines = getLines(this.data)), range.start, range.end);
118
121
  }
119
122
  enterState(state) {
120
123
  this.activeState = state;
@@ -2097,5 +2100,8 @@ function createParser(handlers) {
2097
2100
  export {
2098
2101
  ErrorCode,
2099
2102
  TagType,
2100
- createParser
2103
+ createParser,
2104
+ getLines,
2105
+ getLocation,
2106
+ getPosition
2101
2107
  };
@@ -1,7 +1,17 @@
1
1
  import { type Parser } from "../internal";
2
- import { Location, Position, Range } from "./constants";
2
+ import { Location, Position } from "./constants";
3
3
  export declare function isWhitespaceCode(code: number): boolean;
4
- export declare function getLoc(lines: number[], range: Range): Location;
5
- export declare function getPos(lines: number[], startLine: number, index: number): Position;
4
+ /**
5
+ * Given a source code line offsets, a start offset and an end offset, returns a Location object with line & character information for the start and end offsets.
6
+ */
7
+ export declare function getLocation(lines: number[], startOffset: number, endOffset: number): Location;
8
+ /**
9
+ * Given a source code line offsets and an offset, returns a Position object with line & character information.
10
+ */
11
+ export declare function getPosition(lines: number[], offset: number): Position;
12
+ /**
13
+ * Scan through some source code and generate an array of offsets for each newline.
14
+ * Useful for generating line/column information for source code.
15
+ */
6
16
  export declare function getLines(src: string): number[];
7
17
  export declare function htmlEOF(this: Parser): void;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "htmljs-parser",
3
3
  "description": "An HTML parser recognizes content and string placeholders and allows JavaScript expressions as attribute values",
4
- "version": "5.0.4",
4
+ "version": "5.1.0",
5
5
  "devDependencies": {
6
6
  "@changesets/changelog-github": "^0.4.4",
7
7
  "@changesets/cli": "^2.22.0",