html-block.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. var openCloseTag = require('../util/html').openCloseTag;
  3. module.exports = blockHTML;
  4. var C_TAB = '\t';
  5. var C_SPACE = ' ';
  6. var C_NEWLINE = '\n';
  7. var C_LT = '<';
  8. function blockHTML(eat, value, silent) {
  9. var self = this;
  10. var blocks = self.options.blocks;
  11. var length = value.length;
  12. var index = 0;
  13. var next;
  14. var line;
  15. var offset;
  16. var character;
  17. var count;
  18. var sequence;
  19. var subvalue;
  20. var sequences = [
  21. [/^<(script|pre|style)(?=(\s|>|$))/i, /<\/(script|pre|style)>/i, true],
  22. [/^<!--/, /-->/, true],
  23. [/^<\?/, /\?>/, true],
  24. [/^<![A-Za-z]/, />/, true],
  25. [/^<!\[CDATA\[/, /\]\]>/, true],
  26. [new RegExp('^</?(' + blocks.join('|') + ')(?=(\\s|/?>|$))', 'i'), /^$/, true],
  27. [new RegExp(openCloseTag.source + '\\s*$'), /^$/, false]
  28. ];
  29. /* Eat initial spacing. */
  30. while (index < length) {
  31. character = value.charAt(index);
  32. if (character !== C_TAB && character !== C_SPACE) {
  33. break;
  34. }
  35. index++;
  36. }
  37. if (value.charAt(index) !== C_LT) {
  38. return;
  39. }
  40. next = value.indexOf(C_NEWLINE, index + 1);
  41. next = next === -1 ? length : next;
  42. line = value.slice(index, next);
  43. offset = -1;
  44. count = sequences.length;
  45. while (++offset < count) {
  46. if (sequences[offset][0].test(line)) {
  47. sequence = sequences[offset];
  48. break;
  49. }
  50. }
  51. if (!sequence) {
  52. return;
  53. }
  54. if (silent) {
  55. return sequence[2];
  56. }
  57. index = next;
  58. if (!sequence[1].test(line)) {
  59. while (index < length) {
  60. next = value.indexOf(C_NEWLINE, index + 1);
  61. next = next === -1 ? length : next;
  62. line = value.slice(index + 1, next);
  63. if (sequence[1].test(line)) {
  64. if (line) {
  65. index = next;
  66. }
  67. break;
  68. }
  69. index = next;
  70. }
  71. }
  72. subvalue = value.slice(0, index);
  73. return eat(subvalue)({type: 'html', value: subvalue});
  74. }