preprocessor.js 6.8 KB

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