From 9c750355781dc61fd3b5caeeb3166beff185a3e7 Mon Sep 17 00:00:00 2001 From: Davide Cavaliere Date: Fri, 21 Aug 2015 18:05:11 +0200 Subject: [PATCH 1/3] adding option to leave the text unformatted --- angular-contenteditable.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/angular-contenteditable.js b/angular-contenteditable.js index 49b59ee..cf81aaa 100644 --- a/angular-contenteditable.js +++ b/angular-contenteditable.js @@ -21,6 +21,7 @@ angular.module('contenteditable', []) 'noLineBreaks', 'selectNonEditable', 'moveCaretToEndOnChange', + 'unformattedText' ], function(opt) { var o = attrs[opt] opts[opt] = o && o !== 'false' @@ -30,7 +31,7 @@ angular.module('contenteditable', []) element.bind('input', function(e) { scope.$apply(function() { var html, html2, rerender - html = element.html() + html = opts.unformattedText ? element[0].innerText : element.html() rerender = false if (opts.stripBr) { html = html.replace(/
$/, '') From 010411cf06d206d7bccead9359342f583e48c0c0 Mon Sep 17 00:00:00 2001 From: Davide Cavaliere Date: Mon, 24 Aug 2015 16:46:39 +0200 Subject: [PATCH 2/3] fixing bug. when setting the model first, we need to replace the newlines to
s --- angular-contenteditable.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/angular-contenteditable.js b/angular-contenteditable.js index cf81aaa..2e97a4f 100644 --- a/angular-contenteditable.js +++ b/angular-contenteditable.js @@ -65,6 +65,11 @@ angular.module('contenteditable', []) if (!!oldRender) { oldRender() } + + if (opts.unformattedText) { + ngModel.$viewValue = ngModel.$viewValue.replace(new RegExp('\n','g'), '
'); + } + element.html(ngModel.$viewValue || '') if (opts.moveCaretToEndOnChange) { el = element[0] From 1dc63c7a4d449f40e0a506af0966f80fe04c3715 Mon Sep 17 00:00:00 2001 From: Davide Cavaliere Date: Mon, 24 Aug 2015 16:46:58 +0200 Subject: [PATCH 3/3] creating tests --- test/e2e/scenarios.coffee | 9 +++++++++ test/fixtures/simple.html | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/test/e2e/scenarios.coffee b/test/e2e/scenarios.coffee index f6d267c..3518810 100644 --- a/test/e2e/scenarios.coffee +++ b/test/e2e/scenarios.coffee @@ -22,3 +22,12 @@ describe 'module contenteditable', -> expect(element('#input').html()).toBe 'a red b' expect(element('#input span').html()).toBe 'red' expect(element('#output').html()).toBe 'a <span style="color:red">red</span> b' + + it 'setting the model with some pure text should dispaly
instead of newlines \\n', -> + input('unformatted').enter('#h1\n\n##h2\n###h3'); + expect(element('#output-unformatted').html()).toBe '#h1

##h2
###h3' + + it 'putting some unformatted text with
s in the html should replace them with newlines', -> + + element('#output-unformatted').html('#h1

##h2
###h3'); + expect(input('unformatted').val()).toBe('#h1\n\n##h2\n###h3'); diff --git a/test/fixtures/simple.html b/test/fixtures/simple.html index 1b3d0bf..277314c 100644 --- a/test/fixtures/simple.html +++ b/test/fixtures/simple.html @@ -34,6 +34,11 @@
Edit me - I don't affect anything
+
+ +
+ +