htmljs-parser 5.8.1 → 5.9.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.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { type ParserOptions, type Range } from "./internal";
2
2
  export { TagType, ErrorCode, getLines, getLocation, getPosition, type ParserOptions as Handlers, type Position, type Location, type Ranges, type Range, } from "./internal";
3
- export { isValidStatement, isValidAttrValue } from "./util/validators";
3
+ export { isValidStatement, isValidAttrValue, Validity, } from "./util/validators";
4
4
  /**
5
5
  * Creates a new Marko parser.
6
6
  */
package/dist/index.js CHANGED
@@ -22,6 +22,7 @@ var src_exports = {};
22
22
  __export(src_exports, {
23
23
  ErrorCode: () => ErrorCode,
24
24
  TagType: () => TagType,
25
+ Validity: () => Validity,
25
26
  createParser: () => createParser,
26
27
  getLines: () => getLines,
27
28
  getLocation: () => getLocation,
@@ -97,7 +98,18 @@ function getLines(src) {
97
98
  return lines;
98
99
  }
99
100
  function htmlEOF() {
100
- this.endText();
101
+ if (!this.activeTag || this.activeTag.concise) {
102
+ const pos = this.pos;
103
+ let cur = this.pos;
104
+ while (cur && isWhitespaceCode(this.data.charCodeAt(cur - 1))) {
105
+ cur--;
106
+ }
107
+ this.pos = cur;
108
+ this.endText();
109
+ this.pos = pos;
110
+ } else {
111
+ this.endText();
112
+ }
101
113
  while (this.activeTag) {
102
114
  if (this.activeTag.concise) {
103
115
  this.closeTagEnd(this.pos, this.pos, void 0);
@@ -238,7 +250,9 @@ var Parser = class {
238
250
  var _a, _b;
239
251
  const start = this.textPos;
240
252
  if (start !== -1) {
241
- (_b = (_a = this.options).onText) == null ? void 0 : _b.call(_a, { start, end: this.pos });
253
+ if (start !== this.pos) {
254
+ (_b = (_a = this.options).onText) == null ? void 0 : _b.call(_a, { start, end: this.pos });
255
+ }
242
256
  this.textPos = -1;
243
257
  }
244
258
  }
@@ -1178,7 +1192,7 @@ var BEGIN_DELIMITED_HTML_BLOCK = {
1178
1192
  },
1179
1193
  eol(len, block) {
1180
1194
  this.beginHtmlBlock(block.delimiter, false);
1181
- handleDelimitedBlockEOL(this, len, block);
1195
+ handleDelimitedBlockEOL(this, true, len, block);
1182
1196
  },
1183
1197
  eof: htmlEOF,
1184
1198
  return() {
@@ -1192,21 +1206,20 @@ function handleDelimitedEOL(parser, newLineLength, content) {
1192
1206
  return true;
1193
1207
  }
1194
1208
  if (content.delimiter) {
1195
- handleDelimitedBlockEOL(parser, newLineLength, content);
1209
+ handleDelimitedBlockEOL(parser, false, newLineLength, content);
1196
1210
  return true;
1197
1211
  }
1198
1212
  return false;
1199
1213
  }
1200
- function handleDelimitedBlockEOL(parser, newLineLength, {
1214
+ function handleDelimitedBlockEOL(parser, first, newLineLength, {
1201
1215
  indent,
1202
1216
  delimiter
1203
1217
  }) {
1204
1218
  const endHtmlBlockLookahead = indent + delimiter;
1205
1219
  if (parser.lookAheadFor(endHtmlBlockLookahead, parser.pos + newLineLength)) {
1206
- parser.startText();
1207
- parser.pos += newLineLength;
1208
1220
  parser.endText();
1209
- parser.pos += endHtmlBlockLookahead.length;
1221
+ parser.pos += newLineLength + endHtmlBlockLookahead.length;
1222
+ parser.forward = 0;
1210
1223
  if (parser.consumeWhitespaceOnLine(0)) {
1211
1224
  parser.exitState();
1212
1225
  parser.exitState();
@@ -1218,14 +1231,24 @@ function handleDelimitedBlockEOL(parser, newLineLength, {
1218
1231
  );
1219
1232
  }
1220
1233
  } else if (parser.lookAheadFor(indent, parser.pos + newLineLength)) {
1221
- parser.startText();
1234
+ if (!first)
1235
+ parser.startText();
1236
+ parser.pos += newLineLength;
1237
+ parser.endText();
1222
1238
  parser.pos += indent.length;
1239
+ parser.forward = 0;
1240
+ parser.startText();
1223
1241
  } else if (indent && !parser.onlyWhitespaceRemainsOnLine(newLineLength)) {
1242
+ const pos = parser.pos;
1243
+ let cur = parser.pos;
1244
+ while (cur && isWhitespaceCode(parser.data.charCodeAt(cur - 1))) {
1245
+ cur--;
1246
+ }
1247
+ parser.pos = cur;
1224
1248
  parser.endText();
1249
+ parser.pos = pos;
1225
1250
  parser.exitState();
1226
1251
  parser.exitState();
1227
- } else if (parser.pos + newLineLength !== parser.maxPos) {
1228
- parser.startText();
1229
1252
  }
1230
1253
  }
1231
1254
 
@@ -2862,6 +2885,12 @@ var ROOT_RANGE = {
2862
2885
  start: 0,
2863
2886
  end: 0
2864
2887
  };
2888
+ var Validity = /* @__PURE__ */ ((Validity2) => {
2889
+ Validity2[Validity2["invalid"] = 0] = "invalid";
2890
+ Validity2[Validity2["valid"] = 1] = "valid";
2891
+ Validity2[Validity2["enclosed"] = 2] = "enclosed";
2892
+ return Validity2;
2893
+ })(Validity || {});
2865
2894
  function isValidStatement(code) {
2866
2895
  return isValid(code, true, prepareStatement);
2867
2896
  }
@@ -2892,13 +2921,18 @@ function isValid(data, concise, prepare) {
2892
2921
  parser.activeState = ROOT_STATE;
2893
2922
  parser.activeRange = ROOT_RANGE;
2894
2923
  const expr = parser.enterState(states_exports.EXPRESSION);
2924
+ let isEnclosed = true;
2895
2925
  prepare(expr, concise);
2896
2926
  while (parser.pos < maxPos) {
2897
2927
  const code = data.charCodeAt(parser.pos);
2898
2928
  if (code === 10 /* NEWLINE */) {
2929
+ if (isEnclosed && !expr.groupStack.length)
2930
+ isEnclosed = false;
2899
2931
  parser.forward = 1;
2900
2932
  parser.activeState.eol.call(parser, 1, parser.activeRange);
2901
2933
  } else if (code === 13 /* CARRIAGE_RETURN */ && data.charCodeAt(parser.pos + 1) === 10 /* NEWLINE */) {
2934
+ if (isEnclosed && !expr.groupStack.length)
2935
+ isEnclosed = false;
2902
2936
  parser.forward = 2;
2903
2937
  parser.activeState.eol.call(parser, 2, parser.activeRange);
2904
2938
  } else {
@@ -2906,11 +2940,14 @@ function isValid(data, concise, prepare) {
2906
2940
  parser.activeState.char.call(parser, code, parser.activeRange);
2907
2941
  }
2908
2942
  if (parser.activeRange === ROOT_RANGE) {
2909
- return false;
2943
+ return 0 /* invalid */;
2910
2944
  }
2911
2945
  parser.pos += parser.forward;
2912
2946
  }
2913
- return parser.pos === maxPos && parser.activeRange === expr && !expr.groupStack.length;
2947
+ if (parser.pos === maxPos && parser.activeRange === expr && !expr.groupStack.length) {
2948
+ return isEnclosed ? 2 /* enclosed */ : 1 /* valid */;
2949
+ }
2950
+ return 0 /* invalid */;
2914
2951
  }
2915
2952
 
2916
2953
  // src/index.ts
@@ -2947,6 +2984,7 @@ function createParser(handlers) {
2947
2984
  0 && (module.exports = {
2948
2985
  ErrorCode,
2949
2986
  TagType,
2987
+ Validity,
2950
2988
  createParser,
2951
2989
  getLines,
2952
2990
  getLocation,
package/dist/index.mjs CHANGED
@@ -70,7 +70,18 @@ function getLines(src) {
70
70
  return lines;
71
71
  }
72
72
  function htmlEOF() {
73
- this.endText();
73
+ if (!this.activeTag || this.activeTag.concise) {
74
+ const pos = this.pos;
75
+ let cur = this.pos;
76
+ while (cur && isWhitespaceCode(this.data.charCodeAt(cur - 1))) {
77
+ cur--;
78
+ }
79
+ this.pos = cur;
80
+ this.endText();
81
+ this.pos = pos;
82
+ } else {
83
+ this.endText();
84
+ }
74
85
  while (this.activeTag) {
75
86
  if (this.activeTag.concise) {
76
87
  this.closeTagEnd(this.pos, this.pos, void 0);
@@ -211,7 +222,9 @@ var Parser = class {
211
222
  var _a, _b;
212
223
  const start = this.textPos;
213
224
  if (start !== -1) {
214
- (_b = (_a = this.options).onText) == null ? void 0 : _b.call(_a, { start, end: this.pos });
225
+ if (start !== this.pos) {
226
+ (_b = (_a = this.options).onText) == null ? void 0 : _b.call(_a, { start, end: this.pos });
227
+ }
215
228
  this.textPos = -1;
216
229
  }
217
230
  }
@@ -1151,7 +1164,7 @@ var BEGIN_DELIMITED_HTML_BLOCK = {
1151
1164
  },
1152
1165
  eol(len, block) {
1153
1166
  this.beginHtmlBlock(block.delimiter, false);
1154
- handleDelimitedBlockEOL(this, len, block);
1167
+ handleDelimitedBlockEOL(this, true, len, block);
1155
1168
  },
1156
1169
  eof: htmlEOF,
1157
1170
  return() {
@@ -1165,21 +1178,20 @@ function handleDelimitedEOL(parser, newLineLength, content) {
1165
1178
  return true;
1166
1179
  }
1167
1180
  if (content.delimiter) {
1168
- handleDelimitedBlockEOL(parser, newLineLength, content);
1181
+ handleDelimitedBlockEOL(parser, false, newLineLength, content);
1169
1182
  return true;
1170
1183
  }
1171
1184
  return false;
1172
1185
  }
1173
- function handleDelimitedBlockEOL(parser, newLineLength, {
1186
+ function handleDelimitedBlockEOL(parser, first, newLineLength, {
1174
1187
  indent,
1175
1188
  delimiter
1176
1189
  }) {
1177
1190
  const endHtmlBlockLookahead = indent + delimiter;
1178
1191
  if (parser.lookAheadFor(endHtmlBlockLookahead, parser.pos + newLineLength)) {
1179
- parser.startText();
1180
- parser.pos += newLineLength;
1181
1192
  parser.endText();
1182
- parser.pos += endHtmlBlockLookahead.length;
1193
+ parser.pos += newLineLength + endHtmlBlockLookahead.length;
1194
+ parser.forward = 0;
1183
1195
  if (parser.consumeWhitespaceOnLine(0)) {
1184
1196
  parser.exitState();
1185
1197
  parser.exitState();
@@ -1191,14 +1203,24 @@ function handleDelimitedBlockEOL(parser, newLineLength, {
1191
1203
  );
1192
1204
  }
1193
1205
  } else if (parser.lookAheadFor(indent, parser.pos + newLineLength)) {
1194
- parser.startText();
1206
+ if (!first)
1207
+ parser.startText();
1208
+ parser.pos += newLineLength;
1209
+ parser.endText();
1195
1210
  parser.pos += indent.length;
1211
+ parser.forward = 0;
1212
+ parser.startText();
1196
1213
  } else if (indent && !parser.onlyWhitespaceRemainsOnLine(newLineLength)) {
1214
+ const pos = parser.pos;
1215
+ let cur = parser.pos;
1216
+ while (cur && isWhitespaceCode(parser.data.charCodeAt(cur - 1))) {
1217
+ cur--;
1218
+ }
1219
+ parser.pos = cur;
1197
1220
  parser.endText();
1221
+ parser.pos = pos;
1198
1222
  parser.exitState();
1199
1223
  parser.exitState();
1200
- } else if (parser.pos + newLineLength !== parser.maxPos) {
1201
- parser.startText();
1202
1224
  }
1203
1225
  }
1204
1226
 
@@ -2835,6 +2857,12 @@ var ROOT_RANGE = {
2835
2857
  start: 0,
2836
2858
  end: 0
2837
2859
  };
2860
+ var Validity = /* @__PURE__ */ ((Validity2) => {
2861
+ Validity2[Validity2["invalid"] = 0] = "invalid";
2862
+ Validity2[Validity2["valid"] = 1] = "valid";
2863
+ Validity2[Validity2["enclosed"] = 2] = "enclosed";
2864
+ return Validity2;
2865
+ })(Validity || {});
2838
2866
  function isValidStatement(code) {
2839
2867
  return isValid(code, true, prepareStatement);
2840
2868
  }
@@ -2865,13 +2893,18 @@ function isValid(data, concise, prepare) {
2865
2893
  parser.activeState = ROOT_STATE;
2866
2894
  parser.activeRange = ROOT_RANGE;
2867
2895
  const expr = parser.enterState(states_exports.EXPRESSION);
2896
+ let isEnclosed = true;
2868
2897
  prepare(expr, concise);
2869
2898
  while (parser.pos < maxPos) {
2870
2899
  const code = data.charCodeAt(parser.pos);
2871
2900
  if (code === 10 /* NEWLINE */) {
2901
+ if (isEnclosed && !expr.groupStack.length)
2902
+ isEnclosed = false;
2872
2903
  parser.forward = 1;
2873
2904
  parser.activeState.eol.call(parser, 1, parser.activeRange);
2874
2905
  } else if (code === 13 /* CARRIAGE_RETURN */ && data.charCodeAt(parser.pos + 1) === 10 /* NEWLINE */) {
2906
+ if (isEnclosed && !expr.groupStack.length)
2907
+ isEnclosed = false;
2875
2908
  parser.forward = 2;
2876
2909
  parser.activeState.eol.call(parser, 2, parser.activeRange);
2877
2910
  } else {
@@ -2879,11 +2912,14 @@ function isValid(data, concise, prepare) {
2879
2912
  parser.activeState.char.call(parser, code, parser.activeRange);
2880
2913
  }
2881
2914
  if (parser.activeRange === ROOT_RANGE) {
2882
- return false;
2915
+ return 0 /* invalid */;
2883
2916
  }
2884
2917
  parser.pos += parser.forward;
2885
2918
  }
2886
- return parser.pos === maxPos && parser.activeRange === expr && !expr.groupStack.length;
2919
+ if (parser.pos === maxPos && parser.activeRange === expr && !expr.groupStack.length) {
2920
+ return isEnclosed ? 2 /* enclosed */ : 1 /* valid */;
2921
+ }
2922
+ return 0 /* invalid */;
2887
2923
  }
2888
2924
 
2889
2925
  // src/index.ts
@@ -2919,6 +2955,7 @@ function createParser(handlers) {
2919
2955
  export {
2920
2956
  ErrorCode,
2921
2957
  TagType,
2958
+ Validity,
2922
2959
  createParser,
2923
2960
  getLines,
2924
2961
  getLocation,
@@ -1,2 +1,7 @@
1
- export declare function isValidStatement(code: string): boolean;
2
- export declare function isValidAttrValue(code: string, concise: boolean): boolean;
1
+ export declare enum Validity {
2
+ invalid = 0,
3
+ valid = 1,
4
+ enclosed = 2
5
+ }
6
+ export declare function isValidStatement(code: string): Validity;
7
+ export declare function isValidAttrValue(code: string, concise: boolean): Validity;
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.8.1",
4
+ "version": "5.9.0",
5
5
  "devDependencies": {
6
6
  "@changesets/changelog-github": "^0.5.0",
7
7
  "@changesets/cli": "^2.27.1",