html-writer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. var _ = require("underscore");
  2. exports.writer = writer;
  3. function writer(options) {
  4. options = options || {};
  5. if (options.prettyPrint) {
  6. return prettyWriter();
  7. } else {
  8. return simpleWriter();
  9. }
  10. }
  11. var indentedElements = {
  12. div: true,
  13. p: true,
  14. ul: true,
  15. li: true
  16. };
  17. function prettyWriter() {
  18. var indentationLevel = 0;
  19. var indentation = " ";
  20. var stack = [];
  21. var start = true;
  22. var inText = false;
  23. var writer = simpleWriter();
  24. function open(tagName, attributes) {
  25. if (indentedElements[tagName]) {
  26. indent();
  27. }
  28. stack.push(tagName);
  29. writer.open(tagName, attributes);
  30. if (indentedElements[tagName]) {
  31. indentationLevel++;
  32. }
  33. start = false;
  34. }
  35. function close(tagName) {
  36. if (indentedElements[tagName]) {
  37. indentationLevel--;
  38. indent();
  39. }
  40. stack.pop();
  41. writer.close(tagName);
  42. }
  43. function text(value) {
  44. startText();
  45. var text = isInPre() ? value : value.replace("\n", "\n" + indentation);
  46. writer.text(text);
  47. }
  48. function selfClosing(tagName, attributes) {
  49. indent();
  50. writer.selfClosing(tagName, attributes);
  51. }
  52. function insideIndentedElement() {
  53. return stack.length === 0 || indentedElements[stack[stack.length - 1]];
  54. }
  55. function startText() {
  56. if (!inText) {
  57. indent();
  58. inText = true;
  59. }
  60. }
  61. function indent() {
  62. inText = false;
  63. if (!start && insideIndentedElement() && !isInPre()) {
  64. writer._append("\n");
  65. for (var i = 0; i < indentationLevel; i++) {
  66. writer._append(indentation);
  67. }
  68. }
  69. }
  70. function isInPre() {
  71. return _.some(stack, function(tagName) {
  72. return tagName === "pre";
  73. });
  74. }
  75. return {
  76. asString: writer.asString,
  77. open: open,
  78. close: close,
  79. text: text,
  80. selfClosing: selfClosing
  81. };
  82. }
  83. function simpleWriter() {
  84. var fragments = [];
  85. function open(tagName, attributes) {
  86. var attributeString = generateAttributeString(attributes);
  87. fragments.push("<" + tagName + attributeString + ">");
  88. }
  89. function close(tagName) {
  90. fragments.push("</" + tagName + ">");
  91. }
  92. function selfClosing(tagName, attributes) {
  93. var attributeString = generateAttributeString(attributes);
  94. fragments.push("<" + tagName + attributeString + " />");
  95. }
  96. function generateAttributeString(attributes) {
  97. return _.map(attributes, function(value, key) {
  98. return " " + key + '="' + escapeHtmlAttribute(value) + '"';
  99. }).join("");
  100. }
  101. function text(value) {
  102. fragments.push(escapeHtmlText(value));
  103. }
  104. function append(html) {
  105. fragments.push(html);
  106. }
  107. function asString() {
  108. return fragments.join("");
  109. }
  110. return {
  111. asString: asString,
  112. open: open,
  113. close: close,
  114. text: text,
  115. selfClosing: selfClosing,
  116. _append: append
  117. };
  118. }
  119. function escapeHtmlText(value) {
  120. return value
  121. .replace(/&/g, '&amp;')
  122. .replace(/</g, '&lt;')
  123. .replace(/>/g, '&gt;');
  124. }
  125. function escapeHtmlAttribute(value) {
  126. return value
  127. .replace(/&/g, '&amp;')
  128. .replace(/"/g, '&quot;')
  129. .replace(/</g, '&lt;')
  130. .replace(/>/g, '&gt;');
  131. }