escape.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Process escaped chars and hardbreaks
  2. 'use strict';
  3. var isSpace = require('../common/utils').isSpace;
  4. var ESCAPED = [];
  5. for (var i = 0; i < 256; i++) { ESCAPED.push(0); }
  6. '\\!"#$%&\'()*+,./:;<=>?@[]^_`{|}~-'
  7. .split('').forEach(function (ch) { ESCAPED[ch.charCodeAt(0)] = 1; });
  8. module.exports = function escape(state, silent) {
  9. var ch, pos = state.pos, max = state.posMax;
  10. if (state.src.charCodeAt(pos) !== 0x5C/* \ */) { return false; }
  11. pos++;
  12. if (pos < max) {
  13. ch = state.src.charCodeAt(pos);
  14. if (ch < 256 && ESCAPED[ch] !== 0) {
  15. if (!silent) { state.pending += state.src[pos]; }
  16. state.pos += 2;
  17. return true;
  18. }
  19. if (ch === 0x0A) {
  20. if (!silent) {
  21. state.push('hardbreak', 'br', 0);
  22. }
  23. pos++;
  24. // skip leading whitespaces from next line
  25. while (pos < max) {
  26. ch = state.src.charCodeAt(pos);
  27. if (!isSpace(ch)) { break; }
  28. pos++;
  29. }
  30. state.pos = pos;
  31. return true;
  32. }
  33. }
  34. if (!silent) { state.pending += '\\'; }
  35. state.pos++;
  36. return true;
  37. };