htmljs-parser 3.2.1 → 3.3.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.mjs CHANGED
@@ -5,6 +5,35 @@ var __export = (target, all) => {
5
5
  };
6
6
 
7
7
  // src/util/constants.ts
8
+ var ErrorCode = /* @__PURE__ */ ((ErrorCode3) => {
9
+ ErrorCode3[ErrorCode3["EXTRA_CLOSING_TAG"] = 0] = "EXTRA_CLOSING_TAG";
10
+ ErrorCode3[ErrorCode3["INVALID_ATTRIBUTE_ARGUMENT"] = 1] = "INVALID_ATTRIBUTE_ARGUMENT";
11
+ ErrorCode3[ErrorCode3["INVALID_ATTRIBUTE_NAME"] = 2] = "INVALID_ATTRIBUTE_NAME";
12
+ ErrorCode3[ErrorCode3["INVALID_ATTRIBUTE_VALUE"] = 3] = "INVALID_ATTRIBUTE_VALUE";
13
+ ErrorCode3[ErrorCode3["INVALID_CHARACTER"] = 4] = "INVALID_CHARACTER";
14
+ ErrorCode3[ErrorCode3["INVALID_CODE_AFTER_SEMICOLON"] = 5] = "INVALID_CODE_AFTER_SEMICOLON";
15
+ ErrorCode3[ErrorCode3["INVALID_EXPRESSION"] = 6] = "INVALID_EXPRESSION";
16
+ ErrorCode3[ErrorCode3["INVALID_INDENTATION"] = 7] = "INVALID_INDENTATION";
17
+ ErrorCode3[ErrorCode3["INVALID_LINE_START"] = 8] = "INVALID_LINE_START";
18
+ ErrorCode3[ErrorCode3["INVALID_REGULAR_EXPRESSION"] = 9] = "INVALID_REGULAR_EXPRESSION";
19
+ ErrorCode3[ErrorCode3["INVALID_STRING"] = 10] = "INVALID_STRING";
20
+ ErrorCode3[ErrorCode3["INVALID_TAG_ARGUMENT"] = 11] = "INVALID_TAG_ARGUMENT";
21
+ ErrorCode3[ErrorCode3["INVALID_TAG_SHORTHAND"] = 12] = "INVALID_TAG_SHORTHAND";
22
+ ErrorCode3[ErrorCode3["INVALID_TEMPLATE_STRING"] = 13] = "INVALID_TEMPLATE_STRING";
23
+ ErrorCode3[ErrorCode3["MALFORMED_CDATA"] = 14] = "MALFORMED_CDATA";
24
+ ErrorCode3[ErrorCode3["MALFORMED_CLOSE_TAG"] = 15] = "MALFORMED_CLOSE_TAG";
25
+ ErrorCode3[ErrorCode3["MALFORMED_COMMENT"] = 16] = "MALFORMED_COMMENT";
26
+ ErrorCode3[ErrorCode3["MALFORMED_DECLARATION"] = 17] = "MALFORMED_DECLARATION";
27
+ ErrorCode3[ErrorCode3["MALFORMED_DOCUMENT_TYPE"] = 18] = "MALFORMED_DOCUMENT_TYPE";
28
+ ErrorCode3[ErrorCode3["MALFORMED_OPEN_TAG"] = 19] = "MALFORMED_OPEN_TAG";
29
+ ErrorCode3[ErrorCode3["MALFORMED_PLACEHOLDER"] = 20] = "MALFORMED_PLACEHOLDER";
30
+ ErrorCode3[ErrorCode3["MISMATCHED_CLOSING_TAG"] = 21] = "MISMATCHED_CLOSING_TAG";
31
+ ErrorCode3[ErrorCode3["MISSING_END_TAG"] = 22] = "MISSING_END_TAG";
32
+ ErrorCode3[ErrorCode3["MISSING_TAG_VARIABLE"] = 23] = "MISSING_TAG_VARIABLE";
33
+ ErrorCode3[ErrorCode3["RESERVED_TAG_NAME"] = 24] = "RESERVED_TAG_NAME";
34
+ ErrorCode3[ErrorCode3["ROOT_TAG_ONLY"] = 25] = "ROOT_TAG_ONLY";
35
+ return ErrorCode3;
36
+ })(ErrorCode || {});
8
37
  var TagType = /* @__PURE__ */ ((TagType2) => {
9
38
  TagType2[TagType2["html"] = 0] = "html";
10
39
  TagType2[TagType2["text"] = 1] = "text";
@@ -53,7 +82,7 @@ function htmlEOF() {
53
82
  if (this.activeTag.concise) {
54
83
  this.closeTag(this.pos, this.pos, void 0);
55
84
  } else {
56
- return this.emitError(this.activeTag, "MISSING_END_TAG", 'Missing ending "' + this.read(this.activeTag.tagName) + '" tag');
85
+ return this.emitError(this.activeTag, 22 /* MISSING_END_TAG */, 'Missing ending "' + this.read(this.activeTag.tagName) + '" tag');
57
86
  }
58
87
  }
59
88
  }
@@ -423,7 +452,7 @@ var ATTRIBUTE = {
423
452
  if (this.isConcise) {
424
453
  this.exitState();
425
454
  } else {
426
- this.emitError(attr, "MALFORMED_OPEN_TAG", 'EOF reached while parsing attribute "' + (attr.name ? this.read(attr.name) : "default") + '" for the "' + this.read(this.activeTag.tagName) + '" tag');
455
+ this.emitError(attr, 19 /* MALFORMED_OPEN_TAG */, 'EOF reached while parsing attribute "' + (attr.name ? this.read(attr.name) : "default") + '" for the "' + this.read(this.activeTag.tagName) + '" tag');
427
456
  }
428
457
  },
429
458
  return(child, attr) {
@@ -439,7 +468,7 @@ var ATTRIBUTE = {
439
468
  }
440
469
  case 3 /* ARGUMENT */: {
441
470
  if (attr.args) {
442
- this.emitError(child, "ILLEGAL_ATTRIBUTE_ARGUMENT", "An attribute can only have one set of arguments");
471
+ this.emitError(child, 1 /* INVALID_ATTRIBUTE_ARGUMENT */, "An attribute can only have one set of arguments");
443
472
  return;
444
473
  }
445
474
  const start = child.start - 1;
@@ -486,7 +515,7 @@ var ATTRIBUTE = {
486
515
  }
487
516
  case 2 /* VALUE */: {
488
517
  if (child.start === child.end) {
489
- return this.emitError(child, "ILLEGAL_ATTRIBUTE_VALUE", "Missing value for attribute");
518
+ return this.emitError(child, 3 /* INVALID_ATTRIBUTE_VALUE */, "Missing value for attribute");
490
519
  }
491
520
  if (attr.spread) {
492
521
  (_h = (_g = this.options).onAttrSpread) == null ? void 0 : _h.call(_g, {
@@ -587,7 +616,7 @@ function handleDelimitedBlockEOL(parser, newLineLength, {
587
616
  parser.exitState();
588
617
  parser.exitState();
589
618
  } else {
590
- parser.emitError(parser.pos, "INVALID_CHARACTER", "A concise mode closing block delimiter can only be followed by whitespace.");
619
+ parser.emitError(parser.pos, 4 /* INVALID_CHARACTER */, "A concise mode closing block delimiter can only be followed by whitespace.");
591
620
  }
592
621
  } else if (parser.lookAheadFor(indent, parser.pos + newLineLength)) {
593
622
  parser.startText();
@@ -633,7 +662,7 @@ var CDATA = {
633
662
  eol() {
634
663
  },
635
664
  eof(cdata) {
636
- this.emitError(cdata, "MALFORMED_CDATA", "EOF reached while parsing CDATA");
665
+ this.emitError(cdata, 14 /* MALFORMED_CDATA */, "EOF reached while parsing CDATA");
637
666
  },
638
667
  return() {
639
668
  }
@@ -672,7 +701,7 @@ var CLOSE_TAG = {
672
701
  eol() {
673
702
  },
674
703
  eof(closeTag) {
675
- this.emitError(closeTag, "MALFORMED_CLOSE_TAG", "EOF reached while parsing closing tag");
704
+ this.emitError(closeTag, 15 /* MALFORMED_CLOSE_TAG */, "EOF reached while parsing closing tag");
676
705
  },
677
706
  return() {
678
707
  }
@@ -709,7 +738,7 @@ function ensureExpectedCloseTag(parser, closeTag) {
709
738
  const closeTagNameStart = closeTag.start + 2;
710
739
  const closeTagNameEnd = closeTag.end - 1;
711
740
  if (!activeTag) {
712
- parser.emitError(closeTag, "EXTRA_CLOSING_TAG", 'The closing "' + parser.read({ start: closeTagNameStart, end: closeTagNameEnd }) + '" tag was not expected');
741
+ parser.emitError(closeTag, 0 /* EXTRA_CLOSING_TAG */, 'The closing "' + parser.read({ start: closeTagNameStart, end: closeTagNameEnd }) + '" tag was not expected');
713
742
  return false;
714
743
  }
715
744
  const closeTagNamePos = {
@@ -722,7 +751,7 @@ function ensureExpectedCloseTag(parser, closeTag) {
722
751
  start: activeTag.tagName.start,
723
752
  end: activeTag.shorthandEnd
724
753
  })) {
725
- parser.emitError(closeTag, "MISMATCHED_CLOSING_TAG", 'The closing "' + parser.read(closeTagNamePos) + '" tag does not match the corresponding opening "' + (parser.read(activeTag.tagName) || "div") + '" tag');
754
+ parser.emitError(closeTag, 21 /* MISMATCHED_CLOSING_TAG */, 'The closing "' + parser.read(closeTagNamePos) + '" tag does not match the corresponding opening "' + (parser.read(activeTag.tagName) || "div") + '" tag');
726
755
  return false;
727
756
  }
728
757
  }
@@ -758,18 +787,18 @@ var CONCISE_HTML_CONTENT = {
758
787
  parentTag = this.activeTag;
759
788
  }
760
789
  if (!parentTag && curIndent) {
761
- this.emitError(this.pos, "BAD_INDENTATION", "Line has extra indentation at the beginning");
790
+ this.emitError(this.pos, 7 /* INVALID_INDENTATION */, "Line has extra indentation at the beginning");
762
791
  return;
763
792
  }
764
793
  if (parentTag) {
765
794
  if (parentTag.type === 1 /* text */ && code !== 45 /* HTML_BLOCK_DELIMITER */) {
766
- this.emitError(this.pos, "ILLEGAL_LINE_START", 'A line within a tag that only allows text content must begin with a "-" character');
795
+ this.emitError(this.pos, 8 /* INVALID_LINE_START */, 'A line within a tag that only allows text content must begin with a "-" character');
767
796
  return;
768
797
  }
769
798
  if (parentTag.nestedIndent === void 0) {
770
799
  parentTag.nestedIndent = this.indent;
771
800
  } else if (parentTag.nestedIndent !== this.indent) {
772
- this.emitError(this.pos, "BAD_INDENTATION", "Line indentation does match indentation of previous line");
801
+ this.emitError(this.pos, 7 /* INVALID_INDENTATION */, "Line indentation does match indentation of previous line");
773
802
  return;
774
803
  }
775
804
  }
@@ -791,7 +820,7 @@ var CONCISE_HTML_CONTENT = {
791
820
  this.enterState(states_exports.BEGIN_DELIMITED_HTML_BLOCK);
792
821
  this.pos--;
793
822
  } else {
794
- this.emitError(this.pos, "ILLEGAL_LINE_START", 'A line in concise mode cannot start with a single hyphen. Use "--" instead. See: https://github.com/marko-js/htmljs-parser/issues/43');
823
+ this.emitError(this.pos, 8 /* INVALID_LINE_START */, 'A line in concise mode cannot start with a single hyphen. Use "--" instead. See: https://github.com/marko-js/htmljs-parser/issues/43');
795
824
  }
796
825
  return;
797
826
  case 47 /* FORWARD_SLASH */:
@@ -805,7 +834,7 @@ var CONCISE_HTML_CONTENT = {
805
834
  this.pos++;
806
835
  return;
807
836
  default:
808
- this.emitError(this.pos, "ILLEGAL_LINE_START", 'A line in concise mode cannot start with "/" unless it starts a "//" or "/*" comment');
837
+ this.emitError(this.pos, 8 /* INVALID_LINE_START */, 'A line in concise mode cannot start with "/" unless it starts a "//" or "/*" comment');
809
838
  return;
810
839
  }
811
840
  }
@@ -842,7 +871,7 @@ var CONCISE_HTML_CONTENT = {
842
871
  }
843
872
  });
844
873
  if (!this.consumeWhitespaceOnLine(0)) {
845
- this.emitError(this.pos, "INVALID_CHARACTER", "In concise mode a javascript comment block can only be followed by whitespace characters and a newline.");
874
+ this.emitError(this.pos, 4 /* INVALID_CHARACTER */, "In concise mode a javascript comment block can only be followed by whitespace characters and a newline.");
846
875
  }
847
876
  break;
848
877
  }
@@ -876,7 +905,7 @@ var DECLARATION = {
876
905
  eol() {
877
906
  },
878
907
  eof(declaration) {
879
- this.emitError(declaration, "MALFORMED_DECLARATION", "EOF reached while parsing declaration");
908
+ this.emitError(declaration, 17 /* MALFORMED_DECLARATION */, "EOF reached while parsing declaration");
880
909
  },
881
910
  return() {
882
911
  }
@@ -927,7 +956,7 @@ var DTD = {
927
956
  eol() {
928
957
  },
929
958
  eof(documentType) {
930
- this.emitError(documentType, "MALFORMED_DOCUMENT_TYPE", "EOF reached while parsing document type");
959
+ this.emitError(documentType, 18 /* MALFORMED_DOCUMENT_TYPE */, "EOF reached while parsing document type");
931
960
  },
932
961
  return() {
933
962
  }
@@ -1007,11 +1036,11 @@ var EXPRESSION = {
1007
1036
  case 93 /* CLOSE_SQUARE_BRACKET */:
1008
1037
  case 125 /* CLOSE_CURLY_BRACE */: {
1009
1038
  if (!expression.groupStack.length) {
1010
- return this.emitError(expression, "INVALID_EXPRESSION", 'Mismatched group. A closing "' + String.fromCharCode(code) + '" character was found but it is not matched with a corresponding opening character.');
1039
+ return this.emitError(expression, 6 /* INVALID_EXPRESSION */, 'Mismatched group. A closing "' + String.fromCharCode(code) + '" character was found but it is not matched with a corresponding opening character.');
1011
1040
  }
1012
1041
  const expectedCode = expression.groupStack.pop();
1013
1042
  if (expectedCode !== code) {
1014
- return this.emitError(expression, "INVALID_EXPRESSION", 'Mismatched group. A "' + String.fromCharCode(code) + '" character was found when "' + String.fromCharCode(expectedCode) + '" was expected.');
1043
+ return this.emitError(expression, 6 /* INVALID_EXPRESSION */, 'Mismatched group. A "' + String.fromCharCode(code) + '" character was found when "' + String.fromCharCode(expectedCode) + '" was expected.');
1015
1044
  }
1016
1045
  break;
1017
1046
  }
@@ -1031,25 +1060,25 @@ var EXPRESSION = {
1031
1060
  case states_exports.ATTRIBUTE: {
1032
1061
  const attr = parent;
1033
1062
  if (!attr.spread && !attr.name) {
1034
- return this.emitError(expression, "MALFORMED_OPEN_TAG", 'EOF reached while parsing attribute name for the "' + this.read(this.activeTag.tagName) + '" tag');
1063
+ return this.emitError(expression, 19 /* MALFORMED_OPEN_TAG */, 'EOF reached while parsing attribute name for the "' + this.read(this.activeTag.tagName) + '" tag');
1035
1064
  }
1036
- return this.emitError(expression, "MALFORMED_OPEN_TAG", `EOF reached while parsing attribute value for the ${attr.spread ? "..." : attr.name ? `"${this.read(attr.name)}"` : `"default"`} attribute`);
1065
+ return this.emitError(expression, 19 /* MALFORMED_OPEN_TAG */, `EOF reached while parsing attribute value for the ${attr.spread ? "..." : attr.name ? `"${this.read(attr.name)}"` : `"default"`} attribute`);
1037
1066
  }
1038
1067
  case states_exports.TAG_NAME:
1039
- return this.emitError(expression, "MALFORMED_OPEN_TAG", "EOF reached while parsing tag name");
1068
+ return this.emitError(expression, 19 /* MALFORMED_OPEN_TAG */, "EOF reached while parsing tag name");
1040
1069
  case states_exports.PLACEHOLDER:
1041
- return this.emitError(expression, "MALFORMED_PLACEHOLDER", "EOF reached while parsing placeholder");
1070
+ return this.emitError(expression, 20 /* MALFORMED_PLACEHOLDER */, "EOF reached while parsing placeholder");
1042
1071
  }
1043
- return this.emitError(expression, "INVALID_EXPRESSION", "EOF reached while parsing expression");
1072
+ return this.emitError(expression, 6 /* INVALID_EXPRESSION */, "EOF reached while parsing expression");
1044
1073
  }
1045
1074
  },
1046
1075
  return() {
1047
1076
  }
1048
1077
  };
1049
1078
  function buildOperatorPattern(isConcise) {
1050
- const binary = `[!~*%&^|?:]+|(?<=[!=<>&^~|/*?%+-])=|=(?=[!=<>&^~|/*?%+-])|(?<!\\+)\\s*\\+(?:\\s*\\+\\s*\\+)*\\s*(?!\\+)|(?<!^\\s*|-)-${isConcise ? "" : "(?:\\s*-\\s*-)*\\s*"}(?!-)|(?<![/*])/(?![/*${isConcise ? "" : ">"}])|(?<!${isConcise ? "^\\s*|" : ""}\\.)\\.(?!\\.)|<${isConcise ? "{2,}|(?<!^\\s*)<" : "+"}|>${isConcise ? "+" : "{2,}"}|\\bin(?:stanceof)?(?=\\s+[^=/,;:>])`;
1051
- const unary = "\\b(?:a(?:sync|wait)|class|function|new|typeof|void)\\b";
1052
- const lookAheadPattern = "\\s*(?:" + binary + `)\\s*|\\s+(?=[${isConcise ? "" : "["}{(])`;
1079
+ const binary = `[!~*%&^|?:<]+|(?<=[!=<>&^~|/*?%+-])=|=(?=[!=<>&^~|/*?%+-])|(?<!\\+)[ \\t]*\\+(?:[ \\t]*\\+[ \\t]*\\+)*[ \\t]*(?!\\+)|(?<!-)-${isConcise ? "" : "(?:[ \\t]*-[ \\t]*-)*[ \\t]*"}(?!-)|(?<![/*])/(?![/*${isConcise ? "" : ">"}])|(?<!\\.)\\.(?!\\.)|>${isConcise ? "+" : "{2,}"}|\\b(?:in(?:stanceof)?|as|extends)(?=[ \\t]+[^=/,;:>])`;
1080
+ const unary = "\\b(?:a(?:sync|wait)|keyof|class|function|new|typeof|void)\\b";
1081
+ const lookAheadPattern = "[ \\t]*(?:" + binary + `)[ \\t]*|[ \\t]+(?=[{(])`;
1053
1082
  const lookBehindPattern = `(?<=${unary}|${binary})`;
1054
1083
  return new RegExp(`${lookAheadPattern}|${lookBehindPattern}`, "ym");
1055
1084
  }
@@ -1057,7 +1086,7 @@ function checkForOperators(parser, expression) {
1057
1086
  if (expression.skipOperators) {
1058
1087
  return false;
1059
1088
  }
1060
- const pattern = parser.isConcise ? conciseOperatorPattern : htmlOperatorPattern;
1089
+ const pattern = parser.isConcise || expression.terminatedByEOL ? conciseOperatorPattern : htmlOperatorPattern;
1061
1090
  pattern.lastIndex = parser.pos;
1062
1091
  const matches = pattern.exec(parser.data);
1063
1092
  if (matches) {
@@ -1132,7 +1161,7 @@ var HTML_COMMENT = {
1132
1161
  eol() {
1133
1162
  },
1134
1163
  eof(comment) {
1135
- this.emitError(comment, "MALFORMED_COMMENT", "EOF reached while parsing comment");
1164
+ this.emitError(comment, 16 /* MALFORMED_COMMENT */, "EOF reached while parsing comment");
1136
1165
  },
1137
1166
  return() {
1138
1167
  }
@@ -1297,7 +1326,7 @@ var JS_COMMENT_BLOCK = {
1297
1326
  eol() {
1298
1327
  },
1299
1328
  eof(comment) {
1300
- this.emitError(comment, "MALFORMED_COMMENT", "EOF reached while parsing multi-line JavaScript comment");
1329
+ this.emitError(comment, 16 /* MALFORMED_COMMENT */, "EOF reached while parsing multi-line JavaScript comment");
1301
1330
  },
1302
1331
  return() {
1303
1332
  }
@@ -1420,7 +1449,7 @@ var PLACEHOLDER = {
1420
1449
  },
1421
1450
  return(child) {
1422
1451
  if (child.start === child.end) {
1423
- this.emitError(child, "PLACEHOLDER_EXPRESSION_REQUIRED", "Invalid placeholder, the expression cannot be missing");
1452
+ this.emitError(child, 20 /* MALFORMED_PLACEHOLDER */, "Invalid placeholder, the expression cannot be missing");
1424
1453
  }
1425
1454
  this.pos++;
1426
1455
  this.exitState();
@@ -1494,10 +1523,10 @@ var REGULAR_EXPRESSION = {
1494
1523
  }
1495
1524
  },
1496
1525
  eol(_, regExp) {
1497
- this.emitError(regExp, "INVALID_REGULAR_EXPRESSION", "EOL reached while parsing regular expression");
1526
+ this.emitError(regExp, 9 /* INVALID_REGULAR_EXPRESSION */, "EOL reached while parsing regular expression");
1498
1527
  },
1499
1528
  eof(regExp) {
1500
- this.emitError(regExp, "INVALID_REGULAR_EXPRESSION", "EOF reached while parsing regular expression");
1529
+ this.emitError(regExp, 9 /* INVALID_REGULAR_EXPRESSION */, "EOF reached while parsing regular expression");
1501
1530
  },
1502
1531
  return() {
1503
1532
  }
@@ -1531,7 +1560,7 @@ var STRING = {
1531
1560
  eol() {
1532
1561
  },
1533
1562
  eof(string) {
1534
- this.emitError(string, "INVALID_STRING", "EOF reached while parsing string expression");
1563
+ this.emitError(string, 10 /* INVALID_STRING */, "EOF reached while parsing string expression");
1535
1564
  },
1536
1565
  return() {
1537
1566
  }
@@ -1559,7 +1588,7 @@ var TAG_NAME = {
1559
1588
  switch (tagName.shorthandCode) {
1560
1589
  case 35 /* NUMBER_SIGN */:
1561
1590
  if (this.activeTag.hasShorthandId) {
1562
- return this.emitError(tagName, "INVALID_TAG_SHORTHAND", "Multiple shorthand ID parts are not allowed on the same tag");
1591
+ return this.emitError(tagName, 12 /* INVALID_TAG_SHORTHAND */, "Multiple shorthand ID parts are not allowed on the same tag");
1563
1592
  }
1564
1593
  this.activeTag.hasShorthandId = true;
1565
1594
  (_b = (_a = this.options).onTagShorthandId) == null ? void 0 : _b.call(_a, {
@@ -1591,10 +1620,10 @@ var TAG_NAME = {
1591
1620
  tag.type = tagType;
1592
1621
  if (tagType === 3 /* statement */) {
1593
1622
  if (!tag.concise) {
1594
- return this.emitError(tagName, "RESERVED_TAG_NAME", `The "${this.read(tagName)}" tag is reserved and cannot be used as an HTML tag.`);
1623
+ return this.emitError(tagName, 24 /* RESERVED_TAG_NAME */, `The "${this.read(tagName)}" tag is reserved and cannot be used as an HTML tag.`);
1595
1624
  }
1596
1625
  if (tag.parentTag) {
1597
- return this.emitError(tagName, "ROOT_TAG_ONLY", `"${this.read(tagName)}" can only be used at the root of the template.`);
1626
+ return this.emitError(tagName, 25 /* ROOT_TAG_ONLY */, `"${this.read(tagName)}" can only be used at the root of the template.`);
1598
1627
  }
1599
1628
  this.enterState(states_exports.EXPRESSION).terminatedByEOL = true;
1600
1629
  }
@@ -1629,12 +1658,12 @@ var TAG_NAME = {
1629
1658
  if (child.terminatedByEOL)
1630
1659
  return;
1631
1660
  if (child.start === child.end) {
1632
- this.emitError(child, "PLACEHOLDER_EXPRESSION_REQUIRED", "Invalid placeholder, the expression cannot be missing");
1661
+ this.emitError(child, 20 /* MALFORMED_PLACEHOLDER */, "Invalid placeholder, the expression cannot be missing");
1633
1662
  }
1634
1663
  const { quasis, expressions } = tagName;
1635
1664
  const start = child.start - 2;
1636
1665
  const end = ++this.pos;
1637
- const nextStart = end + 1;
1666
+ const nextStart = end;
1638
1667
  expressions.push({
1639
1668
  start,
1640
1669
  end,
@@ -1677,13 +1706,13 @@ var TEMPLATE_STRING = {
1677
1706
  }
1678
1707
  },
1679
1708
  eof(templateString) {
1680
- this.emitError(templateString, "INVALID_TEMPLATE_STRING", "EOF reached while parsing template string expression");
1709
+ this.emitError(templateString, 13 /* INVALID_TEMPLATE_STRING */, "EOF reached while parsing template string expression");
1681
1710
  },
1682
1711
  eol() {
1683
1712
  },
1684
1713
  return(child) {
1685
1714
  if (child.start === child.end) {
1686
- this.emitError(child, "PLACEHOLDER_EXPRESSION_REQUIRED", "Invalid placeholder, the expression cannot be missing");
1715
+ this.emitError(child, 20 /* MALFORMED_PLACEHOLDER */, "Invalid placeholder, the expression cannot be missing");
1687
1716
  }
1688
1717
  this.pos++;
1689
1718
  }
@@ -1765,12 +1794,12 @@ var OPEN_TAG = {
1765
1794
  eof(tag) {
1766
1795
  if (this.isConcise) {
1767
1796
  if (tag.stage === 4 /* ATTR_GROUP */) {
1768
- this.emitError(tag, "MALFORMED_OPEN_TAG", 'EOF reached while within an attribute group (e.g. "[ ... ]").');
1797
+ this.emitError(tag, 19 /* MALFORMED_OPEN_TAG */, 'EOF reached while within an attribute group (e.g. "[ ... ]").');
1769
1798
  return;
1770
1799
  }
1771
1800
  this.exitState();
1772
1801
  } else {
1773
- this.emitError(tag, "MALFORMED_OPEN_TAG", "EOF reached while parsing open tag");
1802
+ this.emitError(tag, 19 /* MALFORMED_OPEN_TAG */, "EOF reached while parsing open tag");
1774
1803
  }
1775
1804
  },
1776
1805
  char(code, tag) {
@@ -1800,17 +1829,17 @@ var OPEN_TAG = {
1800
1829
  }
1801
1830
  break;
1802
1831
  }
1803
- this.emitError(this.pos, "INVALID_CODE_AFTER_SEMICOLON", "A semicolon indicates the end of a line. Only comments may follow it.");
1832
+ this.emitError(this.pos, 5 /* INVALID_CODE_AFTER_SEMICOLON */, "A semicolon indicates the end of a line. Only comments may follow it.");
1804
1833
  }
1805
1834
  return;
1806
1835
  }
1807
1836
  if (code === 45 /* HTML_BLOCK_DELIMITER */) {
1808
1837
  if (this.lookAtCharCodeAhead(1) !== 45 /* HTML_BLOCK_DELIMITER */) {
1809
- this.emitError(tag, "MALFORMED_OPEN_TAG", '"-" not allowed as first character of attribute name');
1838
+ this.emitError(tag, 19 /* MALFORMED_OPEN_TAG */, '"-" not allowed as first character of attribute name');
1810
1839
  return;
1811
1840
  }
1812
1841
  if (tag.stage === 4 /* ATTR_GROUP */) {
1813
- this.emitError(this.pos, "MALFORMED_OPEN_TAG", "Attribute group was not properly ended");
1842
+ this.emitError(this.pos, 19 /* MALFORMED_OPEN_TAG */, "Attribute group was not properly ended");
1814
1843
  return;
1815
1844
  }
1816
1845
  this.exitState();
@@ -1835,14 +1864,14 @@ var OPEN_TAG = {
1835
1864
  return;
1836
1865
  } else if (code === 91 /* OPEN_SQUARE_BRACKET */) {
1837
1866
  if (tag.stage === 4 /* ATTR_GROUP */) {
1838
- this.emitError(this.pos, "MALFORMED_OPEN_TAG", 'Unexpected "[" character within open tag.');
1867
+ this.emitError(this.pos, 19 /* MALFORMED_OPEN_TAG */, 'Unexpected "[" character within open tag.');
1839
1868
  return;
1840
1869
  }
1841
1870
  tag.stage = 4 /* ATTR_GROUP */;
1842
1871
  return;
1843
1872
  } else if (code === 93 /* CLOSE_SQUARE_BRACKET */) {
1844
1873
  if (tag.stage !== 4 /* ATTR_GROUP */) {
1845
- this.emitError(this.pos, "MALFORMED_OPEN_TAG", 'Unexpected "]" character within open tag.');
1874
+ this.emitError(this.pos, 19 /* MALFORMED_OPEN_TAG */, 'Unexpected "]" character within open tag.');
1846
1875
  return;
1847
1876
  }
1848
1877
  tag.stage = 0 /* UNKNOWN */;
@@ -1859,7 +1888,7 @@ var OPEN_TAG = {
1859
1888
  return;
1860
1889
  }
1861
1890
  if (code === 60 /* OPEN_ANGLE_BRACKET */) {
1862
- return this.emitError(this.pos, "ILLEGAL_ATTRIBUTE_NAME", 'Invalid attribute name. Attribute name cannot begin with the "<" character.');
1891
+ return this.emitError(this.pos, 2 /* INVALID_ATTRIBUTE_NAME */, 'Invalid attribute name. Attribute name cannot begin with the "<" character.');
1863
1892
  }
1864
1893
  if (code === 47 /* FORWARD_SLASH */ && this.lookAtCharCodeAhead(1) === 42 /* ASTERISK */) {
1865
1894
  this.enterState(states_exports.JS_COMMENT_BLOCK);
@@ -1881,7 +1910,7 @@ var OPEN_TAG = {
1881
1910
  this.pos--;
1882
1911
  } else if (code === 40 /* OPEN_PAREN */ && !tag.hasAttrs) {
1883
1912
  if (tag.hasArgs) {
1884
- this.emitError(this.pos, "ILLEGAL_TAG_ARGUMENT", "A tag can only have one argument");
1913
+ this.emitError(this.pos, 11 /* INVALID_TAG_ARGUMENT */, "A tag can only have one argument");
1885
1914
  return;
1886
1915
  }
1887
1916
  tag.stage = 2 /* ARGUMENT */;
@@ -1917,7 +1946,7 @@ var OPEN_TAG = {
1917
1946
  switch (tag.stage) {
1918
1947
  case 1 /* VAR */: {
1919
1948
  if (child.start === child.end) {
1920
- return this.emitError(child, "MISSING_TAG_VARIABLE", "A slash was found that was not followed by a variable name or lhs expression");
1949
+ return this.emitError(child, 23 /* MISSING_TAG_VARIABLE */, "A slash was found that was not followed by a variable name or lhs expression");
1921
1950
  }
1922
1951
  (_b = (_a = this.options).onTagVar) == null ? void 0 : _b.call(_a, {
1923
1952
  start: child.start - 1,
@@ -1981,8 +2010,8 @@ function createParser(handlers) {
1981
2010
  read(range) {
1982
2011
  return parser.read(range);
1983
2012
  },
1984
- positionAt(index) {
1985
- return parser.positionAt(index);
2013
+ positionAt(offset) {
2014
+ return parser.positionAt(offset);
1986
2015
  },
1987
2016
  locationAt(range) {
1988
2017
  return parser.locationAt(range);
@@ -1990,6 +2019,7 @@ function createParser(handlers) {
1990
2019
  };
1991
2020
  }
1992
2021
  export {
2022
+ ErrorCode,
1993
2023
  TagType,
1994
2024
  createParser
1995
2025
  };
@@ -70,7 +70,7 @@ export declare namespace Ranges {
70
70
  quasis: Range[];
71
71
  }
72
72
  interface Error extends Range {
73
- code: string;
73
+ code: ErrorCode;
74
74
  message: string;
75
75
  }
76
76
  interface Scriptlet extends Value {
@@ -96,6 +96,34 @@ export declare namespace Ranges {
96
96
  value: Range | undefined;
97
97
  }
98
98
  }
99
+ export declare enum ErrorCode {
100
+ EXTRA_CLOSING_TAG = 0,
101
+ INVALID_ATTRIBUTE_ARGUMENT = 1,
102
+ INVALID_ATTRIBUTE_NAME = 2,
103
+ INVALID_ATTRIBUTE_VALUE = 3,
104
+ INVALID_CHARACTER = 4,
105
+ INVALID_CODE_AFTER_SEMICOLON = 5,
106
+ INVALID_EXPRESSION = 6,
107
+ INVALID_INDENTATION = 7,
108
+ INVALID_LINE_START = 8,
109
+ INVALID_REGULAR_EXPRESSION = 9,
110
+ INVALID_STRING = 10,
111
+ INVALID_TAG_ARGUMENT = 11,
112
+ INVALID_TAG_SHORTHAND = 12,
113
+ INVALID_TEMPLATE_STRING = 13,
114
+ MALFORMED_CDATA = 14,
115
+ MALFORMED_CLOSE_TAG = 15,
116
+ MALFORMED_COMMENT = 16,
117
+ MALFORMED_DECLARATION = 17,
118
+ MALFORMED_DOCUMENT_TYPE = 18,
119
+ MALFORMED_OPEN_TAG = 19,
120
+ MALFORMED_PLACEHOLDER = 20,
121
+ MISMATCHED_CLOSING_TAG = 21,
122
+ MISSING_END_TAG = 22,
123
+ MISSING_TAG_VARIABLE = 23,
124
+ RESERVED_TAG_NAME = 24,
125
+ ROOT_TAG_ONLY = 25
126
+ }
99
127
  export declare const enum TagType {
100
128
  html = 0,
101
129
  text = 1,
@@ -105,12 +133,12 @@ export declare const enum TagType {
105
133
  export interface ParserOptions {
106
134
  onError?(data: Ranges.Error): void;
107
135
  onText?(data: Range): void;
136
+ onPlaceholder?(data: Ranges.Placeholder): void;
108
137
  onComment?(data: Ranges.Value): void;
109
138
  onCDATA?(data: Ranges.Value): void;
110
139
  onDeclaration?(data: Ranges.Value): void;
111
140
  onDoctype?(data: Ranges.Value): void;
112
141
  onScriptlet?(data: Ranges.Scriptlet): void;
113
- onPlaceholder?(data: Ranges.Placeholder): void;
114
142
  onTagName?(data: Ranges.TagName): TagType | void;
115
143
  onTagShorthandId?(data: Ranges.Template): void;
116
144
  onTagShorthandClass?(data: Ranges.Template): void;
@@ -1,5 +1,5 @@
1
1
  import { type Parser } from "../internal";
2
- import type { Location, Position, Range } from "./constants";
2
+ import { Location, Position, Range } from "./constants";
3
3
  export declare function isWhitespaceCode(code: number): boolean;
4
4
  export declare function getLoc(lines: number[], range: Range): Location;
5
5
  export declare function getPos(lines: number[], startLine: number, index: number): Position;
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": "3.2.1",
4
+ "version": "3.3.0",
5
5
  "author": "Phillip Gates-Idem <phillip.idem@gmail.com>",
6
6
  "devDependencies": {
7
7
  "@commitlint/cli": "^16.2.3",