From 4bbfbabae7f85c810f1a614bb515a1454d3fb29c Mon Sep 17 00:00:00 2001 From: SimeonC Date: Wed, 28 Jan 2015 10:44:18 +1300 Subject: [PATCH] fix(taBind._blankTest): Fix for highly nested content. Fixes #512 --- lib/taBind.js | 17 +++++++++++++---- test/taBind/taBind._taBlankTest.spec.js | 21 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/taBind.js b/lib/taBind.js index 3cf2d291..e8765f83 100644 --- a/lib/taBind.js +++ b/lib/taBind.js @@ -4,11 +4,20 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM' return function(_defaultTest){ return function(_blankVal){ if(!_blankVal) return true; - // Don't do a global replace as that would be waaayy too long, just replace the first 4 occurences should be enough - _blankVal = _blankVal.toString().replace(/="[^"]*"/i, '').replace(/="[^"]*"/i, '').replace(/="[^"]*"/i, '').replace(/="[^"]*"/i, ''); - var _firstTagIndex = _blankVal.indexOf('>'); - if(_firstTagIndex === -1) return _blankVal.trim().length === 0; + // find first non-tag match - ie start of string or after tag that is not whitespace + var _firstMatch = /(^[^<]|>)[^<]/i.exec(_blankVal); + var _firstTagIndex; + if(!_firstMatch){ + // find the end of the first tag removing all the + // Don't do a global replace as that would be waaayy too long, just replace the first 4 occurences should be enough + _blankVal = _blankVal.toString().replace(/="[^"]*"/i, '').replace(/="[^"]*"/i, '').replace(/="[^"]*"/i, '').replace(/="[^"]*"/i, ''); + _firstTagIndex = _blankVal.indexOf('>'); + }else{ + _firstTagIndex = _firstMatch.index; + } _blankVal = _blankVal.trim().substring(_firstTagIndex, _firstTagIndex + 100); + // check for no tags entry + if(/^[^<>]+$/i.test(_blankVal)) return false; // this regex is to match any number of whitespace only between two tags if (_blankVal.length === 0 || _blankVal === _defaultTest || /^>(\s| )*<\/[^>]+>$/ig.test(_blankVal)) return true; // this regex tests if there is a tag followed by some optional whitespace and some text after that diff --git a/test/taBind/taBind._taBlankTest.spec.js b/test/taBind/taBind._taBlankTest.spec.js index 0377b3b4..6cf194ef 100644 --- a/test/taBind/taBind._taBlankTest.spec.js +++ b/test/taBind/taBind._taBlankTest.spec.js @@ -24,7 +24,26 @@ describe('taBind._taBlankTest', function () { '

Test Some

', 'Some Test', '


', - '

ATTITUDES:

' + '

ATTITUDES:

', + '
' + // a real world case from #512 + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '

Lorem ipsum

' + + '

Lorem ipsum

' + + '
' ], testString(false) );