You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7 KiB

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