From 08cade6674f38354f9bb6c3d1cb8359f7ca6d337 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 20 Mar 2018 19:54:34 +0000 Subject: When using the pretty HTML writer, don't format HTML inside pre elements --- NEWS | 4 ++++ lib/writers/html-writer.js | 11 +++++++++-- test/writers/html-writer.tests.js | 18 ++++++++++++++++++ 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(), "

\n One\n Two\n

"); }); + +test('newlines in
 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(), "
One\nTwo
"); +}); + +test('newlines in element in
 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(), "

One\nTwo

"); +}); -- cgit v1.2.3