Skip to content
This repository was archived by the owner on Nov 15, 2019. It is now read-only.

Commit c9b48a5

Browse files
author
Filippo Conti
authored
Merge pull request #19 from vincurekf/strip-replace
Added stripReplace feature, can be used to remove defined string from text, to filter by custom regular expression or strip html tags from resul text
2 parents f8bba46 + 75bac27 commit c9b48a5

7 files changed

+92
-9
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ and you are ready to go, add the directive to any element you want:
3737
* __render-html__: if set to true allow the text passed as input to be compiled and rendered
3838
* __edit-callback__: a callback that is called wherever the model value is changed
3939
* __is-editing__: optional argument that can be used to programatically enable/disable the editor
40+
* __strip-replace__: optional argument that can be `true` to remove all HTML tags and line breaks, `string` to remove or custom regular `expression`, or array with `expression` to match with `replacement` to and `flags` use: `['expression','replacement','flags']`
4041

4142
Note that, __edit-callback__ has two arguments, that you must specify in your template to use them:
4243
* __text__: the new text inside the element
@@ -81,6 +82,18 @@ With __focus-select__ all text content will be selected on element click or focu
8182
<span focus-select="true" ng-model="myModel" content-editable>Change me!</span>
8283
```
8384

85+
With __strip-replace__ attribute set as `boolean`:
86+
```html
87+
<!-- boolean: removes all HTML tags and line breaks -->
88+
<span focus-select="true" ng-model="myModel" strip-replace="true" content-editable>Change me!<br><b>I will become clear text without formating</b></span>
89+
```
90+
91+
With __strip-replace__ attribute set as `array`:
92+
```html
93+
<!-- array: creates new RegExp() from array ['string / regular expression','replace with','expression flags'] -->
94+
<span focus-select="true" ng-model="myModel" strip-replace="[' ','-','gi']" content-editable>Change me!</span>
95+
```
96+
8497
If you want to run a callback you must use __edit-callback__ attribute with a valid function and it will run every time the model value is __changed__.
8598

8699
Since version __1.2.0__, after issue [#13](https://github.com/codekraft-studio/angular-content-editable/issues/13) you __MUST__ specify the arguments `text, elem` if you want to use them in your callback, like in this example.

dist/angular-content-editable.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ angular.module('angular-content-editable')
77
var directive = {
88
restrict: 'A',
99
require: 'ngModel',
10-
scope: { editCallback: '&?', isEditing: '=?' },
10+
scope: { editCallback: '&?', isEditing: '=?', stripReplace: '=?' },
1111
link: _link
1212
};
1313

@@ -21,7 +21,7 @@ angular.module('angular-content-editable')
2121
return;
2222
}
2323

24-
var noEscape = true, originalElement = elem[0], callback;
24+
var noEscape = true, originalElement = elem[0], callback, stripReplace;
2525

2626
// get default usage options
2727
var options = angular.copy(contentEditable);
@@ -36,6 +36,9 @@ angular.module('angular-content-editable')
3636
// Get the callback from item scope or global defined
3737
callback = scope.editCallback || options.editCallback;
3838

39+
// Get the strip tags option from item scope or global defined
40+
stripReplace = scope.stripReplace || options.stripReplace;
41+
3942
// add editable class
4043
attrs.$addClass(options.editableClass);
4144

@@ -124,7 +127,25 @@ angular.module('angular-content-editable')
124127

125128
// if element value is different from model value
126129
if( html != ngModel.$modelValue ) {
127-
130+
131+
// if user defined strip-replace variable
132+
if( stripReplace ){
133+
if( angular.isString(stripReplace) ) {
134+
// if stripReplace is a string create new RegExp with gi (global, ignore case)
135+
html = html.replace( new RegExp( stripReplace, 'g' ), '' );
136+
}else if( angular.isArray(stripReplace) ){
137+
// if stripReplace is an array create new RegExp from array values
138+
// get values from array or set default
139+
var e = stripReplace[0] || '', r = stripReplace[1] || '', f = stripReplace[2] || 'g';
140+
html = html.replace( new RegExp( e, f ), r );
141+
}else{
142+
// if stripReplace is set to "true", remove all html tags and new line breaks
143+
html = html.replace(/(<([^>]+)>)/ig, '').replace(/\r?\n|\r/g, '');
144+
}
145+
// update elem html value
146+
elem.html(html);
147+
}
148+
128149
/**
129150
* This method should be called
130151
* when a controller wants to
@@ -228,7 +249,8 @@ angular.module('angular-content-editable')
228249
singleLine: false,
229250
focusSelect: true, // default on focus select all text inside
230251
renderHtml: false,
231-
editCallback: false
252+
editCallback: false,
253+
stripReplace: false
232254
}
233255

234256
this.configure = function (options) {

dist/angular-content-editable.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ <h3 ng-model="viewStrings.pageTitle" single-line="true" focus-select="false" con
7070
<br>and you can make her doing all types of crazy things, like: <i>saving the data just changed with a ajax http request</i>, or <i>save the data in local storage</i> like in the example showed in this page
7171
</p>
7272

73+
<p ng-model="viewStrings.stripReplaceParagraph" focus-select="false" single-line="false" strip-replace="['html','<b>CHANGED to something else</b>','g']" content-editable>
74+
If you specify array or simply "true" inside a <b>strip replace</b> attribute every time your model change,
75+
<br>the text will be stripped of all html tags, in case of setting <b>strip replace</b> to "true".
76+
<br>If you specify array, the array will be converted to regular expression and the text will be matched (and changed) to your parametters.
77+
</p>
78+
7379
</section>
7480
</article>
7581
</body>

src/angular-content-editable.directive.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ angular.module('angular-content-editable')
55
var directive = {
66
restrict: 'A',
77
require: 'ngModel',
8-
scope: { editCallback: '&?', isEditing: '=?' },
8+
scope: { editCallback: '&?', isEditing: '=?', stripReplace: '=?' },
99
link: _link
1010
};
1111

@@ -19,7 +19,7 @@ angular.module('angular-content-editable')
1919
return;
2020
}
2121

22-
var noEscape = true, originalElement = elem[0], callback;
22+
var noEscape = true, originalElement = elem[0], callback, stripReplace;
2323

2424
// get default usage options
2525
var options = angular.copy(contentEditable);
@@ -34,6 +34,9 @@ angular.module('angular-content-editable')
3434
// Get the callback from item scope or global defined
3535
callback = scope.editCallback || options.editCallback;
3636

37+
// Get the strip tags option from item scope or global defined
38+
stripReplace = scope.stripReplace || options.stripReplace;
39+
3740
// add editable class
3841
attrs.$addClass(options.editableClass);
3942

@@ -122,7 +125,25 @@ angular.module('angular-content-editable')
122125

123126
// if element value is different from model value
124127
if( html != ngModel.$modelValue ) {
125-
128+
129+
// if user defined strip-replace variable
130+
if( stripReplace ){
131+
if( angular.isString(stripReplace) ) {
132+
// if stripReplace is a string create new RegExp with gi (global, ignore case)
133+
html = html.replace( new RegExp( stripReplace, 'g' ), '' );
134+
}else if( angular.isArray(stripReplace) ){
135+
// if stripReplace is an array create new RegExp from array values
136+
// get values from array or set default
137+
var e = stripReplace[0] || '', r = stripReplace[1] || '', f = stripReplace[2] || 'g';
138+
html = html.replace( new RegExp( e, f ), r );
139+
}else{
140+
// if stripReplace is set to "true", remove all html tags and new line breaks
141+
html = html.replace(/(<([^>]+)>)/ig, '').replace(/\r?\n|\r/g, '');
142+
}
143+
// update elem html value
144+
elem.html(html);
145+
}
146+
126147
/**
127148
* This method should be called
128149
* when a controller wants to

src/angular-content-editable.provider.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ angular.module('angular-content-editable')
1212
singleLine: false,
1313
focusSelect: true, // default on focus select all text inside
1414
renderHtml: false,
15-
editCallback: false
15+
editCallback: false,
16+
stripReplace: false
1617
}
1718

1819
this.configure = function (options) {

test/angular-content-editable.directive.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,24 @@ describe("Angular Content Editable: Directive", function () {
5252
scope.$digest();
5353
expect(scope.isEditing).toBe(false);
5454
});
55+
56+
it('should strip all html tags from text', function () {
57+
element = angular.element('<h1 ng-model="myModel" edit-callback="onEdit(\'extraArg\', text, elem)" is-editing="isEditing" strip-replace="true" content-editable></h1>');
58+
$compile(element)(scope);
59+
scope.$digest();
60+
element.triggerHandler('click');
61+
element.html('Some random text <b>change</b>.');
62+
element.triggerHandler('blur');
63+
expect(element.html()).toContain("Some random text change.");
64+
});
65+
66+
it('should replace all matching strings', function () {
67+
element = angular.element('<h1 ng-model="myModel" edit-callback="onEdit(\'extraArg\', text, elem)" is-editing="isEditing" strip-replace="[\'change\',\'success\',\'g\']" content-editable></h1>');
68+
$compile(element)(scope);
69+
scope.$digest();
70+
element.triggerHandler('click');
71+
element.html('Some random text change.');
72+
element.triggerHandler('blur');
73+
expect(element.html()).toContain("Some random text success.");
74+
});
5575
});

0 commit comments

Comments
 (0)