123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- 'use strict';
- const unicode = require('../common/unicode');
- const ERR = require('../common/error-codes');
- const $ = unicode.CODE_POINTS;
- const DEFAULT_BUFFER_WATERLINE = 1 << 16;
- class Preprocessor {
- constructor() {
- this.html = null;
- this.pos = -1;
- this.lastGapPos = -1;
- this.lastCharPos = -1;
- this.gapStack = [];
- this.skipNextNewLine = false;
- this.lastChunkWritten = false;
- this.endOfChunkHit = false;
- this.bufferWaterline = DEFAULT_BUFFER_WATERLINE;
- }
- _err() {
-
- }
- _addGap() {
- this.gapStack.push(this.lastGapPos);
- this.lastGapPos = this.pos;
- }
- _processSurrogate(cp) {
-
- if (this.pos !== this.lastCharPos) {
- const nextCp = this.html.charCodeAt(this.pos + 1);
- if (unicode.isSurrogatePair(nextCp)) {
-
- this.pos++;
-
- this._addGap();
- return unicode.getSurrogatePairCodePoint(cp, nextCp);
- }
- }
-
- else if (!this.lastChunkWritten) {
- this.endOfChunkHit = true;
- return $.EOF;
- }
-
- this._err(ERR.surrogateInInputStream);
- return cp;
- }
- dropParsedChunk() {
- if (this.pos > this.bufferWaterline) {
- this.lastCharPos -= this.pos;
- this.html = this.html.substring(this.pos);
- this.pos = 0;
- this.lastGapPos = -1;
- this.gapStack = [];
- }
- }
- write(chunk, isLastChunk) {
- if (this.html) {
- this.html += chunk;
- } else {
- this.html = chunk;
- }
- this.lastCharPos = this.html.length - 1;
- this.endOfChunkHit = false;
- this.lastChunkWritten = isLastChunk;
- }
- insertHtmlAtCurrentPos(chunk) {
- this.html = this.html.substring(0, this.pos + 1) + chunk + this.html.substring(this.pos + 1, this.html.length);
- this.lastCharPos = this.html.length - 1;
- this.endOfChunkHit = false;
- }
- advance() {
- this.pos++;
- if (this.pos > this.lastCharPos) {
- this.endOfChunkHit = !this.lastChunkWritten;
- return $.EOF;
- }
- let cp = this.html.charCodeAt(this.pos);
-
-
- if (this.skipNextNewLine && cp === $.LINE_FEED) {
- this.skipNextNewLine = false;
- this._addGap();
- return this.advance();
- }
-
- if (cp === $.CARRIAGE_RETURN) {
- this.skipNextNewLine = true;
- return $.LINE_FEED;
- }
- this.skipNextNewLine = false;
- if (unicode.isSurrogate(cp)) {
- cp = this._processSurrogate(cp);
- }
-
-
-
- const isCommonValidRange =
- (cp > 0x1f && cp < 0x7f) || cp === $.LINE_FEED || cp === $.CARRIAGE_RETURN || (cp > 0x9f && cp < 0xfdd0);
- if (!isCommonValidRange) {
- this._checkForProblematicCharacters(cp);
- }
- return cp;
- }
- _checkForProblematicCharacters(cp) {
- if (unicode.isControlCodePoint(cp)) {
- this._err(ERR.controlCharacterInInputStream);
- } else if (unicode.isUndefinedCodePoint(cp)) {
- this._err(ERR.noncharacterInInputStream);
- }
- }
- retreat() {
- if (this.pos === this.lastGapPos) {
- this.lastGapPos = this.gapStack.pop();
- this.pos--;
- }
- this.pos--;
- }
- }
- module.exports = Preprocessor;
|