Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Leave text unformatted #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion angular-contenteditable.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ angular.module('contenteditable', [])
'noLineBreaks',
'selectNonEditable',
'moveCaretToEndOnChange',
'unformattedText'
], function(opt) {
var o = attrs[opt]
opts[opt] = o && o !== 'false'
Expand All @@ -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(/<br>$/, '')
Expand Down Expand Up @@ -64,6 +65,11 @@ angular.module('contenteditable', [])
if (!!oldRender) {
oldRender()
}

if (opts.unformattedText) {
ngModel.$viewValue = ngModel.$viewValue.replace(new RegExp('\n','g'), '<br>');
}

element.html(ngModel.$viewValue || '')
if (opts.moveCaretToEndOnChange) {
el = element[0]
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/scenarios.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ describe 'module contenteditable', ->
expect(element('#input').html()).toBe 'a <span style="color:red">red</span> b'
expect(element('#input span').html()).toBe 'red'
expect(element('#output').html()).toBe 'a &lt;span style="color:red"&gt;red&lt;/span&gt; b'

it 'setting the model with some pure text should dispaly <br> instead of newlines \\n', ->
input('unformatted').enter('#h1\n\n##h2\n###h3');
expect(element('#output-unformatted').html()).toBe '#h1<br><br>##h2<br>###h3'

it 'putting some unformatted text with <br>s in the html should replace them with newlines', ->

element('#output-unformatted').html('#h1<br><br>##h2<br>###h3');
expect(input('unformatted').val()).toBe('#h1\n\n##h2\n###h3');
5 changes: 5 additions & 0 deletions test/fixtures/simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<hr>
<label>Contenteditable without Model</label>
<div contenteditable>Edit me - I don't affect anything</div>
<hr>
<label>Model has pure text</label>
<br>
<textarea id="input-unformatted" ng-model="unformatted"></textarea>
<div id="output-unformatted" contenteditable="true" ng-model="unformatted" unformatted-text="true"></div>
</div>
</body>
</html>