X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=editor.js;h=104bbae3eb42ba4ecc6aa2773dade9bdf3730013;hb=c753e098069685cae49988ba752a47b2818b1c5b;hp=83cc246a6029348e5494a4cdb6ee3f3a0f3d496e;hpb=f8c6b74c95580c1981a6578ebc8681b4975fe35c;p=peach-html5-editor.git diff --git a/editor.js b/editor.js index 83cc246..104bbae 100644 --- a/editor.js +++ b/editor.js @@ -225,7 +225,8 @@ function new_cursor_position (args) { return null } -// encode text so it can be safely placed inside an html attribute +// html encoding for attributes +// encoding nbsp is not required, but hopefully it is useful enc_attr_regex = new RegExp('(&)|(")|(\u00A0)', 'g') function enc_attr (txt) { return txt.replace(enc_attr_regex, function(match, amp, quote) { @@ -238,6 +239,8 @@ function enc_attr (txt) { return ' ' }) } +// html encoding for text (does nothing to stop whitespace collapse) +// encoding nbsp is not required, but hopefully it is useful enc_text_regex = new RegExp('(&)|(<)|(\u00A0)', 'g') function enc_text (txt) { return txt.replace(enc_text_regex, function(match, amp, lt) { @@ -251,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 +var plaintext_elements = { + style: 1, + script: 1, + xmp: 1, + iframe: 1, + noembed: 1, + noframes: 1, + plaintext: 1, + noscript: 1 +} + +// parser deletes a starting newline inside: +var newline_eating_elements = { + pre: 1, + textarea: 1, + listing: 1 } // this does not pretty-print @@ -294,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 += "" // TODO encode? @@ -768,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 @@ -795,39 +825,37 @@ function PeachHTML5Editor (in_el, options) { if (this.options.editor_id != null) { this.outer_iframe.setAttribute('id', this.options.editor_id) } - this.outer_iframe.onload = (function(_this) { - return function() { - var icss - _this.outer_idoc = _this.outer_iframe.contentDocument - icss = domify(_this.outer_idoc, { style: { children: [ - domify(_this.outer_idoc, {text: css}) - ]}}) - _this.outer_idoc.head.appendChild(icss) - _this.iframe = domify(_this.outer_idoc, {iframe: {sandbox: 'allow-same-origin allow-scripts'}}) - _this.iframe.onload = function() { + this.outer_iframe.onload = (function(_this) { return function() { + var icss + _this.outer_idoc = _this.outer_iframe.contentDocument + icss = domify(_this.outer_idoc, { style: { children: [ + domify(_this.outer_idoc, {text: css}) + ]}}) + _this.outer_idoc.head.appendChild(icss) + _this.iframe = domify(_this.outer_idoc, {iframe: {sandbox: 'allow-same-origin allow-scripts'}}) + _this.iframe.onload = function() { + return _this.init_1() + } + timeout(200, function() { // firefox never fires this onload + if (!_this.init_1_called) { return _this.init_1() } - timeout(200, function() { // firefox never fires this onload - if (!_this.init_1_called) { - return _this.init_1() - } - }) - _this.outer_idoc.body.appendChild( - domify(_this.outer_idoc, {div: {id: 'wrap1', children: [ - domify(_this.outer_idoc, {div: { - style: "position: absolute; top: 0; left: 1px; font-size: 10px", - children: [domify(_this.outer_idoc, {text: "Peach HTML5 Editor"})] - }}), - _this.wrap2 = domify(_this.outer_idoc, {div: {id: 'wrap2', children: [ - domify(_this.outer_idoc, {div: {id: 'wrap3', children: [ - _this.iframe, - _this.overlay = domify(_this.outer_idoc, { div: { id: 'overlay' }}) - ]}}) + }) + _this.outer_idoc.body.appendChild( + domify(_this.outer_idoc, {div: {id: 'wrap1', children: [ + domify(_this.outer_idoc, {div: { + style: "position: absolute; top: 0; left: 1px; font-size: 10px", + children: [domify(_this.outer_idoc, {text: "Peach HTML5 Editor"})] + }}), + _this.wrap2 = domify(_this.outer_idoc, {div: {id: 'wrap2', children: [ + domify(_this.outer_idoc, {div: {id: 'wrap3', children: [ + _this.iframe, + _this.overlay = domify(_this.outer_idoc, { div: { id: 'overlay' }}) ]}}) ]}}) - ) - } - })(this) + ]}}) + ) + }})(this) outer_wrap = domify(document, {div: {"class": 'peach_html5_editor' }}) this.in_el.parentNode.appendChild(outer_wrap) outer_bounds = get_el_bounds(outer_wrap) @@ -927,6 +955,7 @@ PeachHTML5Editor.prototype.generate_outer_css = function(args) { ret += 'padding: 0;' ret += 'color: black;' ret += 'background: white;' + ret += 'font-family: sans;' ret += '}' ret += '#wrap1 {' ret += "border: " + (occupy(1)) + "px solid black;" @@ -1673,8 +1702,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) @@ -1800,7 +1832,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) { @@ -2123,7 +2155,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 + ws = this.computed_style(n, 'white-space') if (ws_props[ws] != null ? !ws_props[ws].space : true) { // 2nd arg is ideal target for css rule ws = this.preserve_space(n, block) @@ -2131,7 +2163,7 @@ PeachHTML5Editor.prototype.text_cleanup = function(n) { eats_start_sp = false } else { if (is_space_code(n.text.charCodeAt(n.text.length - 1))) { - ws = this.computed_style(n, 'white-space') // FIXME implement this + ws = this.computed_style(n, 'white-space') if ((ref1 = ws_props[ws]) != null ? ref1.space : void 0) { eats_start_sp = false } else { @@ -2465,7 +2497,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 += "" // TODO encode?