From 1eb93125e63424cee0fc00f4748840c60b55d108 Mon Sep 17 00:00:00 2001 From: rlx <0x0073@0x2620.org> Date: Thu, 27 Oct 2011 08:47:31 +0000 Subject: [PATCH] some improvements to Ox.Editable --- source/Ox.UI/js/Form/Ox.Editable.js | 59 +++++++++++++++-------------- source/Ox/js/HTML.js | 8 ++-- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/source/Ox.UI/js/Form/Ox.Editable.js b/source/Ox.UI/js/Form/Ox.Editable.js index 8a3f3fb7..e3fd279c 100644 --- a/source/Ox.UI/js/Form/Ox.Editable.js +++ b/source/Ox.UI/js/Form/Ox.Editable.js @@ -58,11 +58,7 @@ Ox.Editable = function(options, self) { //padding: '0 4px 0 4px' width: self.options.width + 'px' }) - .html( - self.options.format - ? self.options.format(self.options.value) - : self.options.value - ) + .html(formatValue()) //[self.options.editing ? 'hide' : 'show']() .appendTo(that); @@ -73,7 +69,7 @@ Ox.Editable = function(options, self) { //left: 200, //top: 200 }) - .html(self.options.value.replace(/ /g, ' ')) + .html(formatTestValue()) .hide() .appendTo(that.$element); @@ -81,7 +77,7 @@ Ox.Editable = function(options, self) { changeOnKeypress: true, style: 'square', type: self.options.type, - value: Ox.parseHTML(self.options.value) + value: formatInputValue() }, self.options.type == 'textarea' ? { width: self.options.width } : {})) @@ -109,13 +105,9 @@ Ox.Editable = function(options, self) { function change(event) { var height, width; - self.options.value = formatValue(event.value); - self.$value.html( - self.options.format - ? self.options.format(self.options.value) - : self.options.value - ); - self.$test.html(self.options.value.replace(/ /g, ' ')); + self.options.value = event.value; + self.$value.html(formatValue); + self.$test.html(formatTestValue()); height = self.options.height || self.$test.height(); //height = Math.max(self.$test.height(), 14); width = Math.max(self.$test.width() + 2, 8); @@ -180,24 +172,33 @@ Ox.Editable = function(options, self) { } } - function formatValue(value) { - if (self.options.type == 'input') { - value = value.replace(/ /g, ' '); - } else { - value = value.replace(/\n/g, '
') - .replace(/$/, '
 '); - } - return value; + function formatInputValue() { + return self.options.type == 'input' + ? self.options.value + : self.options.value.replace(//g, '\n'); + } + + function formatTestValue() { + Ox.print('TEST VALUE =', self.options.type == 'input' + ? self.options.value.replace(/ /g, ' ') + : self.options.value.replace(/\n$/, '
 ') + .replace(/\n/g, '
')); + return self.options.type == 'input' + ? self.options.value.replace(/ /g, ' ') + : self.options.value.replace(/\n$/, '
 ') + .replace(/\n/g, '
'); + } + + function formatValue() { + return self.options.format + ? self.options.format(self.options.value) + : self.options.value } function submit() { - self.options.value = self.$input.value().trim(); - self.$input.value(self.options.value); - self.$value.html( - self.options.format - ? self.options.format(self.options.value) - : self.options.value - ); + self.options.value = Ox.parseHTML(self.$input.value()); + self.$input.value(formatInputValue()); + self.$value.html(formatValue()); cancel(); that.triggerEvent('submit', { value: self.options.value diff --git a/source/Ox/js/HTML.js b/source/Ox/js/HTML.js index c0ae4544..9d4b49d4 100644 --- a/source/Ox/js/HTML.js +++ b/source/Ox/js/HTML.js @@ -37,10 +37,9 @@ Ox.parseHTML Takes HTML from an untrusted source and returns something sane Ox.parseHTML = (function() { var defaultTags = [ - // fixme: why not p? - 'a', 'b', 'blockquote', 'cite', 'code', + 'a', 'b', 'blockquote', 'br', 'cite', 'code', 'del', 'em', 'i', 'img', 'ins', - 'li', 'ol', 'q', 'rtl', 's', + 'li', 'ol', 'p', 'q', 'rtl', 's', 'strong', 'sub', 'sup', 'ul', '[]' ], parse = { @@ -89,7 +88,8 @@ Ox.parseHTML = (function() { matches.forEach(function(match, i) { html = html.replace(new RegExp(tab + i + tab, 'gi'), match); }); - html = html.replace(/\n/g, '
\n'); + //html = html.replace(/\n/g, '
\n'); + html = html.replace(/\n/g, '
'); // close extra opening (and remove extra closing) tags // note: this converts '"' to '"' return Ox.element('
').html(html).html();