JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
round-trip newlines at start of pre, textare, listing
authorJason Woofenden <jason@jasonwoof.com>
Wed, 17 May 2017 05:13:11 +0000 (01:13 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Wed, 17 May 2017 05:13:11 +0000 (01:13 -0400)
editor.js

index 1a408ba..0c9cf46 100644 (file)
--- a/editor.js
+++ b/editor.js
@@ -256,33 +256,40 @@ function enc_text (txt) {
 
 // no closing tag, cannot have children
 void_elements = {
-       area: true,
-       base: true,
-       br: true,
-       col: true,
-       embed: true,
-       hr: true,
-       img: true,
-       input: true,
-       keygen: true,
-       link: true,
-       meta: true,
-       param: true,
-       source: true,
-       track: true,
-       wbr: true
+       area: 1,
+       base: 1,
+       br: 1,
+       col: 1,
+       embed: 1,
+       hr: 1,
+       img: 1,
+       input: 1,
+       keygen: 1,
+       link: 1,
+       meta: 1,
+       param: 1,
+       source: 1,
+       track: 1,
+       wbr: 1
 }
 
 // contents are not html encoded
 plaintext_elements = {
-       style: true,
-       script: true,
-       xmp: true,
-       iframe: true,
-       noembed: true,
-       noframes: true,
-       plaintext: true,
-       noscript: true
+       style: 1,
+       script: 1,
+       xmp: 1,
+       iframe: 1,
+       noembed: 1,
+       noframes: 1,
+       plaintext: 1,
+       noscript: 1
+}
+
+// parser deletes a starting newline inside:
+newline_eating_elements = {
+       pre: 1,
+       textarea: 1,
+       listing: 1
 }
 
 // this does not pretty-print
@@ -312,6 +319,8 @@ function nodes_to_html (tree) {
                        case 'text':
                                if (n.parent != null ? plaintext_elements[n.parent.name] : false) {
                                        ret += n.text
+                               } else if (n.parent != null ? newline_eating_elements[n.parent.name] && n.text.charAt(0) === "\n" : false) {
+                                       ret += enc_text("\n" + n.text)
                                } else {
                                        ret += enc_text(n.text)
                                }
@@ -2491,6 +2500,8 @@ PeachHTML5Editor.prototype.pretty_html = function(tree, indent, parent_flags) {
                        case 'text':
                                if (n.parent != null ? plaintext_elements[n.parent.name] : false) {
                                        ret += n.text
+                               } else if (n.parent != null ? newline_eating_elements[n.parent.name] && n.text.charAt(0) === "\n" : false) {
+                                       ret += enc_text("\n" + n.text)
                                } else {
                                        ret += enc_text(n.text)
                                }