1'use strict'; 2 3const Mixin = require('../../utils/mixin'); 4 5class PositionTrackingPreprocessorMixin extends Mixin { 6 constructor(preprocessor) { 7 super(preprocessor); 8 9 this.preprocessor = preprocessor; 10 this.isEol = false; 11 this.lineStartPos = 0; 12 this.droppedBufferSize = 0; 13 14 this.offset = 0; 15 this.col = 0; 16 this.line = 1; 17 } 18 19 _getOverriddenMethods(mxn, orig) { 20 return { 21 advance() { 22 const pos = this.pos + 1; 23 const ch = this.html[pos]; 24 25 //NOTE: LF should be in the last column of the line 26 if (mxn.isEol) { 27 mxn.isEol = false; 28 mxn.line++; 29 mxn.lineStartPos = pos; 30 } 31 32 if (ch === '\n' || (ch === '\r' && this.html[pos + 1] !== '\n')) { 33 mxn.isEol = true; 34 } 35 36 mxn.col = pos - mxn.lineStartPos + 1; 37 mxn.offset = mxn.droppedBufferSize + pos; 38 39 return orig.advance.call(this); 40 }, 41 42 retreat() { 43 orig.retreat.call(this); 44 45 mxn.isEol = false; 46 mxn.col = this.pos - mxn.lineStartPos + 1; 47 }, 48 49 dropParsedChunk() { 50 const prevPos = this.pos; 51 52 orig.dropParsedChunk.call(this); 53 54 const reduction = prevPos - this.pos; 55 56 mxn.lineStartPos -= reduction; 57 mxn.droppedBufferSize += reduction; 58 mxn.offset = mxn.droppedBufferSize + this.pos; 59 } 60 }; 61 } 62} 63 64module.exports = PositionTrackingPreprocessorMixin; 65