JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix whitespace preservation
authorJason Woofenden <jason@jasonwoof.com>
Sun, 14 May 2017 02:31:51 +0000 (22:31 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Sun, 14 May 2017 02:31:51 +0000 (22:31 -0400)
editor.js

index 16986f4..83cc246 100644 (file)
--- a/editor.js
+++ b/editor.js
@@ -268,6 +268,51 @@ void_elements = {
        track: true,
        wbr: true
 }
        track: true,
        wbr: true
 }
+
+// this does not pretty-print
+function nodes_to_html (tree) {
+       var attr_keys, i, k, n, ret
+       ret = ''
+       for (i = 0; i < tree.length; ++i) {
+               n = tree[i]
+               switch (n.type) {
+                       case 'tag':
+                               ret += '<' + n.name
+                               attr_keys = []
+                               for (k in n.attrs) {
+                                       ret += " " + k
+                                       if (n.attrs[k].length > 0) {
+                                               ret += "=\"" + (enc_attr(n.attrs[k])) + "\""
+                                       }
+                               }
+                               ret += '>'
+                               if (void_elements[n.name] == null) {
+                                       if (n.children.length) {
+                                               ret += nodes_to_html(n.children)
+                                       }
+                                       ret += "</" + n.name + ">"
+                               }
+                               break
+                       case 'text':
+                               ret += enc_text(n.text)
+                               break
+                       case 'comment':
+                               ret += "<!--" + n.text + "-->" // TODO encode?
+                               break
+                       case 'doctype':
+                               ret += "<!DOCTYPE " + n.name
+                               if ((n.public_identifier != null) && n.public_identifier.length > 0) {
+                                       ret += " \"" + n.public_identifier + "\""
+                               }
+                               if ((n.system_identifier != null) && n.system_identifier.length > 0) {
+                                       ret += " \"" + n.system_identifier + "\""
+                               }
+                               ret += ">"
+               }
+       }
+       return ret
+}
+
 // TODO make these always pretty-print (on the inside) like blocks
 // TODO careful though: whitespace might get pushed to parent, which might be rendered
 no_text_elements = { // these elements never contain text
 // TODO make these always pretty-print (on the inside) like blocks
 // TODO careful though: whitespace might get pushed to parent, which might be rendered
 no_text_elements = { // these elements never contain text
@@ -1629,6 +1674,7 @@ PeachHTML5Editor.prototype.load_html = function(html) {
 PeachHTML5Editor.prototype.changed = function() {
        this.in_el.onchange = null
        this.in_el.value = this.pretty_html(this.tree)
 PeachHTML5Editor.prototype.changed = function() {
        this.in_el.onchange = null
        this.in_el.value = this.pretty_html(this.tree)
+       // TODO make option for not pretty-printing: nodes_to_html(tree)
        this.in_el.onchange = (function(_this) { return function() {
                return _this.load_html(_this.in_el.value)
        }})(this)
        this.in_el.onchange = (function(_this) { return function() {
                return _this.load_html(_this.in_el.value)
        }})(this)
@@ -1996,6 +2042,7 @@ PeachHTML5Editor.prototype.text_cleanup = function(n) {
        if (run == null) {
                return
        }
        if (run == null) {
                return
        }
+       // merge consecutive text elements
        if (run.length > 1) {
                i = 1
                prev = run[0]
        if (run.length > 1) {
                i = 1
                prev = run[0]
@@ -2077,7 +2124,7 @@ PeachHTML5Editor.prototype.text_cleanup = function(n) {
                        if (need_preserve) {
                                // do we have it already?
                                ws = this.computed_style(n, 'white-space') // FIXME implement this
                        if (need_preserve) {
                                // do we have it already?
                                ws = this.computed_style(n, 'white-space') // FIXME implement this
-                               if (ws_props[ws] == null ? true : ws_props[ws].space == null) {
+                               if (ws_props[ws] != null ? !ws_props[ws].space : true) {
                                        // 2nd arg is ideal target for css rule
                                        ws = this.preserve_space(n, block)
                                }
                                        // 2nd arg is ideal target for css rule
                                        ws = this.preserve_space(n, block)
                                }