summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2018-03-20 19:54:34 +0000
committerMichael Williamson <mike@zwobble.org>2018-03-20 19:54:34 +0000
commit08cade6674f38354f9bb6c3d1cb8359f7ca6d337 (patch)
tree33999a255b0b7b334b88cc7867db5ec4071be7fa
parent31d2ad16c1a492cf0de0d5b6786f63496c01d78d (diff)
When using the pretty HTML writer, don't format HTML inside pre elements
-rw-r--r--NEWS4
-rw-r--r--lib/writers/html-writer.js11
-rw-r--r--test/writers/html-writer.tests.js18
3 files changed, 31 insertions, 2 deletions
diff --git a/NEWS b/NEWS
index 746d9aa..e94d656 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+# 1.4.5
+
+* When using the pretty HTML writer, don't format HTML inside pre elements.
+
# 1.4.4
* Parse paragraph indents.
diff --git a/lib/writers/html-writer.js b/lib/writers/html-writer.js
index ee7d49f..e16feec 100644
--- a/lib/writers/html-writer.js
+++ b/lib/writers/html-writer.js
@@ -53,7 +53,8 @@ function prettyWriter() {
function text(value) {
startText();
- writer.text(value.replace("\n", "\n" + indentation));
+ var text = isInPre() ? value : value.replace("\n", "\n" + indentation);
+ writer.text(text);
}
function selfClosing(tagName, attributes) {
@@ -79,7 +80,7 @@ function prettyWriter() {
function indent() {
inText = false;
- if (!start && insideIndentedElement()) {
+ if (!start && insideIndentedElement() && !isInPre()) {
writer.append("\n");
for (var i = 0; i < indentationLevel; i++) {
writer.append(indentation);
@@ -87,6 +88,12 @@ function prettyWriter() {
}
}
+ function isInPre() {
+ return _.some(stack, function(tagName) {
+ return tagName === "pre";
+ });
+ }
+
return {
asString: writer.asString,
open: open,
diff --git a/test/writers/html-writer.tests.js b/test/writers/html-writer.tests.js
index b5cdf4e..b91cba8 100644
--- a/test/writers/html-writer.tests.js
+++ b/test/writers/html-writer.tests.js
@@ -113,3 +113,21 @@ test('newlines in appended HTML are indented', function() {
writer.close("p");
return assert.equal(writer.asString(), "<p>\n One\n Two\n</p>");
});
+
+test('newlines in <pre> are not indented', function() {
+ var writer = htmlWriter.writer({prettyPrint: true});
+ writer.open("pre");
+ writer.text("One\nTwo");
+ writer.close("pre");
+ return assert.equal(writer.asString(), "<pre>One\nTwo</pre>");
+});
+
+test('newlines in element in <pre> are not indented', function() {
+ var writer = htmlWriter.writer({prettyPrint: true});
+ writer.open("pre");
+ writer.open("p");
+ writer.text("One\nTwo");
+ writer.close("p");
+ writer.close("pre");
+ return assert.equal(writer.asString(), "<pre><p>One\nTwo</p></pre>");
+});