JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
don't leave empty style attributes
[peach-html5-editor.git] / editor.js
index 564befc..e4ab024 100644 (file)
--- a/editor.js
+++ b/editor.js
@@ -254,22 +254,42 @@ 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: 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
@@ -297,7 +317,13 @@ function nodes_to_html (tree) {
                                }
                                break
                        case 'text':
-                               ret += enc_text(n.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)
+                               }
                                break
                        case 'comment':
                                ret += "<!--" + n.text + "-->" // TODO encode?
@@ -771,6 +797,7 @@ function PeachHTML5Editor (in_el, options) {
        //   on_init: callback for when the editable content is in place
        var css, opt_fragment, outer_bounds, outer_iframe_style, outer_wrap
        this.options = options != null ? options : {}
+       this.pretty_print = options.pretty_print != null ? options.pretty_print : true
        this.in_el = in_el
        this.tree = null // array of Nodes, all editable content
        this.tree_parent = null // this.tree is this.children. .el might === this.idoc.body
@@ -1676,8 +1703,11 @@ 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)
+       if (this.pretty_print) {
+               this.in_el.value = this.pretty_html(this.tree)
+       } else {
+               this.in_el.value = nodes_to_html(this.tree)
+       }
        this.in_el.onchange = (function(_this) { return function() {
                return _this.load_html(_this.in_el.value)
        }})(this)
@@ -1803,7 +1833,7 @@ PeachHTML5Editor.prototype.preserve_space = function(n, ideal_target) {
 PeachHTML5Editor.prototype.update_style_from_el = function(n) {
        var style
        style = n.el.getAttribute('style')
-       if (style != null) {
+       if (style != null && style != '') {
                return n.attrs.style = style
        } else {
                if (n.attrs.style != null) {
@@ -2468,7 +2498,13 @@ PeachHTML5Editor.prototype.pretty_html = function(tree, indent, parent_flags) {
                                }
                                break
                        case 'text':
-                               ret += enc_text(n.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)
+                               }
                                break
                        case 'comment':
                                ret += "<!--" + n.text + "-->" // TODO encode?