htmljs-parser 5.8.2 → 5.10.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, Validity, } from "./util/validators";
3
+ export { isValidStatement, isValidScriptlet, isValidAttrValue, Validity, } from "./util/validators";
4
4
  /**
5
5
  * Creates a new Marko parser.
6
6
  */
package/dist/index.js CHANGED
@@ -28,6 +28,7 @@ __export(src_exports, {
28
28
  getLocation: () => getLocation,
29
29
  getPosition: () => getPosition,
30
30
  isValidAttrValue: () => isValidAttrValue,
31
+ isValidScriptlet: () => isValidScriptlet,
31
32
  isValidStatement: () => isValidStatement
32
33
  });
33
34
  module.exports = __toCommonJS(src_exports);
@@ -98,7 +99,18 @@ function getLines(src) {
98
99
  return lines;
99
100
  }
100
101
  function htmlEOF() {
101
- this.endText();
102
+ if (!this.activeTag || this.activeTag.concise) {
103
+ const pos = this.pos;
104
+ let cur = this.pos;
105
+ while (cur && isWhitespaceCode(this.data.charCodeAt(cur - 1))) {
106
+ cur--;
107
+ }
108
+ this.pos = cur;
109
+ this.endText();
110
+ this.pos = pos;
111
+ } else {
112
+ this.endText();
113
+ }
102
114
  while (this.activeTag) {
103
115
  if (this.activeTag.concise) {
104
116
  this.closeTagEnd(this.pos, this.pos, void 0);
@@ -239,7 +251,9 @@ var Parser = class {
239
251
  var _a, _b;
240
252
  const start = this.textPos;
241
253
  if (start !== -1) {
242
- (_b = (_a = this.options).onText) == null ? void 0 : _b.call(_a, { start, end: this.pos });
254
+ if (start !== this.pos) {
255
+ (_b = (_a = this.options).onText) == null ? void 0 : _b.call(_a, { start, end: this.pos });
256
+ }
243
257
  this.textPos = -1;
244
258
  }
245
259
  }
@@ -1179,7 +1193,7 @@ var BEGIN_DELIMITED_HTML_BLOCK = {
1179
1193
  },
1180
1194
  eol(len, block) {
1181
1195
  this.beginHtmlBlock(block.delimiter, false);
1182
- handleDelimitedBlockEOL(this, len, block);
1196
+ handleDelimitedBlockEOL(this, true, len, block);
1183
1197
  },
1184
1198
  eof: htmlEOF,
1185
1199
  return() {
@@ -1193,21 +1207,20 @@ function handleDelimitedEOL(parser, newLineLength, content) {
1193
1207
  return true;
1194
1208
  }
1195
1209
  if (content.delimiter) {
1196
- handleDelimitedBlockEOL(parser, newLineLength, content);
1210
+ handleDelimitedBlockEOL(parser, false, newLineLength, content);
1197
1211
  return true;
1198
1212
  }
1199
1213
  return false;
1200
1214
  }
1201
- function handleDelimitedBlockEOL(parser, newLineLength, {
1215
+ function handleDelimitedBlockEOL(parser, first, newLineLength, {
1202
1216
  indent,
1203
1217
  delimiter
1204
1218
  }) {
1205
1219
  const endHtmlBlockLookahead = indent + delimiter;
1206
1220
  if (parser.lookAheadFor(endHtmlBlockLookahead, parser.pos + newLineLength)) {
1207
- parser.startText();
1208
- parser.pos += newLineLength;
1209
1221
  parser.endText();
1210
- parser.pos += endHtmlBlockLookahead.length;
1222
+ parser.pos += newLineLength + endHtmlBlockLookahead.length;
1223
+ parser.forward = 0;
1211
1224
  if (parser.consumeWhitespaceOnLine(0)) {
1212
1225
  parser.exitState();
1213
1226
  parser.exitState();
@@ -1219,14 +1232,24 @@ function handleDelimitedBlockEOL(parser, newLineLength, {
1219
1232
  );
1220
1233
  }
1221
1234
  } else if (parser.lookAheadFor(indent, parser.pos + newLineLength)) {
1222
- parser.startText();
1235
+ if (!first)
1236
+ parser.startText();
1237
+ parser.pos += newLineLength;
1238
+ parser.endText();
1223
1239
  parser.pos += indent.length;
1240
+ parser.forward = 0;
1241
+ parser.startText();
1224
1242
  } else if (indent && !parser.onlyWhitespaceRemainsOnLine(newLineLength)) {
1243
+ const pos = parser.pos;
1244
+ let cur = parser.pos;
1245
+ while (cur && isWhitespaceCode(parser.data.charCodeAt(cur - 1))) {
1246
+ cur--;
1247
+ }
1248
+ parser.pos = cur;
1225
1249
  parser.endText();
1250
+ parser.pos = pos;
1226
1251
  parser.exitState();
1227
1252
  parser.exitState();
1228
- } else if (parser.pos + newLineLength !== parser.maxPos) {
1229
- parser.startText();
1230
1253
  }
1231
1254
  }
1232
1255
 
@@ -2877,6 +2900,13 @@ function prepareStatement(expr) {
2877
2900
  expr.terminatedByEOL = true;
2878
2901
  expr.consumeIndentedContent = true;
2879
2902
  }
2903
+ function isValidScriptlet(code) {
2904
+ return isValid(code, true, prepareScriptlet);
2905
+ }
2906
+ function prepareScriptlet(expr) {
2907
+ expr.operators = true;
2908
+ expr.terminatedByEOL = true;
2909
+ }
2880
2910
  function isValidAttrValue(code, concise) {
2881
2911
  return isValid(code, concise, prepareAttrValue);
2882
2912
  }
@@ -2968,5 +2998,6 @@ function createParser(handlers) {
2968
2998
  getLocation,
2969
2999
  getPosition,
2970
3000
  isValidAttrValue,
3001
+ isValidScriptlet,
2971
3002
  isValidStatement
2972
3003
  });
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
 
@@ -2849,6 +2871,13 @@ function prepareStatement(expr) {
2849
2871
  expr.terminatedByEOL = true;
2850
2872
  expr.consumeIndentedContent = true;
2851
2873
  }
2874
+ function isValidScriptlet(code) {
2875
+ return isValid(code, true, prepareScriptlet);
2876
+ }
2877
+ function prepareScriptlet(expr) {
2878
+ expr.operators = true;
2879
+ expr.terminatedByEOL = true;
2880
+ }
2852
2881
  function isValidAttrValue(code, concise) {
2853
2882
  return isValid(code, concise, prepareAttrValue);
2854
2883
  }
@@ -2939,5 +2968,6 @@ export {
2939
2968
  getLocation,
2940
2969
  getPosition,
2941
2970
  isValidAttrValue,
2971
+ isValidScriptlet,
2942
2972
  isValidStatement
2943
2973
  };
@@ -4,4 +4,5 @@ export declare enum Validity {
4
4
  enclosed = 2
5
5
  }
6
6
  export declare function isValidStatement(code: string): Validity;
7
+ export declare function isValidScriptlet(code: string): Validity;
7
8
  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.2",
4
+ "version": "5.10.0",
5
5
  "devDependencies": {
6
6
  "@changesets/changelog-github": "^0.5.0",
7
7
  "@changesets/cli": "^2.27.1",