Skip to content

Commit c3900a4

Browse files
SimeonCSimeonC
SimeonC
authored and
SimeonC
committed
fix(taBind): Add blankTest tests and seperate service
1 parent c08ddd7 commit c3900a4

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

lib/taBind.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'])
2-
.directive('taBind', ['taSanitize', '$timeout', '$window', '$document', 'taFixChrome', 'taBrowserTag', 'taSelection', 'taSelectableElements', 'taApplyCustomRenderers', 'taOptions',
3-
function(taSanitize, $timeout, $window, $document, taFixChrome, taBrowserTag, taSelection, taSelectableElements, taApplyCustomRenderers, taOptions){
2+
.service('_taBlankTest', [function(){
3+
var INLINETAGS_NONBLANK = /<(a|abbr|acronym|bdi|bdo|big|cite|code|del|dfn|img|ins|kbd|label|map|mark|q|ruby|rp|rt|s|samp|time|tt|var)[^>]*(>|$)/i;
4+
return function(_defaultTest){
5+
return function(_blankVal){
6+
if(!_blankVal) return true;
7+
_blankVal = _blankVal.toString();
8+
var _firstTagIndex = _blankVal.indexOf('>');
9+
if(_firstTagIndex === -1) return _blankVal.trim().length === 0;
10+
_blankVal = _blankVal.trim().substring(_firstTagIndex, _firstTagIndex + 100);
11+
// this regex is to match any number of whitespace only between two tags
12+
if (_blankVal.length === 0 || _blankVal === _defaultTest || /^>(\s|&nbsp;)*<\/[^>]+>$/ig.test(_blankVal)) return true;
13+
// this regex tests if there is a tag followed by some optional whitespace and some text after that
14+
else if (/>\s*[^\s<]/i.test(_blankVal) || INLINETAGS_NONBLANK.test(_blankVal)) return false;
15+
else return true;
16+
};
17+
};
18+
}])
19+
.directive('taBind', ['taSanitize', '$timeout', '$window', '$document', 'taFixChrome', 'taBrowserTag', 'taSelection', 'taSelectableElements', 'taApplyCustomRenderers', 'taOptions', '_taBlankTest',
20+
function(taSanitize, $timeout, $window, $document, taFixChrome, taBrowserTag, taSelection, taSelectableElements, taApplyCustomRenderers, taOptions, _taBlankTest){
421
// Uses for this are textarea or input with ng-model and ta-bind='text'
522
// OR any non-form element with contenteditable="contenteditable" ta-bind="html|text" ng-model
623
return {
@@ -15,7 +32,6 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
1532
var _lastKey;
1633
var BLOCKED_KEYS = /^(9|19|20|27|33|34|35|36|37|38|39|40|45|112|113|114|115|116|117|118|119|120|121|122|123|144|145)$/i;
1734
var UNDO_TRIGGER_KEYS = /^(8|13|32|46|59|61|107|109|186|187|188|189|190|191|192|219|220|221|222)$/i; // spaces, enter, delete, backspace, all punctuation
18-
var INLINETAGS_NONBLANK = /<(a|abbr|acronym|bdi|bdo|big|cite|code|del|dfn|img|ins|kbd|label|map|mark|q|ruby|rp|rt|s|samp|time|tt|var)[^>]*>/i;
1935

2036
// defaults to the paragraph element, but we need the line-break or it doesn't allow you to type into the empty element
2137
// non IE is '<p><br/></p>', ie is '<p></p>' as for once IE gets it correct...
@@ -39,16 +55,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
3955
'<' + attrs.taDefaultWrap + '>&nbsp;</' + attrs.taDefaultWrap + '>';
4056
}
4157

42-
var _blankTest = function(_blankVal){
43-
var _firstTagIndex = _blankVal.indexOf('>');
44-
if(_firstTagIndex === -1) return _blankVal.trim().length === 0;
45-
_blankVal = _blankVal.trim().substring(_firstTagIndex, _firstTagIndex + 100);
46-
// this regex is to match any number of whitespace only between two tags
47-
if (_blankVal.length === 0 || _blankVal === _defaultTest || /^>(\s|&nbsp;)*<\/[^>]+>$/ig.test(_blankVal)) return true;
48-
// this regex tests if there is a tag followed by some optional whitespace and some text after that
49-
else if (/>\s*[^\s<]/i.test(_blankVal) || INLINETAGS_NONBLANK.test(_blankVal)) return false;
50-
else return true;
51-
};
58+
var _blankTest = _taBlankTest(_defaultTest);
5259

5360
element.addClass('ta-bind');
5461

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
describe('taBind._taBlankTest', function () {
2+
'use strict';
3+
beforeEach(module('textAngular'));
4+
afterEach(inject(function($document){
5+
$document.find('body').html('');
6+
}));
7+
var testString = function(result){ return function(_str){
8+
it(_str, inject(function (_taBlankTest) {
9+
expect(_taBlankTest('<p><br></p>')(_str)).toBe(result);
10+
}));
11+
};};
12+
13+
describe('should return true for', function () {
14+
it('undefined', inject(function (_taBlankTest) {
15+
expect(_taBlankTest('<p><br></p>')()).toBe(true);
16+
}));
17+
angular.forEach(['<p></p>','<p><br></p>',''], testString(true));
18+
});
19+
20+
describe('should return false for', function () {
21+
angular.forEach(
22+
['<p>test</p>','<p>Test Some<br></p>','Some Test','<p><img class="ta-insert-video" src="https://img.youtube.com/vi/sbQQKI1Fwo4/hqdefault.jpg" ta-insert-video="https://www.youtube.com/embed/sbQQKI1Fwo4" contenteditable="false" allowfullscreen="true" frameborder="0"><br></p>'],
23+
testString(false)
24+
);
25+
});
26+
27+
});

test/taBind/taBind.validation.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ describe('taBind.validation', function () {
169169

170170
describe('should handle inline elements like img', function () {
171171
beforeEach(function(){
172-
$rootScope.html = '<pre><img src="test.jpg"/></pre>';
172+
$rootScope.html = '<p><img src="test.jpg"/></p>';
173173
$rootScope.$digest();
174174
});
175175
it('ng-required', function(){

0 commit comments

Comments
 (0)