preprocessor.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Preprocessor = void 0;
  4. const unicode_js_1 = require("../common/unicode.js");
  5. const error_codes_js_1 = require("../common/error-codes.js");
  6. //Const
  7. const DEFAULT_BUFFER_WATERLINE = 1 << 16;
  8. //Preprocessor
  9. //NOTE: HTML input preprocessing
  10. //(see: http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#preprocessing-the-input-stream)
  11. class Preprocessor {
  12. constructor(handler) {
  13. this.handler = handler;
  14. this.html = '';
  15. this.pos = -1;
  16. // NOTE: Initial `lastGapPos` is -2, to ensure `col` on initialisation is 0
  17. this.lastGapPos = -2;
  18. this.gapStack = [];
  19. this.skipNextNewLine = false;
  20. this.lastChunkWritten = false;
  21. this.endOfChunkHit = false;
  22. this.bufferWaterline = DEFAULT_BUFFER_WATERLINE;
  23. this.isEol = false;
  24. this.lineStartPos = 0;
  25. this.droppedBufferSize = 0;
  26. this.line = 1;
  27. //NOTE: avoid reporting errors twice on advance/retreat
  28. this.lastErrOffset = -1;
  29. }
  30. /** The column on the current line. If we just saw a gap (eg. a surrogate pair), return the index before. */
  31. get col() {
  32. return this.pos - this.lineStartPos + Number(this.lastGapPos !== this.pos);
  33. }
  34. get offset() {
  35. return this.droppedBufferSize + this.pos;
  36. }
  37. getError(code) {
  38. const { line, col, offset } = this;
  39. return {
  40. code,
  41. startLine: line,
  42. endLine: line,
  43. startCol: col,
  44. endCol: col,
  45. startOffset: offset,
  46. endOffset: offset,
  47. };
  48. }
  49. _err(code) {
  50. if (this.handler.onParseError && this.lastErrOffset !== this.offset) {
  51. this.lastErrOffset = this.offset;
  52. this.handler.onParseError(this.getError(code));
  53. }
  54. }
  55. _addGap() {
  56. this.gapStack.push(this.lastGapPos);
  57. this.lastGapPos = this.pos;
  58. }
  59. _processSurrogate(cp) {
  60. //NOTE: try to peek a surrogate pair
  61. if (this.pos !== this.html.length - 1) {
  62. const nextCp = this.html.charCodeAt(this.pos + 1);
  63. if ((0, unicode_js_1.isSurrogatePair)(nextCp)) {
  64. //NOTE: we have a surrogate pair. Peek pair character and recalculate code point.
  65. this.pos++;
  66. //NOTE: add a gap that should be avoided during retreat
  67. this._addGap();
  68. return (0, unicode_js_1.getSurrogatePairCodePoint)(cp, nextCp);
  69. }
  70. }
  71. //NOTE: we are at the end of a chunk, therefore we can't infer the surrogate pair yet.
  72. else if (!this.lastChunkWritten) {
  73. this.endOfChunkHit = true;
  74. return unicode_js_1.CODE_POINTS.EOF;
  75. }
  76. //NOTE: isolated surrogate
  77. this._err(error_codes_js_1.ERR.surrogateInInputStream);
  78. return cp;
  79. }
  80. willDropParsedChunk() {
  81. return this.pos > this.bufferWaterline;
  82. }
  83. dropParsedChunk() {
  84. if (this.willDropParsedChunk()) {
  85. this.html = this.html.substring(this.pos);
  86. this.lineStartPos -= this.pos;
  87. this.droppedBufferSize += this.pos;
  88. this.pos = 0;
  89. this.lastGapPos = -2;
  90. this.gapStack.length = 0;
  91. }
  92. }
  93. write(chunk, isLastChunk) {
  94. if (this.html.length > 0) {
  95. this.html += chunk;
  96. }
  97. else {
  98. this.html = chunk;
  99. }
  100. this.endOfChunkHit = false;
  101. this.lastChunkWritten = isLastChunk;
  102. }
  103. insertHtmlAtCurrentPos(chunk) {
  104. this.html = this.html.substring(0, this.pos + 1) + chunk + this.html.substring(this.pos + 1);
  105. this.endOfChunkHit = false;
  106. }
  107. startsWith(pattern, caseSensitive) {
  108. // Check if our buffer has enough characters
  109. if (this.pos + pattern.length > this.html.length) {
  110. this.endOfChunkHit = !this.lastChunkWritten;
  111. return false;
  112. }
  113. if (caseSensitive) {
  114. return this.html.startsWith(pattern, this.pos);
  115. }
  116. for (let i = 0; i < pattern.length; i++) {
  117. const cp = this.html.charCodeAt(this.pos + i) | 0x20;
  118. if (cp !== pattern.charCodeAt(i)) {
  119. return false;
  120. }
  121. }
  122. return true;
  123. }
  124. peek(offset) {
  125. const pos = this.pos + offset;
  126. if (pos >= this.html.length) {
  127. this.endOfChunkHit = !this.lastChunkWritten;
  128. return unicode_js_1.CODE_POINTS.EOF;
  129. }
  130. const code = this.html.charCodeAt(pos);
  131. return code === unicode_js_1.CODE_POINTS.CARRIAGE_RETURN ? unicode_js_1.CODE_POINTS.LINE_FEED : code;
  132. }
  133. advance() {
  134. this.pos++;
  135. //NOTE: LF should be in the last column of the line
  136. if (this.isEol) {
  137. this.isEol = false;
  138. this.line++;
  139. this.lineStartPos = this.pos;
  140. }
  141. if (this.pos >= this.html.length) {
  142. this.endOfChunkHit = !this.lastChunkWritten;
  143. return unicode_js_1.CODE_POINTS.EOF;
  144. }
  145. let cp = this.html.charCodeAt(this.pos);
  146. //NOTE: all U+000D CARRIAGE RETURN (CR) characters must be converted to U+000A LINE FEED (LF) characters
  147. if (cp === unicode_js_1.CODE_POINTS.CARRIAGE_RETURN) {
  148. this.isEol = true;
  149. this.skipNextNewLine = true;
  150. return unicode_js_1.CODE_POINTS.LINE_FEED;
  151. }
  152. //NOTE: any U+000A LINE FEED (LF) characters that immediately follow a U+000D CARRIAGE RETURN (CR) character
  153. //must be ignored.
  154. if (cp === unicode_js_1.CODE_POINTS.LINE_FEED) {
  155. this.isEol = true;
  156. if (this.skipNextNewLine) {
  157. // `line` will be bumped again in the recursive call.
  158. this.line--;
  159. this.skipNextNewLine = false;
  160. this._addGap();
  161. return this.advance();
  162. }
  163. }
  164. this.skipNextNewLine = false;
  165. if ((0, unicode_js_1.isSurrogate)(cp)) {
  166. cp = this._processSurrogate(cp);
  167. }
  168. //OPTIMIZATION: first check if code point is in the common allowed
  169. //range (ASCII alphanumeric, whitespaces, big chunk of BMP)
  170. //before going into detailed performance cost validation.
  171. const isCommonValidRange = this.handler.onParseError === null ||
  172. (cp > 0x1f && cp < 0x7f) ||
  173. cp === unicode_js_1.CODE_POINTS.LINE_FEED ||
  174. cp === unicode_js_1.CODE_POINTS.CARRIAGE_RETURN ||
  175. (cp > 0x9f && cp < 64976);
  176. if (!isCommonValidRange) {
  177. this._checkForProblematicCharacters(cp);
  178. }
  179. return cp;
  180. }
  181. _checkForProblematicCharacters(cp) {
  182. if ((0, unicode_js_1.isControlCodePoint)(cp)) {
  183. this._err(error_codes_js_1.ERR.controlCharacterInInputStream);
  184. }
  185. else if ((0, unicode_js_1.isUndefinedCodePoint)(cp)) {
  186. this._err(error_codes_js_1.ERR.noncharacterInInputStream);
  187. }
  188. }
  189. retreat(count) {
  190. this.pos -= count;
  191. while (this.pos < this.lastGapPos) {
  192. this.lastGapPos = this.gapStack.pop();
  193. this.pos--;
  194. }
  195. this.isEol = false;
  196. }
  197. }
  198. exports.Preprocessor = Preprocessor;
  199. //# sourceMappingURL=preprocessor.js.map