HtmlSourceError.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function getIndices(value) {
  7. const result = [];
  8. let index = value.indexOf('\n');
  9. while (index !== -1) {
  10. result.push(index + 1);
  11. index = value.indexOf('\n', index + 1);
  12. }
  13. result.push(value.length + 1);
  14. return result;
  15. }
  16. function offsetToPosition(source, offset) {
  17. let index = -1;
  18. const indices = getIndices(source);
  19. const {
  20. length
  21. } = indices;
  22. if (offset < 0) {
  23. return {};
  24. } // eslint-disable-next-line no-plusplus
  25. while (++index < length) {
  26. if (indices[index] > offset) {
  27. return {
  28. line: index + 1,
  29. column: offset - (indices[index - 1] || 0) + 1,
  30. offset
  31. };
  32. }
  33. }
  34. return {};
  35. }
  36. class HtmlSourceError extends Error {
  37. constructor(error, startIndex, endIndex, source) {
  38. super(error);
  39. this.name = 'HtmlSourceError';
  40. this.message = `${this.name}: ${this.message}`;
  41. this.startIndex = startIndex;
  42. this.endIndex = endIndex;
  43. this.source = source;
  44. const startPosition = offsetToPosition(source, this.startIndex);
  45. const endPosition = offsetToPosition(source, this.endIndex);
  46. this.message += ` (From line ${startPosition.line}, column ${startPosition.column}; to line ${endPosition.line}, column ${endPosition.column})`; // We don't need stack
  47. this.stack = false;
  48. }
  49. }
  50. exports.default = HtmlSourceError;