From f8c6b74c95580c1981a6578ebc8681b4975fe35c Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Sat, 13 May 2017 22:31:51 -0400 Subject: [PATCH] fix whitespace preservation --- editor.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/editor.js b/editor.js index 16986f4..83cc246 100644 --- a/editor.js +++ b/editor.js @@ -268,6 +268,51 @@ void_elements = { 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 += "" + } + break + case 'text': + ret += enc_text(n.text) + break + case 'comment': + ret += "" // TODO encode? + break + case 'doctype': + ret += " 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 @@ -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) + // 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) @@ -1996,6 +2042,7 @@ PeachHTML5Editor.prototype.text_cleanup = function(n) { if (run == null) { return } + // merge consecutive text elements 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 (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) } -- 1.7.10.4