@mtkruto/node 0.1.289 → 0.1.299

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.
Files changed (114) hide show
  1. package/README.md +77 -1
  2. package/esm/client/4_client.js +1 -1
  3. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.d.ts +83 -0
  4. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.js +203 -0
  5. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.d.ts +50 -0
  6. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.js +274 -0
  7. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.d.ts +47 -0
  8. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.js +51 -0
  9. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.d.ts +66 -0
  10. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.js +191 -0
  11. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Node.d.ts +168 -0
  12. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Node.js +385 -0
  13. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Parser.d.ts +159 -0
  14. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Parser.js +431 -0
  15. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.d.ts +181 -0
  16. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.js +1046 -0
  17. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/mod.d.ts +42 -0
  18. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/mod.js +52 -0
  19. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.d.ts +11 -0
  20. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.js +122 -0
  21. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.d.ts +1 -0
  22. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.js +24 -0
  23. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.d.ts +46 -0
  24. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.js +121 -0
  25. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.d.ts +31 -0
  26. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.js +30 -0
  27. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.d.ts +2128 -0
  28. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.js +2127 -0
  29. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.d.ts +109 -0
  30. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.js +108 -0
  31. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.d.ts +8 -0
  32. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.js +1 -0
  33. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.d.ts +90 -0
  34. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.js +95 -0
  35. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.d.ts +50 -0
  36. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.js +128 -0
  37. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.d.ts +46 -0
  38. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.js +110 -0
  39. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.d.ts +42 -0
  40. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.js +120 -0
  41. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.d.ts +6 -0
  42. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.js +6 -0
  43. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.d.ts +54 -0
  44. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.js +110 -0
  45. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.d.ts +40 -0
  46. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.js +75 -0
  47. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.d.ts +58 -0
  48. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.js +101 -0
  49. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/deflate.d.ts +2 -0
  50. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/deflate.js +5 -1
  51. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/inflate.d.ts +2 -2
  52. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/inflate.js +5 -1
  53. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/zlib/crc32.js +1 -1
  54. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/zlib/deflate.js +8 -8
  55. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/zlib/inffast.js +2 -2
  56. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/zlib/inflate.js +19 -21
  57. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/zlib/trees.js +3 -3
  58. package/package.json +1 -1
  59. package/script/client/4_client.js +1 -1
  60. package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.d.ts +83 -0
  61. package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.js +207 -0
  62. package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.d.ts +50 -0
  63. package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.js +301 -0
  64. package/script/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.d.ts +47 -0
  65. package/script/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.js +55 -0
  66. package/script/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.d.ts +66 -0
  67. package/script/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.js +222 -0
  68. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Node.d.ts +168 -0
  69. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Node.js +404 -0
  70. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Parser.d.ts +159 -0
  71. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Parser.js +438 -0
  72. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.d.ts +181 -0
  73. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.js +1052 -0
  74. package/script/deps/deno.land/x/html_parser@v0.1.3/src/mod.d.ts +42 -0
  75. package/script/deps/deno.land/x/html_parser@v0.1.3/src/mod.js +88 -0
  76. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.d.ts +11 -0
  77. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.js +128 -0
  78. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.d.ts +1 -0
  79. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.js +30 -0
  80. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.d.ts +46 -0
  81. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.js +129 -0
  82. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.d.ts +31 -0
  83. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.js +32 -0
  84. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.d.ts +2128 -0
  85. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.js +2129 -0
  86. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.d.ts +109 -0
  87. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.js +110 -0
  88. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.d.ts +8 -0
  89. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.js +3 -0
  90. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.d.ts +90 -0
  91. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.js +114 -0
  92. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.d.ts +50 -0
  93. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.js +134 -0
  94. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.d.ts +46 -0
  95. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.js +118 -0
  96. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.d.ts +42 -0
  97. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.js +129 -0
  98. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.d.ts +6 -0
  99. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.js +22 -0
  100. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.d.ts +54 -0
  101. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.js +119 -0
  102. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.d.ts +40 -0
  103. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.js +86 -0
  104. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.d.ts +58 -0
  105. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.js +112 -0
  106. package/script/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/deflate.d.ts +2 -0
  107. package/script/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/deflate.js +7 -1
  108. package/script/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/inflate.d.ts +2 -2
  109. package/script/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/inflate.js +7 -1
  110. package/script/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/zlib/crc32.js +1 -1
  111. package/script/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/zlib/deflate.js +8 -8
  112. package/script/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/zlib/inffast.js +2 -2
  113. package/script/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/zlib/inflate.js +19 -21
  114. package/script/deps/raw.githubusercontent.com/MTKruto/compress/main/zlib/zlib/trees.js +3 -3
@@ -0,0 +1,1046 @@
1
+ import decodeCodePoint from './utils/entities/decode_codepoint.js';
2
+ import entityMap from './utils/entities/maps/entities.js';
3
+ import legacyMap from './utils/entities/maps/legacy.js';
4
+ import xmlMap from './utils/entities/maps/xml.js';
5
+ /** All the states the tokenizer can be in. */
6
+ var State;
7
+ (function (State) {
8
+ State[State["Text"] = 1] = "Text";
9
+ State[State["BeforeTagName"] = 2] = "BeforeTagName";
10
+ State[State["InTagName"] = 3] = "InTagName";
11
+ State[State["InSelfClosingTag"] = 4] = "InSelfClosingTag";
12
+ State[State["BeforeClosingTagName"] = 5] = "BeforeClosingTagName";
13
+ State[State["InClosingTagName"] = 6] = "InClosingTagName";
14
+ State[State["AfterClosingTagName"] = 7] = "AfterClosingTagName";
15
+ // Attributes
16
+ State[State["BeforeAttributeName"] = 8] = "BeforeAttributeName";
17
+ State[State["InAttributeName"] = 9] = "InAttributeName";
18
+ State[State["AfterAttributeName"] = 10] = "AfterAttributeName";
19
+ State[State["BeforeAttributeValue"] = 11] = "BeforeAttributeValue";
20
+ State[State["InAttributeValueDq"] = 12] = "InAttributeValueDq";
21
+ State[State["InAttributeValueSq"] = 13] = "InAttributeValueSq";
22
+ State[State["InAttributeValueNq"] = 14] = "InAttributeValueNq";
23
+ // Declarations
24
+ State[State["BeforeDeclaration"] = 15] = "BeforeDeclaration";
25
+ State[State["InDeclaration"] = 16] = "InDeclaration";
26
+ // Processing instructions
27
+ State[State["InProcessingInstruction"] = 17] = "InProcessingInstruction";
28
+ // Comments
29
+ State[State["BeforeComment"] = 18] = "BeforeComment";
30
+ State[State["InComment"] = 19] = "InComment";
31
+ State[State["InSpecialComment"] = 20] = "InSpecialComment";
32
+ State[State["AfterComment1"] = 21] = "AfterComment1";
33
+ State[State["AfterComment2"] = 22] = "AfterComment2";
34
+ // Cdata
35
+ State[State["BeforeCdata1"] = 23] = "BeforeCdata1";
36
+ State[State["BeforeCdata2"] = 24] = "BeforeCdata2";
37
+ State[State["BeforeCdata3"] = 25] = "BeforeCdata3";
38
+ State[State["BeforeCdata4"] = 26] = "BeforeCdata4";
39
+ State[State["BeforeCdata5"] = 27] = "BeforeCdata5";
40
+ State[State["BeforeCdata6"] = 28] = "BeforeCdata6";
41
+ State[State["InCdata"] = 29] = "InCdata";
42
+ State[State["AfterCdata1"] = 30] = "AfterCdata1";
43
+ State[State["AfterCdata2"] = 31] = "AfterCdata2";
44
+ // Special tags
45
+ State[State["BeforeSpecialS"] = 32] = "BeforeSpecialS";
46
+ State[State["BeforeSpecialSEnd"] = 33] = "BeforeSpecialSEnd";
47
+ State[State["BeforeScript1"] = 34] = "BeforeScript1";
48
+ State[State["BeforeScript2"] = 35] = "BeforeScript2";
49
+ State[State["BeforeScript3"] = 36] = "BeforeScript3";
50
+ State[State["BeforeScript4"] = 37] = "BeforeScript4";
51
+ State[State["BeforeScript5"] = 38] = "BeforeScript5";
52
+ State[State["AfterScript1"] = 39] = "AfterScript1";
53
+ State[State["AfterScript2"] = 40] = "AfterScript2";
54
+ State[State["AfterScript3"] = 41] = "AfterScript3";
55
+ State[State["AfterScript4"] = 42] = "AfterScript4";
56
+ State[State["AfterScript5"] = 43] = "AfterScript5";
57
+ State[State["BeforeStyle1"] = 44] = "BeforeStyle1";
58
+ State[State["BeforeStyle2"] = 45] = "BeforeStyle2";
59
+ State[State["BeforeStyle3"] = 46] = "BeforeStyle3";
60
+ State[State["BeforeStyle4"] = 47] = "BeforeStyle4";
61
+ State[State["AfterStyle1"] = 48] = "AfterStyle1";
62
+ State[State["AfterStyle2"] = 49] = "AfterStyle2";
63
+ State[State["AfterStyle3"] = 50] = "AfterStyle3";
64
+ State[State["AfterStyle4"] = 51] = "AfterStyle4";
65
+ State[State["BeforeSpecialT"] = 52] = "BeforeSpecialT";
66
+ State[State["BeforeSpecialTEnd"] = 53] = "BeforeSpecialTEnd";
67
+ State[State["BeforeTitle1"] = 54] = "BeforeTitle1";
68
+ State[State["BeforeTitle2"] = 55] = "BeforeTitle2";
69
+ State[State["BeforeTitle3"] = 56] = "BeforeTitle3";
70
+ State[State["BeforeTitle4"] = 57] = "BeforeTitle4";
71
+ State[State["AfterTitle1"] = 58] = "AfterTitle1";
72
+ State[State["AfterTitle2"] = 59] = "AfterTitle2";
73
+ State[State["AfterTitle3"] = 60] = "AfterTitle3";
74
+ State[State["AfterTitle4"] = 61] = "AfterTitle4";
75
+ State[State["BeforeEntity"] = 62] = "BeforeEntity";
76
+ State[State["BeforeNumericEntity"] = 63] = "BeforeNumericEntity";
77
+ State[State["InNamedEntity"] = 64] = "InNamedEntity";
78
+ State[State["InNumericEntity"] = 65] = "InNumericEntity";
79
+ State[State["InHexEntity"] = 66] = "InHexEntity";
80
+ })(State || (State = {}));
81
+ var Special;
82
+ (function (Special) {
83
+ Special[Special["None"] = 1] = "None";
84
+ Special[Special["Script"] = 2] = "Script";
85
+ Special[Special["Style"] = 3] = "Style";
86
+ Special[Special["Title"] = 4] = "Title";
87
+ })(Special || (Special = {}));
88
+ function whitespace(c) {
89
+ return c === ' ' || c === '\n' || c === '\t' || c === '\f' || c === '\r';
90
+ }
91
+ function isASCIIAlpha(c) {
92
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
93
+ }
94
+ function ifElseState(upper, SUCCESS, FAILURE) {
95
+ const lower = upper.toLowerCase();
96
+ if (upper === lower) {
97
+ return (t, c) => {
98
+ if (c === lower) {
99
+ t._state = SUCCESS;
100
+ }
101
+ else {
102
+ t._state = FAILURE;
103
+ t._index--;
104
+ }
105
+ };
106
+ }
107
+ return (t, c) => {
108
+ if (c === lower || c === upper) {
109
+ t._state = SUCCESS;
110
+ }
111
+ else {
112
+ t._state = FAILURE;
113
+ t._index--;
114
+ }
115
+ };
116
+ }
117
+ function consumeSpecialNameChar(upper, NEXT_STATE) {
118
+ const lower = upper.toLowerCase();
119
+ return (t, c) => {
120
+ if (c === lower || c === upper) {
121
+ t._state = NEXT_STATE;
122
+ }
123
+ else {
124
+ t._state = State.InTagName;
125
+ t._index--; // Consume the token again
126
+ }
127
+ };
128
+ }
129
+ const stateBeforeCdata1 = ifElseState('C', State.BeforeCdata2, State.InDeclaration);
130
+ const stateBeforeCdata2 = ifElseState('D', State.BeforeCdata3, State.InDeclaration);
131
+ const stateBeforeCdata3 = ifElseState('A', State.BeforeCdata4, State.InDeclaration);
132
+ const stateBeforeCdata4 = ifElseState('T', State.BeforeCdata5, State.InDeclaration);
133
+ const stateBeforeCdata5 = ifElseState('A', State.BeforeCdata6, State.InDeclaration);
134
+ const stateBeforeScript1 = consumeSpecialNameChar('R', State.BeforeScript2);
135
+ const stateBeforeScript2 = consumeSpecialNameChar('I', State.BeforeScript3);
136
+ const stateBeforeScript3 = consumeSpecialNameChar('P', State.BeforeScript4);
137
+ const stateBeforeScript4 = consumeSpecialNameChar('T', State.BeforeScript5);
138
+ const stateAfterScript1 = ifElseState('R', State.AfterScript2, State.Text);
139
+ const stateAfterScript2 = ifElseState('I', State.AfterScript3, State.Text);
140
+ const stateAfterScript3 = ifElseState('P', State.AfterScript4, State.Text);
141
+ const stateAfterScript4 = ifElseState('T', State.AfterScript5, State.Text);
142
+ const stateBeforeStyle1 = consumeSpecialNameChar('Y', State.BeforeStyle2);
143
+ const stateBeforeStyle2 = consumeSpecialNameChar('L', State.BeforeStyle3);
144
+ const stateBeforeStyle3 = consumeSpecialNameChar('E', State.BeforeStyle4);
145
+ const stateAfterStyle1 = ifElseState('Y', State.AfterStyle2, State.Text);
146
+ const stateAfterStyle2 = ifElseState('L', State.AfterStyle3, State.Text);
147
+ const stateAfterStyle3 = ifElseState('E', State.AfterStyle4, State.Text);
148
+ const stateBeforeSpecialT = consumeSpecialNameChar('I', State.BeforeTitle1);
149
+ const stateBeforeTitle1 = consumeSpecialNameChar('T', State.BeforeTitle2);
150
+ const stateBeforeTitle2 = consumeSpecialNameChar('L', State.BeforeTitle3);
151
+ const stateBeforeTitle3 = consumeSpecialNameChar('E', State.BeforeTitle4);
152
+ const stateAfterSpecialTEnd = ifElseState('I', State.AfterTitle1, State.Text);
153
+ const stateAfterTitle1 = ifElseState('T', State.AfterTitle2, State.Text);
154
+ const stateAfterTitle2 = ifElseState('L', State.AfterTitle3, State.Text);
155
+ const stateAfterTitle3 = ifElseState('E', State.AfterTitle4, State.Text);
156
+ const stateBeforeEntity = ifElseState('#', State.BeforeNumericEntity, State.InNamedEntity);
157
+ const stateBeforeNumericEntity = ifElseState('X', State.InHexEntity, State.InNumericEntity);
158
+ export default class Tokenizer {
159
+ constructor(options, cbs) {
160
+ /** The current state the tokenizer is in. */
161
+ Object.defineProperty(this, "_state", {
162
+ enumerable: true,
163
+ configurable: true,
164
+ writable: true,
165
+ value: State.Text
166
+ });
167
+ /** The read buffer. */
168
+ Object.defineProperty(this, "buffer", {
169
+ enumerable: true,
170
+ configurable: true,
171
+ writable: true,
172
+ value: ''
173
+ });
174
+ /** The beginning of the section that is currently being read. */
175
+ Object.defineProperty(this, "sectionStart", {
176
+ enumerable: true,
177
+ configurable: true,
178
+ writable: true,
179
+ value: 0
180
+ });
181
+ /** The index within the buffer that we are currently looking at. */
182
+ Object.defineProperty(this, "_index", {
183
+ enumerable: true,
184
+ configurable: true,
185
+ writable: true,
186
+ value: 0
187
+ });
188
+ /**
189
+ * Data that has already been processed will be removed from the buffer occasionally.
190
+ * `_bufferOffset` keeps track of how many characters have been removed, to make sure position information is accurate.
191
+ */
192
+ Object.defineProperty(this, "bufferOffset", {
193
+ enumerable: true,
194
+ configurable: true,
195
+ writable: true,
196
+ value: 0
197
+ });
198
+ /** Some behavior, eg. when decoding entities, is done while we are in another state. This keeps track of the other state type. */
199
+ Object.defineProperty(this, "baseState", {
200
+ enumerable: true,
201
+ configurable: true,
202
+ writable: true,
203
+ value: State.Text
204
+ });
205
+ /** For special parsing behavior inside of script and style tags. */
206
+ Object.defineProperty(this, "special", {
207
+ enumerable: true,
208
+ configurable: true,
209
+ writable: true,
210
+ value: Special.None
211
+ });
212
+ /** Indicates whether the tokenizer has been paused. */
213
+ Object.defineProperty(this, "running", {
214
+ enumerable: true,
215
+ configurable: true,
216
+ writable: true,
217
+ value: true
218
+ });
219
+ /** Indicates whether the tokenizer has finished running / `.end` has been called. */
220
+ Object.defineProperty(this, "ended", {
221
+ enumerable: true,
222
+ configurable: true,
223
+ writable: true,
224
+ value: false
225
+ });
226
+ Object.defineProperty(this, "cbs", {
227
+ enumerable: true,
228
+ configurable: true,
229
+ writable: true,
230
+ value: void 0
231
+ });
232
+ Object.defineProperty(this, "xmlMode", {
233
+ enumerable: true,
234
+ configurable: true,
235
+ writable: true,
236
+ value: void 0
237
+ });
238
+ Object.defineProperty(this, "decodeEntities", {
239
+ enumerable: true,
240
+ configurable: true,
241
+ writable: true,
242
+ value: void 0
243
+ });
244
+ this.cbs = cbs;
245
+ this.xmlMode = !!options?.xmlMode;
246
+ this.decodeEntities = options?.decodeEntities ?? true;
247
+ }
248
+ reset() {
249
+ this._state = State.Text;
250
+ this.buffer = '';
251
+ this.sectionStart = 0;
252
+ this._index = 0;
253
+ this.bufferOffset = 0;
254
+ this.baseState = State.Text;
255
+ this.special = Special.None;
256
+ this.running = true;
257
+ this.ended = false;
258
+ }
259
+ write(chunk) {
260
+ if (this.ended)
261
+ this.cbs.onerror(Error('.write() after done!'));
262
+ this.buffer += chunk;
263
+ this.parse();
264
+ }
265
+ end(chunk) {
266
+ if (this.ended)
267
+ this.cbs.onerror(Error('.end() after done!'));
268
+ if (chunk)
269
+ this.write(chunk);
270
+ this.ended = true;
271
+ if (this.running)
272
+ this.finish();
273
+ }
274
+ pause() {
275
+ this.running = false;
276
+ }
277
+ resume() {
278
+ this.running = true;
279
+ if (this._index < this.buffer.length) {
280
+ this.parse();
281
+ }
282
+ if (this.ended) {
283
+ this.finish();
284
+ }
285
+ }
286
+ /**
287
+ * The current index within all of the written data.
288
+ */
289
+ getAbsoluteIndex() {
290
+ return this.bufferOffset + this._index;
291
+ }
292
+ stateText(c) {
293
+ if (c === '<') {
294
+ if (this._index > this.sectionStart) {
295
+ this.cbs.ontext(this.getSection());
296
+ }
297
+ this._state = State.BeforeTagName;
298
+ this.sectionStart = this._index;
299
+ }
300
+ else if (this.decodeEntities &&
301
+ c === '&' &&
302
+ (this.special === Special.None || this.special === Special.Title)) {
303
+ if (this._index > this.sectionStart) {
304
+ this.cbs.ontext(this.getSection());
305
+ }
306
+ this.baseState = State.Text;
307
+ this._state = State.BeforeEntity;
308
+ this.sectionStart = this._index;
309
+ }
310
+ }
311
+ /**
312
+ * HTML only allows ASCII alpha characters (a-z and A-Z) at the beginning of a tag name.
313
+ *
314
+ * XML allows a lot more characters here (@see https://www.w3.org/TR/REC-xml/#NT-NameStartChar).
315
+ * We allow anything that wouldn't end the tag.
316
+ */
317
+ isTagStartChar(c) {
318
+ return (isASCIIAlpha(c) ||
319
+ (this.xmlMode && !whitespace(c) && c !== '/' && c !== '>'));
320
+ }
321
+ stateBeforeTagName(c) {
322
+ if (c === '/') {
323
+ this._state = State.BeforeClosingTagName;
324
+ }
325
+ else if (c === '<') {
326
+ this.cbs.ontext(this.getSection());
327
+ this.sectionStart = this._index;
328
+ }
329
+ else if (c === '>' ||
330
+ this.special !== Special.None ||
331
+ whitespace(c)) {
332
+ this._state = State.Text;
333
+ }
334
+ else if (c === '!') {
335
+ this._state = State.BeforeDeclaration;
336
+ this.sectionStart = this._index + 1;
337
+ }
338
+ else if (c === '?') {
339
+ this._state = State.InProcessingInstruction;
340
+ this.sectionStart = this._index + 1;
341
+ }
342
+ else if (!this.isTagStartChar(c)) {
343
+ this._state = State.Text;
344
+ }
345
+ else {
346
+ this._state =
347
+ !this.xmlMode && (c === 's' || c === 'S')
348
+ ? State.BeforeSpecialS
349
+ : !this.xmlMode && (c === 't' || c === 'T')
350
+ ? State.BeforeSpecialT
351
+ : State.InTagName;
352
+ this.sectionStart = this._index;
353
+ }
354
+ }
355
+ stateInTagName(c) {
356
+ if (c === '/' || c === '>' || whitespace(c)) {
357
+ this.emitToken('onopentagname');
358
+ this._state = State.BeforeAttributeName;
359
+ this._index--;
360
+ }
361
+ }
362
+ stateBeforeClosingTagName(c) {
363
+ if (whitespace(c)) {
364
+ // Ignore
365
+ }
366
+ else if (c === '>') {
367
+ this._state = State.Text;
368
+ }
369
+ else if (this.special !== Special.None) {
370
+ if (this.special !== Special.Title && (c === 's' || c === 'S')) {
371
+ this._state = State.BeforeSpecialSEnd;
372
+ }
373
+ else if (this.special === Special.Title &&
374
+ (c === 't' || c === 'T')) {
375
+ this._state = State.BeforeSpecialTEnd;
376
+ }
377
+ else {
378
+ this._state = State.Text;
379
+ this._index--;
380
+ }
381
+ }
382
+ else if (!this.isTagStartChar(c)) {
383
+ this._state = State.InSpecialComment;
384
+ this.sectionStart = this._index;
385
+ }
386
+ else {
387
+ this._state = State.InClosingTagName;
388
+ this.sectionStart = this._index;
389
+ }
390
+ }
391
+ stateInClosingTagName(c) {
392
+ if (c === '>' || whitespace(c)) {
393
+ this.emitToken('onclosetag');
394
+ this._state = State.AfterClosingTagName;
395
+ this._index--;
396
+ }
397
+ }
398
+ stateAfterClosingTagName(c) {
399
+ // Skip everything until ">"
400
+ if (c === '>') {
401
+ this._state = State.Text;
402
+ this.sectionStart = this._index + 1;
403
+ }
404
+ }
405
+ stateBeforeAttributeName(c) {
406
+ if (c === '>') {
407
+ this.cbs.onopentagend();
408
+ this._state = State.Text;
409
+ this.sectionStart = this._index + 1;
410
+ }
411
+ else if (c === '/') {
412
+ this._state = State.InSelfClosingTag;
413
+ }
414
+ else if (!whitespace(c)) {
415
+ this._state = State.InAttributeName;
416
+ this.sectionStart = this._index;
417
+ }
418
+ }
419
+ stateInSelfClosingTag(c) {
420
+ if (c === '>') {
421
+ this.cbs.onselfclosingtag();
422
+ this._state = State.Text;
423
+ this.sectionStart = this._index + 1;
424
+ this.special = Special.None; // Reset special state, in case of self-closing special tags
425
+ }
426
+ else if (!whitespace(c)) {
427
+ this._state = State.BeforeAttributeName;
428
+ this._index--;
429
+ }
430
+ }
431
+ stateInAttributeName(c) {
432
+ if (c === '=' || c === '/' || c === '>' || whitespace(c)) {
433
+ this.cbs.onattribname(this.getSection());
434
+ this.sectionStart = -1;
435
+ this._state = State.AfterAttributeName;
436
+ this._index--;
437
+ }
438
+ }
439
+ stateAfterAttributeName(c) {
440
+ if (c === '=') {
441
+ this._state = State.BeforeAttributeValue;
442
+ }
443
+ else if (c === '/' || c === '>') {
444
+ this.cbs.onattribend(undefined);
445
+ this._state = State.BeforeAttributeName;
446
+ this._index--;
447
+ }
448
+ else if (!whitespace(c)) {
449
+ this.cbs.onattribend(undefined);
450
+ this._state = State.InAttributeName;
451
+ this.sectionStart = this._index;
452
+ }
453
+ }
454
+ stateBeforeAttributeValue(c) {
455
+ if (c === '"') {
456
+ this._state = State.InAttributeValueDq;
457
+ this.sectionStart = this._index + 1;
458
+ }
459
+ else if (c === "'") {
460
+ this._state = State.InAttributeValueSq;
461
+ this.sectionStart = this._index + 1;
462
+ }
463
+ else if (!whitespace(c)) {
464
+ this._state = State.InAttributeValueNq;
465
+ this.sectionStart = this._index;
466
+ this._index--; // Reconsume token
467
+ }
468
+ }
469
+ handleInAttributeValue(c, quote) {
470
+ if (c === quote) {
471
+ this.emitToken('onattribdata');
472
+ this.cbs.onattribend(quote);
473
+ this._state = State.BeforeAttributeName;
474
+ }
475
+ else if (this.decodeEntities && c === '&') {
476
+ this.emitToken('onattribdata');
477
+ this.baseState = this._state;
478
+ this._state = State.BeforeEntity;
479
+ this.sectionStart = this._index;
480
+ }
481
+ }
482
+ stateInAttributeValueDoubleQuotes(c) {
483
+ this.handleInAttributeValue(c, '"');
484
+ }
485
+ stateInAttributeValueSingleQuotes(c) {
486
+ this.handleInAttributeValue(c, "'");
487
+ }
488
+ stateInAttributeValueNoQuotes(c) {
489
+ if (whitespace(c) || c === '>') {
490
+ this.emitToken('onattribdata');
491
+ this.cbs.onattribend(null);
492
+ this._state = State.BeforeAttributeName;
493
+ this._index--;
494
+ }
495
+ else if (this.decodeEntities && c === '&') {
496
+ this.emitToken('onattribdata');
497
+ this.baseState = this._state;
498
+ this._state = State.BeforeEntity;
499
+ this.sectionStart = this._index;
500
+ }
501
+ }
502
+ stateBeforeDeclaration(c) {
503
+ this._state =
504
+ c === '['
505
+ ? State.BeforeCdata1
506
+ : c === '-'
507
+ ? State.BeforeComment
508
+ : State.InDeclaration;
509
+ }
510
+ stateInDeclaration(c) {
511
+ if (c === '>') {
512
+ this.cbs.ondeclaration(this.getSection());
513
+ this._state = State.Text;
514
+ this.sectionStart = this._index + 1;
515
+ }
516
+ }
517
+ stateInProcessingInstruction(c) {
518
+ if (c === '>') {
519
+ this.cbs.onprocessinginstruction(this.getSection());
520
+ this._state = State.Text;
521
+ this.sectionStart = this._index + 1;
522
+ }
523
+ }
524
+ stateBeforeComment(c) {
525
+ if (c === '-') {
526
+ this._state = State.InComment;
527
+ this.sectionStart = this._index + 1;
528
+ }
529
+ else {
530
+ this._state = State.InDeclaration;
531
+ }
532
+ }
533
+ stateInComment(c) {
534
+ if (c === '-')
535
+ this._state = State.AfterComment1;
536
+ }
537
+ stateInSpecialComment(c) {
538
+ if (c === '>') {
539
+ this.cbs.oncomment(this.buffer.substring(this.sectionStart, this._index));
540
+ this._state = State.Text;
541
+ this.sectionStart = this._index + 1;
542
+ }
543
+ }
544
+ stateAfterComment1(c) {
545
+ if (c === '-') {
546
+ this._state = State.AfterComment2;
547
+ }
548
+ else {
549
+ this._state = State.InComment;
550
+ }
551
+ }
552
+ stateAfterComment2(c) {
553
+ if (c === '>') {
554
+ // Remove 2 trailing chars
555
+ this.cbs.oncomment(this.buffer.substring(this.sectionStart, this._index - 2));
556
+ this._state = State.Text;
557
+ this.sectionStart = this._index + 1;
558
+ }
559
+ else if (c !== '-') {
560
+ this._state = State.InComment;
561
+ }
562
+ // Else: stay in AFTER_COMMENT_2 (`--->`)
563
+ }
564
+ stateBeforeCdata6(c) {
565
+ if (c === '[') {
566
+ this._state = State.InCdata;
567
+ this.sectionStart = this._index + 1;
568
+ }
569
+ else {
570
+ this._state = State.InDeclaration;
571
+ this._index--;
572
+ }
573
+ }
574
+ stateInCdata(c) {
575
+ if (c === ']')
576
+ this._state = State.AfterCdata1;
577
+ }
578
+ stateAfterCdata1(c) {
579
+ if (c === ']')
580
+ this._state = State.AfterCdata2;
581
+ else
582
+ this._state = State.InCdata;
583
+ }
584
+ stateAfterCdata2(c) {
585
+ if (c === '>') {
586
+ // Remove 2 trailing chars
587
+ this.cbs.oncdata(this.buffer.substring(this.sectionStart, this._index - 2));
588
+ this._state = State.Text;
589
+ this.sectionStart = this._index + 1;
590
+ }
591
+ else if (c !== ']') {
592
+ this._state = State.InCdata;
593
+ }
594
+ // Else: stay in AFTER_CDATA_2 (`]]]>`)
595
+ }
596
+ stateBeforeSpecialS(c) {
597
+ if (c === 'c' || c === 'C') {
598
+ this._state = State.BeforeScript1;
599
+ }
600
+ else if (c === 't' || c === 'T') {
601
+ this._state = State.BeforeStyle1;
602
+ }
603
+ else {
604
+ this._state = State.InTagName;
605
+ this._index--; // Consume the token again
606
+ }
607
+ }
608
+ stateBeforeSpecialSEnd(c) {
609
+ if (this.special === Special.Script && (c === 'c' || c === 'C')) {
610
+ this._state = State.AfterScript1;
611
+ }
612
+ else if (this.special === Special.Style && (c === 't' || c === 'T')) {
613
+ this._state = State.AfterStyle1;
614
+ }
615
+ else
616
+ this._state = State.Text;
617
+ }
618
+ stateBeforeSpecialLast(c, special) {
619
+ if (c === '/' || c === '>' || whitespace(c)) {
620
+ this.special = special;
621
+ }
622
+ this._state = State.InTagName;
623
+ this._index--; // Consume the token again
624
+ }
625
+ stateAfterSpecialLast(c, sectionStartOffset) {
626
+ if (c === '>' || whitespace(c)) {
627
+ this.special = Special.None;
628
+ this._state = State.InClosingTagName;
629
+ this.sectionStart = this._index - sectionStartOffset;
630
+ this._index--; // Reconsume the token
631
+ }
632
+ else
633
+ this._state = State.Text;
634
+ }
635
+ // For entities terminated with a semicolon
636
+ parseFixedEntity(map = this.xmlMode ? xmlMap : entityMap) {
637
+ // Offset = 1
638
+ if (this.sectionStart + 1 < this._index) {
639
+ const entity = this.buffer.substring(this.sectionStart + 1, this._index);
640
+ if (Object.prototype.hasOwnProperty.call(map, entity)) {
641
+ this.emitPartial(map[entity]);
642
+ this.sectionStart = this._index + 1;
643
+ }
644
+ }
645
+ }
646
+ // Parses legacy entities (without trailing semicolon)
647
+ parseLegacyEntity() {
648
+ const start = this.sectionStart + 1;
649
+ // The max length of legacy entities is 6
650
+ let limit = Math.min(this._index - start, 6);
651
+ while (limit >= 2) {
652
+ // The min length of legacy entities is 2
653
+ const entity = this.buffer.substr(start, limit);
654
+ if (Object.prototype.hasOwnProperty.call(legacyMap, entity)) {
655
+ this.emitPartial(legacyMap[entity]);
656
+ this.sectionStart += limit + 1;
657
+ return;
658
+ }
659
+ limit--;
660
+ }
661
+ }
662
+ stateInNamedEntity(c) {
663
+ if (c === ';') {
664
+ this.parseFixedEntity();
665
+ // Retry as legacy entity if entity wasn't parsed
666
+ if (this.baseState === State.Text &&
667
+ this.sectionStart + 1 < this._index &&
668
+ !this.xmlMode) {
669
+ this.parseLegacyEntity();
670
+ }
671
+ this._state = this.baseState;
672
+ }
673
+ else if ((c < '0' || c > '9') && !isASCIIAlpha(c)) {
674
+ if (this.xmlMode || this.sectionStart + 1 === this._index) {
675
+ // Ignore
676
+ }
677
+ else if (this.baseState !== State.Text) {
678
+ if (c !== '=') {
679
+ // Parse as legacy entity, without allowing additional characters.
680
+ this.parseFixedEntity(legacyMap);
681
+ }
682
+ }
683
+ else {
684
+ this.parseLegacyEntity();
685
+ }
686
+ this._state = this.baseState;
687
+ this._index--;
688
+ }
689
+ }
690
+ decodeNumericEntity(offset, base, strict) {
691
+ const sectionStart = this.sectionStart + offset;
692
+ if (sectionStart !== this._index) {
693
+ // Parse entity
694
+ const entity = this.buffer.substring(sectionStart, this._index);
695
+ const parsed = parseInt(entity, base);
696
+ this.emitPartial(decodeCodePoint(parsed));
697
+ this.sectionStart = strict ? this._index + 1 : this._index;
698
+ }
699
+ this._state = this.baseState;
700
+ }
701
+ stateInNumericEntity(c) {
702
+ if (c === ';') {
703
+ this.decodeNumericEntity(2, 10, true);
704
+ }
705
+ else if (c < '0' || c > '9') {
706
+ if (!this.xmlMode) {
707
+ this.decodeNumericEntity(2, 10, false);
708
+ }
709
+ else {
710
+ this._state = this.baseState;
711
+ }
712
+ this._index--;
713
+ }
714
+ }
715
+ stateInHexEntity(c) {
716
+ if (c === ';') {
717
+ this.decodeNumericEntity(3, 16, true);
718
+ }
719
+ else if ((c < 'a' || c > 'f') &&
720
+ (c < 'A' || c > 'F') &&
721
+ (c < '0' || c > '9')) {
722
+ if (!this.xmlMode) {
723
+ this.decodeNumericEntity(3, 16, false);
724
+ }
725
+ else {
726
+ this._state = this.baseState;
727
+ }
728
+ this._index--;
729
+ }
730
+ }
731
+ cleanup() {
732
+ if (this.sectionStart < 0) {
733
+ this.buffer = '';
734
+ this.bufferOffset += this._index;
735
+ this._index = 0;
736
+ }
737
+ else if (this.running) {
738
+ if (this._state === State.Text) {
739
+ if (this.sectionStart !== this._index) {
740
+ this.cbs.ontext(this.buffer.substr(this.sectionStart));
741
+ }
742
+ this.buffer = '';
743
+ this.bufferOffset += this._index;
744
+ this._index = 0;
745
+ }
746
+ else if (this.sectionStart === this._index) {
747
+ // The section just started
748
+ this.buffer = '';
749
+ this.bufferOffset += this._index;
750
+ this._index = 0;
751
+ }
752
+ else {
753
+ // Remove everything unnecessary
754
+ this.buffer = this.buffer.substr(this.sectionStart);
755
+ this._index -= this.sectionStart;
756
+ this.bufferOffset += this.sectionStart;
757
+ }
758
+ this.sectionStart = 0;
759
+ }
760
+ }
761
+ /**
762
+ * Iterates through the buffer, calling the function corresponding to the current state.
763
+ *
764
+ * States that are more likely to be hit are higher up, as a performance improvement.
765
+ */
766
+ parse() {
767
+ while (this._index < this.buffer.length && this.running) {
768
+ const c = this.buffer.charAt(this._index);
769
+ if (this._state === State.Text) {
770
+ this.stateText(c);
771
+ }
772
+ else if (this._state === State.InAttributeValueDq) {
773
+ this.stateInAttributeValueDoubleQuotes(c);
774
+ }
775
+ else if (this._state === State.InAttributeName) {
776
+ this.stateInAttributeName(c);
777
+ }
778
+ else if (this._state === State.InComment) {
779
+ this.stateInComment(c);
780
+ }
781
+ else if (this._state === State.InSpecialComment) {
782
+ this.stateInSpecialComment(c);
783
+ }
784
+ else if (this._state === State.BeforeAttributeName) {
785
+ this.stateBeforeAttributeName(c);
786
+ }
787
+ else if (this._state === State.InTagName) {
788
+ this.stateInTagName(c);
789
+ }
790
+ else if (this._state === State.InClosingTagName) {
791
+ this.stateInClosingTagName(c);
792
+ }
793
+ else if (this._state === State.BeforeTagName) {
794
+ this.stateBeforeTagName(c);
795
+ }
796
+ else if (this._state === State.AfterAttributeName) {
797
+ this.stateAfterAttributeName(c);
798
+ }
799
+ else if (this._state === State.InAttributeValueSq) {
800
+ this.stateInAttributeValueSingleQuotes(c);
801
+ }
802
+ else if (this._state === State.BeforeAttributeValue) {
803
+ this.stateBeforeAttributeValue(c);
804
+ }
805
+ else if (this._state === State.BeforeClosingTagName) {
806
+ this.stateBeforeClosingTagName(c);
807
+ }
808
+ else if (this._state === State.AfterClosingTagName) {
809
+ this.stateAfterClosingTagName(c);
810
+ }
811
+ else if (this._state === State.BeforeSpecialS) {
812
+ this.stateBeforeSpecialS(c);
813
+ }
814
+ else if (this._state === State.AfterComment1) {
815
+ this.stateAfterComment1(c);
816
+ }
817
+ else if (this._state === State.InAttributeValueNq) {
818
+ this.stateInAttributeValueNoQuotes(c);
819
+ }
820
+ else if (this._state === State.InSelfClosingTag) {
821
+ this.stateInSelfClosingTag(c);
822
+ }
823
+ else if (this._state === State.InDeclaration) {
824
+ this.stateInDeclaration(c);
825
+ }
826
+ else if (this._state === State.BeforeDeclaration) {
827
+ this.stateBeforeDeclaration(c);
828
+ }
829
+ else if (this._state === State.AfterComment2) {
830
+ this.stateAfterComment2(c);
831
+ }
832
+ else if (this._state === State.BeforeComment) {
833
+ this.stateBeforeComment(c);
834
+ }
835
+ else if (this._state === State.BeforeSpecialSEnd) {
836
+ this.stateBeforeSpecialSEnd(c);
837
+ }
838
+ else if (this._state === State.BeforeSpecialTEnd) {
839
+ stateAfterSpecialTEnd(this, c);
840
+ }
841
+ else if (this._state === State.AfterScript1) {
842
+ stateAfterScript1(this, c);
843
+ }
844
+ else if (this._state === State.AfterScript2) {
845
+ stateAfterScript2(this, c);
846
+ }
847
+ else if (this._state === State.AfterScript3) {
848
+ stateAfterScript3(this, c);
849
+ }
850
+ else if (this._state === State.BeforeScript1) {
851
+ stateBeforeScript1(this, c);
852
+ }
853
+ else if (this._state === State.BeforeScript2) {
854
+ stateBeforeScript2(this, c);
855
+ }
856
+ else if (this._state === State.BeforeScript3) {
857
+ stateBeforeScript3(this, c);
858
+ }
859
+ else if (this._state === State.BeforeScript4) {
860
+ stateBeforeScript4(this, c);
861
+ }
862
+ else if (this._state === State.BeforeScript5) {
863
+ this.stateBeforeSpecialLast(c, Special.Script);
864
+ }
865
+ else if (this._state === State.AfterScript4) {
866
+ stateAfterScript4(this, c);
867
+ }
868
+ else if (this._state === State.AfterScript5) {
869
+ this.stateAfterSpecialLast(c, 6);
870
+ }
871
+ else if (this._state === State.BeforeStyle1) {
872
+ stateBeforeStyle1(this, c);
873
+ }
874
+ else if (this._state === State.InCdata) {
875
+ this.stateInCdata(c);
876
+ }
877
+ else if (this._state === State.BeforeStyle2) {
878
+ stateBeforeStyle2(this, c);
879
+ }
880
+ else if (this._state === State.BeforeStyle3) {
881
+ stateBeforeStyle3(this, c);
882
+ }
883
+ else if (this._state === State.BeforeStyle4) {
884
+ this.stateBeforeSpecialLast(c, Special.Style);
885
+ }
886
+ else if (this._state === State.AfterStyle1) {
887
+ stateAfterStyle1(this, c);
888
+ }
889
+ else if (this._state === State.AfterStyle2) {
890
+ stateAfterStyle2(this, c);
891
+ }
892
+ else if (this._state === State.AfterStyle3) {
893
+ stateAfterStyle3(this, c);
894
+ }
895
+ else if (this._state === State.AfterStyle4) {
896
+ this.stateAfterSpecialLast(c, 5);
897
+ }
898
+ else if (this._state === State.BeforeSpecialT) {
899
+ stateBeforeSpecialT(this, c);
900
+ }
901
+ else if (this._state === State.BeforeTitle1) {
902
+ stateBeforeTitle1(this, c);
903
+ }
904
+ else if (this._state === State.BeforeTitle2) {
905
+ stateBeforeTitle2(this, c);
906
+ }
907
+ else if (this._state === State.BeforeTitle3) {
908
+ stateBeforeTitle3(this, c);
909
+ }
910
+ else if (this._state === State.BeforeTitle4) {
911
+ this.stateBeforeSpecialLast(c, Special.Title);
912
+ }
913
+ else if (this._state === State.AfterTitle1) {
914
+ stateAfterTitle1(this, c);
915
+ }
916
+ else if (this._state === State.AfterTitle2) {
917
+ stateAfterTitle2(this, c);
918
+ }
919
+ else if (this._state === State.AfterTitle3) {
920
+ stateAfterTitle3(this, c);
921
+ }
922
+ else if (this._state === State.AfterTitle4) {
923
+ this.stateAfterSpecialLast(c, 5);
924
+ }
925
+ else if (this._state === State.InProcessingInstruction) {
926
+ this.stateInProcessingInstruction(c);
927
+ }
928
+ else if (this._state === State.InNamedEntity) {
929
+ this.stateInNamedEntity(c);
930
+ }
931
+ else if (this._state === State.BeforeCdata1) {
932
+ stateBeforeCdata1(this, c);
933
+ }
934
+ else if (this._state === State.BeforeEntity) {
935
+ stateBeforeEntity(this, c);
936
+ }
937
+ else if (this._state === State.BeforeCdata2) {
938
+ stateBeforeCdata2(this, c);
939
+ }
940
+ else if (this._state === State.BeforeCdata3) {
941
+ stateBeforeCdata3(this, c);
942
+ }
943
+ else if (this._state === State.AfterCdata1) {
944
+ this.stateAfterCdata1(c);
945
+ }
946
+ else if (this._state === State.AfterCdata2) {
947
+ this.stateAfterCdata2(c);
948
+ }
949
+ else if (this._state === State.BeforeCdata4) {
950
+ stateBeforeCdata4(this, c);
951
+ }
952
+ else if (this._state === State.BeforeCdata5) {
953
+ stateBeforeCdata5(this, c);
954
+ }
955
+ else if (this._state === State.BeforeCdata6) {
956
+ this.stateBeforeCdata6(c);
957
+ }
958
+ else if (this._state === State.InHexEntity) {
959
+ this.stateInHexEntity(c);
960
+ }
961
+ else if (this._state === State.InNumericEntity) {
962
+ this.stateInNumericEntity(c);
963
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
964
+ }
965
+ else if (this._state === State.BeforeNumericEntity) {
966
+ stateBeforeNumericEntity(this, c);
967
+ }
968
+ else {
969
+ this.cbs.onerror(Error('unknown _state'), this._state);
970
+ }
971
+ this._index++;
972
+ }
973
+ this.cleanup();
974
+ }
975
+ finish() {
976
+ // If there is remaining data, emit it in a reasonable way
977
+ if (this.sectionStart < this._index) {
978
+ this.handleTrailingData();
979
+ }
980
+ this.cbs.onend();
981
+ }
982
+ handleTrailingData() {
983
+ const data = this.buffer.substr(this.sectionStart);
984
+ if (this._state === State.InCdata ||
985
+ this._state === State.AfterCdata1 ||
986
+ this._state === State.AfterCdata2) {
987
+ this.cbs.oncdata(data);
988
+ }
989
+ else if (this._state === State.InComment ||
990
+ this._state === State.AfterComment1 ||
991
+ this._state === State.AfterComment2) {
992
+ this.cbs.oncomment(data);
993
+ }
994
+ else if (this._state === State.InNamedEntity && !this.xmlMode) {
995
+ this.parseLegacyEntity();
996
+ if (this.sectionStart < this._index) {
997
+ this._state = this.baseState;
998
+ this.handleTrailingData();
999
+ }
1000
+ }
1001
+ else if (this._state === State.InNumericEntity && !this.xmlMode) {
1002
+ this.decodeNumericEntity(2, 10, false);
1003
+ if (this.sectionStart < this._index) {
1004
+ this._state = this.baseState;
1005
+ this.handleTrailingData();
1006
+ }
1007
+ }
1008
+ else if (this._state === State.InHexEntity && !this.xmlMode) {
1009
+ this.decodeNumericEntity(3, 16, false);
1010
+ if (this.sectionStart < this._index) {
1011
+ this._state = this.baseState;
1012
+ this.handleTrailingData();
1013
+ }
1014
+ }
1015
+ else if (this._state !== State.InTagName &&
1016
+ this._state !== State.BeforeAttributeName &&
1017
+ this._state !== State.BeforeAttributeValue &&
1018
+ this._state !== State.AfterAttributeName &&
1019
+ this._state !== State.InAttributeName &&
1020
+ this._state !== State.InAttributeValueSq &&
1021
+ this._state !== State.InAttributeValueDq &&
1022
+ this._state !== State.InAttributeValueNq &&
1023
+ this._state !== State.InClosingTagName) {
1024
+ this.cbs.ontext(data);
1025
+ }
1026
+ /*
1027
+ * Else, ignore remaining data
1028
+ * TODO add a way to remove current tag
1029
+ */
1030
+ }
1031
+ getSection() {
1032
+ return this.buffer.substring(this.sectionStart, this._index);
1033
+ }
1034
+ emitToken(name) {
1035
+ this.cbs[name](this.getSection());
1036
+ this.sectionStart = -1;
1037
+ }
1038
+ emitPartial(value) {
1039
+ if (this.baseState !== State.Text) {
1040
+ this.cbs.onattribdata(value); // TODO implement the new event
1041
+ }
1042
+ else {
1043
+ this.cbs.ontext(value);
1044
+ }
1045
+ }
1046
+ }